为什么 iptables 规则会重定向传入流量并阻止其他端口/服务?

为什么 iptables 规则会重定向传入流量并阻止其他端口/服务?

我遵循了一些帖子,以便使用 iptables 将所有传入流量从公共 IP 重定向到内部 IP 地址。我设置sudo sysctl net.ipv4.ip_forward=1并使用了以下规则:

iptables -t nat -A PREROUTING -p tcp --dport 9080 -j DNAT --to-destination 192.168.178.1:80
iptables -t nat -A POSTROUTING -j MASQUERADE

我的想法是,我有一个远程服务器(Ubuntu 20.04.1 LTS),我始终可以使用反向 SSH 隧道访问它。在该网络中,我想访问一些设备,例如网关和其他设备。使用 iptables,我希望能够访问网关的 GUI。在本地网络上,规则有效!

问题是,在服务器上我无法设置隧道以将 9080 重定向到 192.168.178.1:80。当我在服务器上尝试其他操作(例如 ping)时,我得到了以下结果:

$ ping google.com
ping: google.com: Temporary failure in name resolution

当我设置隧道时:

$ ssh [email protected]
ssh: Could not resolve hostname remoteserverfortunnel.com: Temporary failure in name resolution

当我在规则之前设置隧道(使用 -D 删除)并再次添加规则时,在请求时会得到此信息remoteserverfortunnel.com:9080

ssh -N -R 9080:localhost:9080 [email protected]
[email protected]'s password: 
connect_to localhost port 9080: failed.

我仍然可以使用正常的 22 端口 SSH 登录服务器。

我的问题是,为什么规则会阻止隧道?我是否需要添加其他规则?

编辑:

我添加了界面选项:

iptables -t nat -A PREROUTING -i enp2s0 -p tcp --dport 9080 -j DNAT --to 192.168.1.103:80
iptables -t nat -A POSTROUTING -o enp2s0 -j MASQUERADE

名称解析失败问题已解决。不幸的是,使用隧道的想法仍然行不通,我明白了connect_to localhost port 9080: failed.

我确实发现我也可以直接通过隧道连接到设备,例如使用。使用这种方法可以避免使用 iptables,但我仍然希望了解 IP 表选项。有些设备也无法通过隧道按预期工作。ssh -N -R 9080:192.168.178.1:80 [email protected]

相关内容