Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.
share|improve this question
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

1 Answer 1

up vote 16 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

Your Answer

 
discard

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.