在Linux上如何使用命令行删除iptables规则

假设您正在使用“iptables”,并希望删除不再有效、不再需要或者不正确的规则。

完成此任务的一种方法是使用“iptables save”命令保存所有规则,打开输出文件,删除所有规则,并使用“iptables restore”应用新规则。

另一种可能更简单的方法是列出所有可用的规则以及规则行号。
例如:

# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    DROP       all  --  anywhere             10.0.0.0/8          
2    DOCKER     all  --  anywhere             anywhere            
3    ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
4    ACCEPT     all  --  anywhere             anywhere            
5    ACCEPT     all  --  anywhere             anywhere            
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
Chain DOCKER (1 references)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  anywhere             172.17.0.3           tcp dpt:https
2    ACCEPT     tcp  --  anywhere             172.17.0.4           tcp dpt:http
3    ACCEPT     tcp  --  anywhere             172.17.0.5           tcp dpt:4000
4    ACCEPT     tcp  --  anywhere             172.17.0.7           tcp dpt:mysql
5    ACCEPT     tcp  --  anywhere             172.17.0.7           tcp dpt:http
6    ACCEPT     tcp  --  anywhere             172.17.0.6           tcp dpt:3142

注意,左列中的行号。

现在我们有了所有行号,我们可以删除列出的任何iptables规则。

例如,要删除1 DROP all -anywhere 10.0.0.0/8

我们首先需要注意iptables链名,在本例中为'FORWARD',规则号为'1'。
要删除此规则,请输入以下iptables命令:

# iptables -D FORWARD 1
日期:2020-07-07 20:56:30 来源:oir作者:oir