How does SSH port forwarding work? Examples of -L, -R and -D

ssh -L 9000:127.0.0.1:8080 app opens port 9000 on your machine and sends every connection to port 8080 on the server app. -R does the reverse, and -D turns the connection into a SOCKS proxy.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

Use -L to reach a service on the server from your machine, and -R to let the server reach a service on your machine. ssh -L 9000:127.0.0.1:8080 app listens on port 9000 here and sends each connection to 127.0.0.1:8080 as the server app sees it. Everything runs inside the encrypted SSH connection, so nothing else has to be opened in a firewall.

The setup used here

The server app runs a small web service on port 8080. It listens on its loopback address only, as a database or an admin panel often does. Your machine runs a second small service on port 8081. Both pages are one line of text so that you can see which one answers. First, the page as the server sees it:

$ ssh app 'curl -s http://127.0.0.1:8080/'
Hello from the app server

From your machine the same port is closed. The server does not listen on its public address, so the connection fails:

$ curl -s --max-time 3 http://app.example.test:8080/ || echo "no connection"
no connection

Local forwarding: -L

The form is -L local_port:target_host:target_port. The target is resolved on the server, so 127.0.0.1 means the server itself. Three more options keep the command tidy. -N runs no remote command, -f goes to the background once the login is done, and -o ExitOnForwardFailure=yes makes ssh quit if it cannot open the port, which is better than a tunnel that looks fine and does nothing. -M -S /tmp/web-tunnel gives the connection a control socket, which you will use to close it.

$ ssh -f -N -M -S /tmp/web-tunnel -L 9000:127.0.0.1:8080 -o ExitOnForwardFailure=yes app
$ curl -s http://127.0.0.1:9000/
Hello from the app server

Port 9000 on your machine now answers with the page from the server. A database client works the same way. Forward 5432 to the database port and connect the client to 127.0.0.1. This is also how you reach a service that sits behind a bastion: the jump from the jump host article applies to the tunnel too.

Check who listens on the port:

$ ss -ltnH 'sport = :9000' | awk '{print $4}'
127.0.0.1:9000
[::1]:9000

The address is 127.0.0.1 and [::1], so only programs on your own machine can use the tunnel. That is the default and the safe one.

Close it

A tunnel started with -f has no terminal to close. Ask the control master to exit, and the port is free again:

$ ssh -S /tmp/web-tunnel -O exit app
Exit request sent.
$ curl -s --max-time 3 http://127.0.0.1:9000/ || echo "no connection"
no connection

Without a control socket you would have to find the process with ps and kill it. Keeping one is easier.

Remote forwarding: -R

-R remote_port:target_host:target_port is the mirror. The server listens on remote_port and sends each connection back to a service that your machine can reach. It is the way to show a local web server to a colleague who has access to the server, or to let a server call a service on your laptop.

$ ssh -f -N -M -S /tmp/back-tunnel -R 9100:127.0.0.1:8081 -o ExitOnForwardFailure=yes app
$ ssh app 'curl -s http://127.0.0.1:9100/'
Hello from the laptop
$ ssh -S /tmp/back-tunnel -O exit app
Exit request sent.

The page came from your machine, not from the server. By default the port on the server is open to the server’s own loopback only. Letting other machines connect to it needs GatewayPorts in the server’s sshd_config, which is off by default and which you should leave off unless you know who can reach that machine.

Dynamic forwarding: -D

-D 1080 opens a SOCKS proxy on port 1080. A program that speaks SOCKS decides the target for each connection, and the connection leaves from the server. --socks5-hostname makes curl send the name to the proxy to resolve, so 127.0.0.1 below means the server again:

$ ssh -f -N -M -S /tmp/socks-tunnel -D 1080 app
$ curl -s --socks5-hostname 127.0.0.1:1080 http://127.0.0.1:8080/
Hello from the app server
$ ssh -S /tmp/socks-tunnel -O exit app
Exit request sent.

Mistakes people make

You open the tunnel to everyone. Writing -L 0.0.0.0:9001:127.0.0.1:8080 binds the port on every interface of your machine. Anyone on your network can then reach the service on the server through your login:

$ ssh -f -N -M -S /tmp/open-tunnel -L 0.0.0.0:9001:127.0.0.1:8080 app
$ ss -ltnH 'sport = :9001' | awk '{print $4}'
0.0.0.0:9001
$ ssh -S /tmp/open-tunnel -O exit app
Exit request sent.

The address 0.0.0.0 in the output means all interfaces. Use it only on a network you trust, and close the tunnel when you are done.

The port is taken. If something on your machine already listens on 9000, ssh says “Address already in use”. With ExitOnForwardFailure it then stops. Choose another local port.

You forget it is open. A tunnel started with -f keeps running after the terminal closes. Look for them with ss -ltn and close them with -O exit.

The target is localhost. The name is resolved on the server, and it may point to the IPv6 address while the service listens on IPv4 only. Write 127.0.0.1 when you mean it.

The tunnel dies with the connection. If the link drops, the tunnel goes with it. Keep-alive settings make a dead tunnel show up sooner, and a block in your config file can hold LocalForward 9000 127.0.0.1:8080, so that ssh -N app opens it without the long command.

Questions

How do I open a remote database on my own machine?

Forward a local port to the database port on the server, for example ssh -N -L 5432:127.0.0.1:5432 app, and point your client at 127.0.0.1 port 5432. The database can keep listening on the server's loopback address only.

Why does my tunnel stop after a while?

An idle connection can be dropped by a router in between. Set ServerAliveInterval for the host and the tunnel carries traffic often enough to stay open.

Can I forward a port below 1024?

Only if you are allowed to listen on it. On Linux and macOS that needs root for the local side, so choose a higher local port and keep the remote one.

Does the server have to allow it?

Yes. sshd has AllowTcpForwarding, which is on by default. Some servers turn it off for an account, and then -L and -R fail even though the login works.

References

The app is free.

Get notified at launch

macOS is coming to the Mac App Store. iOS and Android later.