About Me

My photo
Working as Technical Lead in CollabNET software private limited.

Thursday 19 August, 2010

How to redirect ports inside same machine using IPTABLES

As my mail server is running on port 8056, I got ended up searching to find a way to redirect all the mails which comes to port25 to port8056.

Below is one way of doing which helped me using iptables entry.


sudo iptables -t nat -A OUTPUT -p tcp -d 127.0.0.0/8 --dport 25 -j REDIRECT --to-port 8056
sudo iptables -t nat -A OUTPUT -p tcp -d x.x.x.x --dport 25 -j REDIRECT --to-port 8056
sudo iptables -t nat -A PREROUTING -p tcp --dport 25 -j REDIRECT --to-ports 8056


In the above commands

command 1 is to, forward port25 requests to port 8056 which are initiated via localhost. (i.e) telnet localhost 25.
command 2 is to, forward port25 requests to port 8056 which are initiated via IP address. (i.e) telnet x.x.x.x 25.
command 3 is to, forward the other network m/c requests to port 8056 through port25.