使用 iptables 进行端口转发时出错?

使用 iptables 进行端口转发时出错?

我有一个相当标准的 ubuntu 安装,并希望添加端口转发。

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

#Redirect 1731 to  8443
-t nat -A PREROUTING -i eth0 -p tcp --dport 1731 -j REDIRECT --to-port 8443

#Allow 443 and 80
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT


-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

但是我一直收到错误:

iptables: Applying firewall rules: iptables-restore v1.4.7: Line 13 seems to have a -t table option.

我已确认该设备已启用 IP 转发:

cat /proc/sys/net/ipv4/ip_forward 
1

我确信我遗漏了 iptables 格式中的一些简单内容。您觉得呢?

答案1

以 开头的行-t nat不应该位于过滤表下方,如第一行所示。

将文件更改为以下内容:

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

#Allow 443 and 80
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]

#Redirect 1731 to  8443
-A PREROUTING -i eth0 -p tcp --dport 1731 -j REDIRECT --to-port 8443

COMMIT

相关内容