Archive for the ‘iptables’ Category

Redirecting network traffic to a new IP using iptables

March 17, 2008

Login to the old server as root and enter the following

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -p tcp -dport 80 -j DNAT –to-destination
iptables -t nat -A POSTROUTING -j MASQUERADE

Here the new ip address is Now all the traffic to port 80 will get redirected to port 80 of

(assuming that you do not have other iptables rules blocking access to port 80 )

If you want to redirect traffic to other ports such as for email POP, IMAP, or SSL , you can simply add in additional iptables rules.

For example to have POP3 redirection, you can add in an additional rule like this:

iptables -t nat -A PREROUTING -p tcp –dport 110 -j DNAT –to-destination

You may find this helpful while server migration