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

53,720

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.

Share:
53,720
user1163005
Author by

user1163005

Looking forward to learn more and more :)

Updated on September 17, 2022

Comments

  • user1163005
    user1163005 almost 2 years

    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.
  • singpolyma
    singpolyma over 9 years
    This does not work for me. Specifically, that last line causes all traffic from my machine to itself to appear to come from outside, and similar problems.
  • Jezor
    Jezor over 7 years
    @Prix wouldn't that redirect all traffic to a specified port (2106) to yyy.yyy.yyy.yyy:port? What if I'd like to redirect traffic that goes only to xxx.xxx.xxx.xxx:port?