无法使用 iptables 将 ip 流量重定向到 Linux CentOS 上的新 ip

无法使用 iptables 将 ip 流量重定向到 Linux CentOS 上的新 ip

今天我能够将一些游戏服务器迁移到另一台服务器,并且需要一些帮助将流量从旧 IP 重定向到新 IP。

服务器 1 1.1.1.1 ----- (互联网) -----> 服务器 2.2.2.2

我假设使用 iptables 来执行此操作,因为在 server1 中的 centOS 机器上使用了此规则。

/etc/sysctl.conf:net.ipv4.ip_forward = 1

iptables -t nat -A PREROUTING -p udp --dest 1.1.1.1 --dport 27015 -j DNAT --to-destination 2.2.2.2:27015
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -t nat -A POSTROUTING -d 2.2.2.2 -p udp --dport 27015 -j SNAT --to 1.1.1.1

但客户端无法从旧 IP 连接到服务器,重定向未启动。

答案1

你可以做类似的事情:

iptables -t nat -A OUTPUT -p all -d 1.1.1.1 -j DNAT --to-destination 2.2.2.2 
iptables -t nat -A POSTROUTING -p all -j MASQUERADE

答案2

如果您之前没有在此服务器上配置任何 NAT,则可能没有设置内核来传递 IPv4 流量。

确保你的文件中有这个/etc/sysctl.conf

net.ipv4.ip_forward = 1

相关内容