如何在 iptables 中编辑规则?

How do you edit a rule in iptables?

我在 iptables 中有一条如下所示的规则:

DROP       all  --  5.158.238.32         anywhere 

但我想将其更改为:

DROP       all  --  5.158.0.0/16         anywhere

我该怎么做?

我找到了有关如何添加规则的信息,但这似乎是将规则附加到列表的末尾,并且出于某种原因,该规则只有在更高的位置才会生效。

我还找到了有关编辑文件的信息,但我的发行版 (debain) 似乎没有文章中提到的任何位置的文件 - 是否有我可以在某处编辑的文件?

任何正确方向的指示都将不胜感激。

干杯

运行 iptables -L --line-numbers,这将为您提供所有当前规则及其规则编号。确定要替换的规则的行号后,运行 iptables -R <chain> <rulenum> <new rule def>。在你的情况下,第一个的输出将是这样的(非常 t运行cated):

Chain INPUT (policy ACCEPT)
num  target     prot opt source       destination
....
12   DROP       all  --  5.158.238.32 anywhere
...

要替换它,您需要 运行:

iptables -R INPUT 12 -s 5.158.0.0/16 -j DROP

希望这是有道理的。祝你好运!