2017年02月28日
Tweet
Ubuntu で iptables の設定を保存する
僕は今まで CentOS をメインで利用しており、このとき iptables は service コマンドなどで管理できました。
しかし Ubuntu は この方法ではできないようです。
一番簡単なのは iptables-persistent を利用するようです。
iptables-persistent の場合
ルールの記載
#vi /etc/iptables/iptables.rules
# iptables-restore < /etc/iptables/iptables.rules
とりあえず僕の環境では以下の方法で行いました。
参考
LinuxのUbuntuやDebianでiptablesの設定方法・設定ファイル
IptablesHowTo
・設定ファイル
/etc/network/if-pre-up.d/iptables.up
/etc/network/if-post-down.d/iptables.down
/etc/network/interfaces
/etc/sysconfig/iptables.rule # 今までと同じ方法でルールを編集したかったので
まずは起動時に設定を読み込むようにします。
# vi /etc/network/if-pre-up.d/iptables.up
----
#!/bin/sh
/sbin/iptables-restore < /etc/sysconfig/iptables.rule
----
権限付与
# chmod +x /etc/network/if-pre-up.d/iptables.up
次はiptables の初期化設定を行います。
# vi /etc/network/if-post-down.d/iptables.down
----
#!/bin/sh
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F # ルールの削除
iptables -Z # カウンターのリセット
iptables -X # 追加チェインの削除
# nat 等の設定は上記コマンドでは初期化されません。
----
権限付与
# chmod +x /etc/network/if-post-down.d/iptables.down
・インターフェースの設定で読み込むように設定
/etc/network/interfaces
pre-up /etc/network/if-pre-up.d/iptables.up
post-down /etc/network/if-post-down.d/iptables.down
※なお今回の運用では、iptables に設定するコマンドはすべてテキストファイルに書き込むことを前提としています。ipta
bles コマンドを利用して直接書き込んだ場合には、service networking restart などによって消えてしまうので注意が必要です。
あと、設定ファイルを書き換えた場合には、 iptables.up を直接実行します。

僕は今まで CentOS をメインで利用しており、このとき iptables は service コマンドなどで管理できました。
しかし Ubuntu は この方法ではできないようです。
一番簡単なのは iptables-persistent を利用するようです。
iptables-persistent の場合
ルールの記載
#vi /etc/iptables/iptables.rules
# iptables-restore < /etc/iptables/iptables.rules
とりあえず僕の環境では以下の方法で行いました。
参考
LinuxのUbuntuやDebianでiptablesの設定方法・設定ファイル
IptablesHowTo
・設定ファイル
/etc/network/if-pre-up.d/iptables.up
/etc/network/if-post-down.d/iptables.down
/etc/network/interfaces
/etc/sysconfig/iptables.rule # 今までと同じ方法でルールを編集したかったので
まずは起動時に設定を読み込むようにします。
# vi /etc/network/if-pre-up.d/iptables.up
----
#!/bin/sh
/sbin/iptables-restore < /etc/sysconfig/iptables.rule
----
権限付与
# chmod +x /etc/network/if-pre-up.d/iptables.up
次はiptables の初期化設定を行います。
# vi /etc/network/if-post-down.d/iptables.down
----
#!/bin/sh
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F # ルールの削除
iptables -Z # カウンターのリセット
iptables -X # 追加チェインの削除
# nat 等の設定は上記コマンドでは初期化されません。
----
権限付与
# chmod +x /etc/network/if-post-down.d/iptables.down
・インターフェースの設定で読み込むように設定
/etc/network/interfaces
pre-up /etc/network/if-pre-up.d/iptables.up
post-down /etc/network/if-post-down.d/iptables.down
※なお今回の運用では、iptables に設定するコマンドはすべてテキストファイルに書き込むことを前提としています。ipta
bles コマンドを利用して直接書き込んだ場合には、service networking restart などによって消えてしまうので注意が必要です。
あと、設定ファイルを書き換えた場合には、 iptables.up を直接実行します。