iptables 特定のルールを削除する方法

2021-05-18

iptablesでルールの番号を指定してルールを削除する方法

iptablesで特定のルールやテスト等で一時的に作ったルールだけを削除したい場合があると思います。その場合の方法を紹介します。

iptableで、ルール番号表示する方法

特定のルールなど1つだけ方法は、まずがルールの番号を表示します。

書式:iptables -L --line-numbers

書式:iptables -nL --line-numbers  #IPアドレスのみで表示(ホスト名・ネットワーク名・サービス名で表示されしない)

※オプション「n」では、ホスト名・ネットワーク名・サービス名で表示されしないでIPアドレスのみで表示します

 

$ sudo iptables -nL --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1 DROP all -- 1xx.1xx.1xx.1xx anywhere
2 DROP all -- 2xx.2xx.2xx.2xx anywhere
3 ACCEPT all -- anywhere anywhere
4 ACCEPT all -- www01 anywhere
5 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
6 ACCEPT tcp -- anywhere anywhere tcp dpt:smtp
7 ACCEPT tcp -- anywhere anywhere tcp dpt:http
8 ACCEPT tcp -- anywhere anywhere tcp dpt:pop3
9 ACCEPT tcp -- anywhere anywhere tcp dpt:imap
10 ACCEPT tcp -- anywhere anywhere tcp dpt:submission

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

iptableで、特定のルールのみ削除する方法

特定のルールだけ削除するには、チェインを指定してルール番号を指定します。ルール番号は先程のコマンド(iptables -L --line-numbers)を使用するとルールの横に表示される数字です

書式:iptables -D チェイン ルール番号

# 上記の設定で「1 DROP all -- 1xx.1xx.1xx.1xx anywhere」を削除する場合
$ sudo iptables -D INPUT 1

# 上記の設定で「10 ACCEPT tcp -- anywhere anywhere tcp dpt:submission」を削除する場合
$ sudo iptables -D INPUT 10

 

iptableで、ルールのみ削除後に変更内容を保存する

設定を変更しても、「/etc/sysconfig/iptables」の設定が反映していないので、「iptables save」で設定を保存する必要があります

$ sudo service iptables save

 

スポンサーリンク

0
0