Behold this command:

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

I use that to reroute any incoming traffic from 80 to 8080, because the Apache 2 server on that computer runs as an unprivileged user and only root can open well known ports. It is saved in /etc/iptables/rules.v4 and it works, if I open the web site on a remote computer. The virtual host is bound to 192.168.X.X:8080.

However, if I ssh into that server and open a browser with X11Forwarding active, entering the IP into the address bar won't open the web site, but the connection will time out instead. Only if I add :8080 to the address it works. So bottom line, the port routing is not working when opened on the same computer.

My question is: How do I need to modify or add a routing directive, so this will work locally as well?


The reason this did not work is because packages from local sources do not got through the routing stage, since there is no routing needed for local packages. Therefore, this rule does not apply. To route packages coming from local source, one needs to send it directly to OUTPUT:

iptables -t nat -I OUTPUT -p tcp -o lo --dport 80 -j REDIRECT --to-ports 8080

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.