Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I want to redirect port 80 on the loopback network interface so that accesses to that port actually go to port 8080.

I tried using the following command, but it only works if the application is listening on, but not if it's listening on

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

I also tried adding --source --destination but it still doesn't work.

The redirection works if I open an application listening on

ssh remote -L127.0.0.1:8080:

nc 80 # works

But it doesn't work if I open an application listening on

ssh remote -L127.1.2.3:8080:

nc 80 # fails
nc: connect to port 80 (tcp) failed: Connection refused

nc 8080 # works, so the application is still listening on 8080

How can I redirect the port 8080 on 127.x.x.x to the port 80 on the same IP?

Update: Motivation

I'm trying to use SSH to do the following port forwardings ( →, and →

ssh remote -L127.1.2.3:80: -L127.1.2.4:80:

Since port 80 is "privileged", ssh can't open it on localhost. There are several ways to circumvent that:

  • sudo ssh, but I don't want to have to copy my ssh config and keys on the root account.
  • Use capabilities, but package updates may break this.
  • Use a different port (e.g. 8080), that works but non-relative urls get broken.
  • Reroute the privileged port to a non-privileged one, and that's what I'm trying to do. It works for a single redirection on, but it fails if I have redirections on IPs other than (see above).
share|improve this question
Why are you using IP & the loopback? I don't believe these addresses are usable in this fashion.… –  slm May 13 '14 at 14:09
@slm Because I want to forward several applications that run on port 80 on different hosts ( maps to (on the ssh remote side), and maps to (on the ssh remote side)). All 127.x.x.x addresses are reserved for loopback: "Various Internet Engineering Task Force (IETF) standards reserve the IPv4 address block [...] The most common IPv4 address used is" –  Georges Dupéron May 13 '14 at 14:15
You'll need to explain what exactly it is you're doing. This sounds like an XY Problem –  slm May 13 '14 at 14:18
@slm I added my motivation for this question, but that's not an XY Problem, since I'm still interested in knowing how to redirect ports on the loopback interface for IPs other than, even if I find a different solution for my original problem. –  Georges Dupéron May 13 '14 at 14:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.