如何从iptables中删除特定规则?

Jer*_*oen 308 linux firewall iptables

我分别在端口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 
Run Code Online (Sandbox Code Playgroud)

这就像一个魅力.但是,我想创建另一个脚本,再次禁用我的服务器; 即在运行上面的行之前将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
Run Code Online (Sandbox Code Playgroud)

但这也将删除其他不受欢迎的iptables规则.

Eli*_*uft 440

执行相同的命令,但将"-A"替换为"-D".例如:

iptables -A ...
Run Code Online (Sandbox Code Playgroud)

iptables -D ...
Run Code Online (Sandbox Code Playgroud)

  • 如果您有多种规则,则不会删除所有规则. (7认同)
  • 尝试多次执行此-D命令,它将删除所有这些命令. (4认同)
  • 如果你添加了一个带有`-I`或`-R`的规则,你仍然可以用`-D`删除它. (4认同)
  • 我执行相同的命令,但使用-D而不是-I.但我得到了BAD RULE(存在匹配规则)...... (3认同)

dom*_*i27 423

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

iptables -L INPUT --line-numbers
Run Code Online (Sandbox Code Playgroud)

示例输出:

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
Run Code Online (Sandbox Code Playgroud)

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

iptables -D INPUT 2
Run Code Online (Sandbox Code Playgroud)

更新

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

sudo iptables -t nat -D PREROUTING 1
Run Code Online (Sandbox Code Playgroud)

  • 请记住,如果删除一条规则,则其余的行号会发生变化.因此,如果您需要删除规则5,10和12 ...将它们删除12,10,然后删除5. (13认同)
  • 只有当表格不能在任何时间插入规则时,这才是好的.否则,行号可能会在观察它们和执行删除规则之间发生变化.在这种情况下,假设时间窗口太短以至于"不太可能发生"是不安全的. (5认同)
  • 这两个解决方案都很不错,但是当行号未知时,这个解决方案将不适用于脚本设置.所以其他解决方案更通用,因此更正确,IMO. (4认同)
  • 好吧,如果你不知道这行,你可以使用注释(比如回答)或为你的规则做一个grep:`iptables -L INPUT --line-numbers | grep -oP"([0-9] {1,3}).*tcp.*domain"| cut -d"" - f1` (2认同)
  • 当试图删除PREROUTING规则时,我必须指定`-t nat`,例如:`sudo iptables -t nat --line-numbers -L`,然后也用`-t nat`删除它们,例如:`sudo iptables- t nat -D PREROUTING 1`(可能值得添加答案吗?) (2认同)

ETe*_*ech 29

对我来说没有任何问题的最佳解决方案就是这样:
1.添加一些注释的临时规则:

comment=$(cat /proc/sys/kernel/random/uuid | sed 's/\-//g')
iptables -A ..... -m comment --comment "${comment}" -j REQUIRED_ACTION
Run Code Online (Sandbox Code Playgroud)

2.当规则添加并且您希望删除它(或带有此注释的所有内容)时,请执行以下操作:

iptables-save | grep -v "${comment}" | iptables-restore
Run Code Online (Sandbox Code Playgroud)

因此,您将100%删除与$ comment匹配的所有规则,并保持其他行不变.这个解决方案在过去的2个月里工作,每天约100次规则变更 - 没有问题.希望,这有帮助

  • 如果您没有iptables-save/restore:`iptables -S | grep"$ {comment}"| sed's/^ - A //'| 阅读规则; 做iptables -D $规则; done` (2认同)
  • @Mansour或`iptables -S | sed"/ $ comment/s/-A/iptables -D/e"`;)喜欢[this](http://stackoverflow.com/a/29058984/171318) (2认同)

ciz*_*ixs 12

使用-D命令,这是man页面解释它的方式:

-D, --delete chain rule-specification
-D, --delete chain rulenum
    Delete  one  or more rules from the selected chain.  
    There are two versions of this command: 
    the rule can be specified as a number in the chain (starting at 1 for the first rule) or a rule to match.
Run Code Online (Sandbox Code Playgroud)

一定要意识到这个命令,就像所有其他命令(-A-I)在某些表上工作一样。如果您不在默认表(filter表)上工作,请使用-t TABLENAME指定该目标表。

删除要匹配的规则

iptables -D INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

注意:这只会删除匹配的第一个规则。如果您有许多匹配的规则(这可能发生在 iptables 中),请多次运行。

删除指定为数字的规则

iptables -D INPUT 2
Run Code Online (Sandbox Code Playgroud)

除了计算数字之外,您还可以列出带有--line-number参数的行号,例如:

iptables -t nat -nL --line-number
Run Code Online (Sandbox Code Playgroud)


小智 6

首先使用此命令列出所有iptables规则:

iptables -S
Run Code Online (Sandbox Code Playgroud)

它列出如下:

-A XYZ -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

然后复制所需的行,只需更换-A-D删除:

iptables -D XYZ -p ...
Run Code Online (Sandbox Code Playgroud)

  • 意识到!这是不完整的答案。从关于“ -S”的手册中:“就像其他所有iptables命令一样,它适用于指定的表(默认为过滤器)。因此,在使用此开关的情况下-应该对所有表重复:nat,mangle等 (2认同)

lak*_*dan 5

假设,如果要删除 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
Run Code Online (Sandbox Code Playgroud)

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

# sudo iptables -F -t nat -v

Flushing chain `PREROUTING'
Flushing chain `INPUT'
Flushing chain `OUTPUT'
Flushing chain `POSTROUTING'
Run Code Online (Sandbox Code Playgroud)

然后,您可以验证,

# sudo iptables -L -t nat -v
Run Code Online (Sandbox Code Playgroud)