取消端口转发

raj*_*raj 23 iptables port-forwarding

我需要将所有对端口 80 的请求转发到 8020。我用谷歌搜索了一下,得到:

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

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

小智 26

当我想删除它时,我发现完全重建 iptable 规则很痛苦。Instaed 我用行号列出规则,然后按编号删除。例如:

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

给出如下输出:

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

然后按数字删除:

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

警告:当你删除一行时,下面的所有行都会得到一个新的行号。例如,如果您有以下规则:

1 rule A
2 rule B
3 rule C
Run Code Online (Sandbox Code Playgroud)

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

1 rule A
2 rule C
Run Code Online (Sandbox Code Playgroud)


bas*_*lei 17

只需删除规则:

iptables -t nat -D PREROUTING --source 0/0 --destination 0/0 -p tcp --dport 80 -j REDIRECT --to-ports 8020
Run Code Online (Sandbox Code Playgroud)