Port forwarding using IPTables

This is how I setup a port forward on a Ubuntu host.
Let us say there is an application with a web interface that runs HTTP but on an odd port, like 8080. We want to accept HTTP traffic for that application on the normal port 80 instead of 8080 but the application can’t be reconfigured to use 80.

There are many ways to make this happen. This way uses an IPTables rule to forward the port and we will make it persistent across reboots.

On Ubuntu, this is very easy.
First thing is to check for any current IPTables rules that might cause issues.

iptables -L -t nat

Any rules would be listed after running that command.

To create the forwarding rule, use the following example:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080 

Once run now you can check the IPTables list again and you should see you new rule.

To make the rule persistent, I like using the ubuntu package iptables-persistence. Its quick and easy:

apt-get install iptables-persistence

Then answer the questions that follow.

That’s it and you are done.

Tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
1 × 27 =