Linux : Port forwarding with IPtables

By | September 30, 2016

Need to forward a custom port to an existing port on your Linux box?

Simply use the following command as root :

iptables -t nat -I PREROUTING --src 0/0 --dst 0.0.0.0/0 -p tcp --dport <port-to-forward> -j REDIRECT --to-ports <destination-port>

Example : Assuming you want to forward port 1025 to port 587 :

iptables -t nat -I PREROUTING --src 0/0 --dst 0.0.0.0/0 -p tcp --dport 1025 -j REDIRECT --to-ports 587

The command above won’t survive to reboot, see the configuration below to enter in the IPTables script :

-A FORWARD -d  0.0.0.0/0 -p tcp -m tcp --dport 1025 -m comment --comment "Forward inbound connections from tcp1025 to tcp587" -j ACCEPT