mcg*_*gag 3 iptables port-forwarding
我的服务在端口 30000-32000 上的 VPS A(1.1.1.1) 上运行。
我想要 VPS B(2.2.2.2) 将它们端口转发到 20000-22000。
(您可以查看下面的演示图链接以了解我想要实现的目标:D)
我使用下面的命令来设置 iptables:
iptables -t nat -A PREROUTING -p tcp --dport 20000:22000 -j DNAT --to-destination 1.1.1.1:30000-32000
iptables -t nat -A POSTROUTING -p tcp -d 1.1.1.1 --dport 30000:32000 -j SNAT --to-source 2.2.2.2
20000:22000
iptables -t nat -A PREROUTING -p udp --dport 20000:22000 -j DNAT --to-destination 1.1.1.1:30000-32000
iptables -t nat -A POSTROUTING -p udp -d 1.1.1.1 --dport 30000:32000 -j SNAT --to-source 2.2.2.2
20000:22000
Run Code Online (Sandbox Code Playgroud)
经过一些测试,我发现似乎只有20000端口转发了原服务器的30000端口。但是其他端口都不起作用。
我检查了以下 4 件事: 1. VPS A (1.1.1.1) 的服务在端口 30000 - 32000 上运行的服务功能齐全
(IE net.ipv4.ip_forward = 1)
(您可以查看下面的演示图链接,了解我想要实现的目标:D)
一切正常。
我真的不知道如何实现它。任何帮助表示赞赏!先感谢您!
您需要更改 iptables 规则。您的 DNAT 规则不会过滤来自用户的传入流量的目标 IP 地址。您的 SNAT 规则不会过滤来自 A 服务器的传入流量的源 IP 地址和源端口 30000:32000 范围。你需要在B服务器上:
iptables -t nat -A PREROUTING -p tcp -d 2.2.2.2 --dport 20000:22000 -j DNAT --to-destination 1.1.1.1:30000-32000
iptables -t nat -A POSTROUTING -p tcp -s 1.1.1.1 --sport 30000:32000 -j SNAT --to-source 2.2.2.2:20000:22000
iptables -t nat -A PREROUTING -p udp -d 2.2.2.2 --dport 20000:22000 -j DNAT --to-destination 1.1.1.1:30000-32000
iptables -t nat -A POSTROUTING -p udp -s 1.1.1.1 --sport 30000:32000 -j SNAT --to-source 2.2.2.2:20000:22000
Run Code Online (Sandbox Code Playgroud)