iptables ルール追加・挿入・削除・置き換え

スポンサーリンク
スポンサーリンク

Linux での iptables ルールの追加・挿入・削除・置き換えです。

ルール番号確認

ルールを一覧表示する際に、左端に番号も表示します。
番号はチェイン毎です。

iptables -L --line-numbers

ここで表示されたルール番号は、ルールの削除や挿入、置き換えの際に指定します。

この記事で説明した iptablesのサンプルでは、次のように表示されます。

# iptables -L --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere
2    DROP       tcp  --  anywhere             anywhere            tcp flags:!FIN,SYN,RST,ACK/SYN state NEW
3    REJECT     tcp  --  anywhere             anywhere            tcp flags:SYN,ACK/SYN,ACK state NEW reject-with tcp-reset
4    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
5    ACCEPT     icmp --  anywhere             anywhere            icmp destination-unreachable
6    ACCEPT     icmp --  anywhere             anywhere            icmp source-quench
7    ACCEPT     icmp --  anywhere             anywhere            icmp redirect
8    ACCEPT     icmp --  anywhere             anywhere            icmp time-exceeded
9    ACCEPT     icmp --  anywhere             anywhere            icmp parameter-problem
10   ACCEPT_FILTER  icmp --  anywhere             anywhere            icmp echo-request
11   sshguard   tcp  --  anywhere             anywhere
12   REJECT     tcp  --  anywhere             anywhere            tcp dpt:auth reject-with tcp-reset
13   ACCEPT_FILTER  tcp  --  anywhere             anywhere            tcp dpt:ssh state NEW
Chain FORWARD (policy DROP)
num  target     prot opt source               destination
Chain OUTPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere
2    ACCEPT     all  --  anywhere             anywhere            state NEW,ESTABLISHED
Chain ACCEPT_FILTER (2 references)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere
Chain sshguard (1 references)
num  target     prot opt source               destination

ルール削除

下記書式で実行します。

iptables -D チェイン ルール番号

実行例です。

まず、ルール番号を確認します。
# iptables -L --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere
2    DROP       tcp  --  anywhere             anywhere            tcp flags:!FIN,SYN,RST,ACK/SYN state NEW
3    REJECT     tcp  --  anywhere             anywhere            tcp flags:SYN,ACK/SYN,ACK state NEW reject-with tcp-reset
      :
      :
ルール1番を削除します。
# iptables -D INPUT 1
ルールを一覧表示させます。
# iptables -L --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    DROP       tcp  --  anywhere             anywhere            tcp flags:!FIN,SYN,RST,ACK/SYN state NEW
2    REJECT     tcp  --  anywhere             anywhere            tcp flags:SYN,ACK/SYN,ACK state NEW reject-with tcp-reset
      :
      :
1番のルールが削除され、2番以降が繰り上がっています。

ルール追加

末尾に追加する場合

下記書式で実行します。

iptables -A チェイン ルール

この末尾へ追加は、下記記事で多用していますね。

iptablesサンプル パソコン鳥のブログ

先頭に追加する場合

下記書式で実行します。

iptables -I チェイン ルール
または
iptables -I チェイン 1 ルール

実行例です。

iptables -I INPUT -p tcp --dport 25 -m state --state NEW -j ACCEPT

挿入する場合

下記書式で実行します。

iptables -I チェイン ルール番号 ルール

ルール番号の個所に挿入され、以降のルールが繰り下がります。
実行例です。

まず、ルール番号を確認します。
# iptables -L --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere
2    DROP       tcp  --  anywhere             anywhere            tcp flags:!FIN,SYN,RST,ACK/SYN state NEW
3    REJECT     tcp  --  anywhere             anywhere            tcp flags:SYN,ACK/SYN,ACK state NEW reject-with tcp-reset
4    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
5    ACCEPT     icmp --  anywhere             anywhere            icmp destination-unreachable
      :
      :
ルール5番の個所に挿入します。
# iptables -I INPUT 5 -p tcp --dport 25 -m state --state NEW -j ACCEPT
ルールを一覧表示させます。
# iptables -L --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere
2    DROP       tcp  --  anywhere             anywhere            tcp flags:!FIN,SYN,RST,ACK/SYN state NEW
3    REJECT     tcp  --  anywhere             anywhere            tcp flags:SYN,ACK/SYN,ACK state NEW reject-with tcp-reset
4    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
5    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:smtp state NEW
6    ACCEPT     icmp --  anywhere             anywhere            icmp destination-unreachable
      :
      :
5番の個所にルールが挿入され、元5番以降が繰り下がっています。

ルール置き換え

下記書式で実行します。

iptables -R チェイン ルール番号 ルール

指定されたチェインのルール番号のルールを置き換えます。

実行例です。

まず、ルール番号を確認します。
# iptables -L --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere
2    DROP       tcp  --  anywhere             anywhere            tcp flags:!FIN,SYN,RST,ACK/SYN state NEW
3    REJECT     tcp  --  anywhere             anywhere            tcp flags:SYN,ACK/SYN,ACK state NEW reject-with tcp-reset
4    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
5    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:smtp state NEW
6    ACCEPT     icmp --  anywhere             anywhere            icmp destination-unreachable
      :
      :
ルール5番を置き換えます。
# iptables -R INPUT 5 -p tcp --dport 22 -m state --state NEW -j ACCEPT
ルールを一覧表示させます。
# iptables -L --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere
2    DROP       tcp  --  anywhere             anywhere            tcp flags:!FIN,SYN,RST,ACK/SYN state NEW
3    REJECT     tcp  --  anywhere             anywhere            tcp flags:SYN,ACK/SYN,ACK state NEW reject-with tcp-reset
4    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
5    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh state NEW
6    ACCEPT     icmp --  anywhere             anywhere            icmp destination-unreachable
      :
      :
5番のルールが変更されました。

参考

Man page of IPTABLES
iptablesによるパケットフィルタリング
iptablesのルールをひとつ削除 | 俺のひとり言

コメント

タイトルとURLをコピーしました