撤销端口转发


16

我需要将端口80的所有请求转发到8020。我用Google搜索它,然后得到:

iptables -t nat -I PREROUTING --source 0/0 --destination 0/0 -p tcp --dport 80 -j REDIRECT --to-ports 8020

现在,将来如果我必须撤消它,该怎么办(除了重新启动系统)?

Answers:


14

只需删除规则:

iptables -t nat -D PREROUTING --source 0/0 --destination 0/0 -p tcp --dport 80 -j REDIRECT --to-ports 8020

18

当我想删除iptable规则时,我感到很痛苦。我立即用行号列出了规则,然后按数字删除了。例如:

iptables -t nat -L --line-numbers

给出如下输出:

Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 redir ports 8020 

然后按数字删除:

iptables -t nat -D PREROUTING 1

注意:删除一行时,下面的所有行都会获得一个新的行号。例如,如果您有以下规则:

1 rule A
2 rule B
3 rule C

然后删除规则2,则得到:

1 rule A
2 rule C

这个答案比公认的答案有用得多,感谢您的来信@brainsik
rjhilgefort

0

我想知道是否可以使用vim

iptables-save > editme

vim editme # remove the rule you dont want

iptables-restore < editme

0

您还可以使用此代码删除整个端口转发规则

sudo iptables -P FORWARD DROP
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.