将对我的外部 IP/端口的请求重定向到不同的外部 IP/端口?

Pri*_*rix 14 iptables redirect

是否可以将到特定 IP/端口的连接重定向到外部 IP/端口?

例子:

  • 服务器 A 有外网 IP xxx.xxx.xxx.xxx
  • 服务器 B 的外部 IP 为 yyy.yyy.yyy.yyy
  • 用户在端口 2106 上向服务器 A 发送请求,我想将其重定向到端口 2106 的服务器 B。

Pri*_*rix 22

问题解决了:

sysctl net.ipv4.ip_forward=1
iptables -t nat -A PREROUTING -p tcp --dport port -j DNAT --to-destination ip:port
iptables -t nat -A POSTROUTING -j MASQUERADE
Run Code Online (Sandbox Code Playgroud)

其中 ip 和 port 是我要将当前服务器端口重定向到的目标服务器。