Redirect requests to my external IP/port to a different external IP/port?

Is it possible to redirect connections to a specific IP/port to an external IP/port?

Example:

  • Server A has the external IP xxx.xxx.xxx.xxx
  • Server B has the external IP yyy.yyy.yyy.yyy
  • User sends a request to server A on port 2106, I want to redirect it to server B at port 2106.

Solution 1:

Problem solved:

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

Where ip and port are the target server I want to redirect the current server port to.