我分别在端口8006和8007上托管特殊的HTTP和HTTPS服务。我使用iptables来“激活”服务器;即路由传入的HTTP和HTTPS端口:

iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8006 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8007 -j ACCEPT
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8006 
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8007  
iptables -A OUTPUT -t nat -d 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to-ports 8006
iptables -A OUTPUT -t nat -d 127.0.0.1 -p tcp --dport 443 -j REDIRECT --to-ports 8007 

这就像一个咒语。然而,我想创建另一个脚本,再次禁用我的服务器;例如,将iptables恢复到运行上述代码行之前的状态。然而,我很难弄清楚删除这些规则的语法。唯一有效的方法是同花:

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

但这也会删除其他不需要的iptables规则。


当前回答

下面是删除与搜索匹配的iptables规则的一行代码。本示例搜索与IP地址192.168.1.27匹配的所有规则,并删除所有规则。您可以在自己的搜索条件中编辑该IP地址。

eval `iptables --list-rules | grep '192.168.1.27' | sed 's/^-A /iptables -D /g;s/$/;/g'`

工作原理:

它使用这个问题的公认答案,并使用-D而不是-A运行规则。

iptables --list-rules lists all the existing rules. Even if you added them with -I or -R, this list shows them all with -A | grep '192.168.1.27' filters the list to just the rules that you want removed (in this case the rules for some specific IP address.) | sed does a search and replace s/^-A /iptables -D /g replaces the -A at the start of each rule with iptables -D so that it becomes an executable command to remove the rule. s/$/;/g replaces the end of each rule with a semi-colon to separate multiple commands when they are run eval ... takes all that output and runs it as a script.

其他回答

你也可以使用规则的编号(——line-numbers):

iptables -L INPUT --line-numbers

示例输出:

Chain INPUT (policy ACCEPT) 
    num  target prot opt source destination
    1    ACCEPT     udp  --  anywhere  anywhere             udp dpt:domain 
    2    ACCEPT     tcp  --  anywhere  anywhere             tcp dpt:domain 
    3    ACCEPT     udp  --  anywhere  anywhere             udp dpt:bootps 
    4    ACCEPT     tcp  --  anywhere  anywhere             tcp dpt:bootps

所以如果你想删除第二条规则:

iptables -D INPUT 2

更新

如果你使用(d)一个特定的表(例如nat),你必须把它添加到删除命令(谢谢@ThorSummoner的评论)

sudo iptables -t nat -D PREROUTING 1

首先用下面的命令列出所有iptables规则:

iptables -S

列表如下:

-A XYZ -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

然后复制想要的行,用-D替换-A来删除它:

iptables -D XYZ -p ...

下面是删除与搜索匹配的iptables规则的一行代码。本示例搜索与IP地址192.168.1.27匹配的所有规则,并删除所有规则。您可以在自己的搜索条件中编辑该IP地址。

eval `iptables --list-rules | grep '192.168.1.27' | sed 's/^-A /iptables -D /g;s/$/;/g'`

工作原理:

它使用这个问题的公认答案,并使用-D而不是-A运行规则。

iptables --list-rules lists all the existing rules. Even if you added them with -I or -R, this list shows them all with -A | grep '192.168.1.27' filters the list to just the rules that you want removed (in this case the rules for some specific IP address.) | sed does a search and replace s/^-A /iptables -D /g replaces the -A at the start of each rule with iptables -D so that it becomes an executable command to remove the rule. s/$/;/g replaces the end of each rule with a semi-colon to separate multiple commands when they are run eval ... takes all that output and runs it as a script.

假设要删除NAT规则,

使用下面的命令列出附加的IPtables,

# sudo iptables -L -t nat -v

Chain PREROUTING (policy ACCEPT 18 packets, 1382 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    7   420 DNAT       tcp  --  any    any     anywhere             saltmaster           tcp dpt:http to:172.31.5.207:80
    0     0 DNAT       tcp  --  eth0   any     anywhere             anywhere             tcp dpt:http to:172.31.5.207:8080

如果您想从IPtables中删除nat规则,只需执行以下命令,

# sudo iptables -F -t nat -v

Flushing chain `PREROUTING'
Flushing chain `INPUT'
Flushing chain `OUTPUT'
Flushing chain `POSTROUTING'

然后,你可以验证,

# sudo iptables -L -t nat -v

您还可以使用以下语法

 iptables -D <chain name> <rule number>

例如

Chain HTTPS 
    target     prot opt source               destination
    ACCEPT     all  --  anywhere             anywhere
    ACCEPT     all  --  10.0.0.0/8           anywhere
    ACCEPT     all  --  182.162.0.0/16       anywhere

删除规则

ACCEPT all—10.0.0.0/8任意位置

iptables -D HTTPS 2