Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


  • Server A has the external IP
  • 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.
share|improve this question
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
up vote 19 down vote accepted

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|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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