15

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.
2
  • 2
    Could you elaborate? Is machine "A" a multihomed gateway? If yes, iptables could probably be used. Example: iptables -A PREROUTING -t nat -i eth1 -p tcp --dport 2106 -j DNAT --to y.y.y.y:2106 iptables -A INPUT -p tcp -m state --state NEW --dport 2106 -i eth1 -j ACCEPT Mar 1, 2011 at 22:06
  • @bumbling fool thanks I had already solved it and posted below what I did.
    – Prix
    Mar 2, 2011 at 1:18

1 Answer 1

23

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.

2
  • 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.
    – singpolyma
    Feb 24, 2015 at 22:40
  • @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?
    – Jezor
    Jan 28, 2017 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .