Zaher Ghaibeh
PHP Backend developer
I've experience in a few PHP Frameworks, such as Laravel, Lumen and Slim (The last two are used for building Microservices/API services).
Redirecting to another port using iptables
Published at Thursday, March 31, 2016 , Categorized under: Linux, Other

One of the things that I hate about nodejs is that I will have to run it as root if I want to use the port 80, but yesterday while I was reviewing a book called Mastering Web Application Development with [Express]( and one of the solutions which he talked about so that you dont need to use the root user is to redirect the ports using iptable , so this is what you have to do :

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

remember to change eth0 to the one you are connecting to, in my vagrant it was eth1

And since iptable does not save anything as permanent, you have to do small work (read more about it here)

# Install the service
$ sudo apt-get install -y iptables-persistent

# Start the service
$ sudo service iptables-persistent start

# Save the rules
$ sudo iptables-save | sudo tee /etc/iptables/rules.v4

# Restart the service
$ sudo service iptables-persistent restart

And we are done, no need to use the root account any more ;)


PS: This article will work with ubuntu 16.04 , because service does not work with ubuntu 16.04 as I know, but not 100% sure.