14

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
    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 – bumbling fool Mar 1 '11 at 22:06
  • @bumbling fool thanks I had already solved it and posted below what I did. – Prix Mar 2 '11 at 1:18
22

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.

  • 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 '15 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 '17 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.