An SSH connection is usually used for a shell, but the same encrypted link can carry other traffic: a database port that only listens on a server’s localhost, a web app on a laptop that someone outside needs to see, or an entire browser session routed through a remote machine. The three flags that do this, -L, -R and -D, are easy to mix up, and each has a default that quietly limits who can use the tunnel. Each was tested below with OpenSSH 9.6 on a small network built from Linux network namespaces: a PC sitting behind two layers of NAT, and an internet server running SSH.
The test network
Who could reach what
- The server, at 203.0.113.10, ran SSH and a small “database” service listening only on its own
127.0.0.1:5432. - The PC, at 192.168.1.10 behind a home router and a provider-level NAT, ran a web app on its own
127.0.0.1:8080. - Two other machines stood in for neighbors: the home router on the PC’s LAN, and a provider host next to the server.
Without any tunnel, the PC could not reach the database (it listened only on the server’s loopback address), and nothing on the internet side could reach the PC at all.

-L: bring a remote port to the local machine
The command
ssh -f -N -L 15432:127.0.0.1:5432 root@203.0.113.10
-N means “no remote command,” -f sends SSH to the background, and -L 15432:127.0.0.1:5432 reads as: listen on local port 15432, and send whatever arrives through the tunnel to 127.0.0.1:5432 as seen from the server.
The result
$ curl http://127.0.0.1:15432/
inet-only database answered; client seen as 127.0.0.1
The service believed it was talking to a local client, because from its point of view, the connection came from SSH on the same machine. This is the standard way to reach a database, admin panel or monitoring page that is deliberately bound to localhost on a server: nothing new is exposed to the network, and the traffic crosses it encrypted.
Who else can use it
ss -ltn on the PC showed the tunnel listening on 127.0.0.1:15432 only. The ssh manual for Ubuntu 24.04 explains that the local port is bound according to the GatewayPorts setting, with an explicit bind address available to override it. When the home router tried the PC’s LAN address on that port, the connection was refused. Starting the tunnel as -L 0.0.0.0:25432:127.0.0.1:5432 made it listen on all interfaces, and the router got the database’s answer. That is useful for sharing a tunnel with a LAN, and risky on a laptop sitting on public Wi-Fi.
-R: publish a local port on the remote machine
The command
ssh -f -N -R 9000:127.0.0.1:8080 root@203.0.113.10
This reverses the direction: the server listens on port 9000, and connections to it come back through the tunnel to the PC’s 127.0.0.1:8080. Because the PC opened the SSH connection outward, it worked even through both NAT layers.
The default bind
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
From the server itself, curl http://127.0.0.1:9000/ returned the PC’s web app. From the neighboring provider host, the same port on the server’s public address was refused. The manual says remote listening sockets bind to the loopback interface by default, and that a remote bind address other than loopback only succeeds if the server’s GatewayPorts option is enabled.
Opening it up
With the server’s sshd running GatewayPorts no, asking for -R 0.0.0.0:9001:... did not fail; the tunnel came up and silently bound to 127.0.0.1:9001 anyway. After restarting sshd with GatewayPorts clientspecified, the same request bound to 0.0.0.0:9001, and the neighboring host reached the PC’s web app through the server. The clientspecified setting leaves the decision to each client, which is safer than yes, where every remote forward becomes public.

-D: a SOCKS proxy through the server
The command
ssh -f -N -D 1080 root@203.0.113.10
The manual describes -D as dynamic, application-level forwarding, with SSH acting as a SOCKS4 or SOCKS5 server. Instead of one fixed destination, any program that supports SOCKS can ask the tunnel to connect anywhere the server can reach.
The trap the test hit first
The first attempt to use the proxy came back with nothing at all:
curl -x socks5h://127.0.0.1:1080 http://127.0.0.1:5432/
The machine’s environment had NO_PROXY set to include localhost and 127.0.0.1, a common setting on corporate laptops and developer tools, and curl obeyed it: it ignored the proxy for those addresses and tried the PC’s own port 5432, where nothing was listening. With the variable cleared and --noproxy '' added, the same command returned:
inet-only database answered; client seen as 127.0.0.1
Where names and addresses are resolved
With the socks5h scheme, the destination is resolved and connected on the server. That is why 127.0.0.1:5432 reached the server’s database, while 127.0.0.1:8080, the PC’s own web app, failed through the proxy: on the server, nothing listened there. The h matters for DNS too. socks5h sends hostnames to the server for lookup, so DNS queries travel through the tunnel; plain socks5 resolves names locally first, which can leak what is being visited to the local network’s DNS server.
What -D does not do
A SOCKS tunnel is not a VPN. Only programs explicitly pointed at the proxy use it: a browser with its proxy set to 127.0.0.1:1080, curl with -x, or a tool that honors an ALL_PROXY variable. Everything else on the machine, from system updates to other browsers, keeps using the normal connection. That makes -D handy for reaching a remote network’s internal web pages, or for checking how a site looks from another location, without rerouting a whole laptop. The NO_PROXY test above also shows the reverse risk: a program can quietly decide to skip the proxy for some addresses, so anything sensitive deserves a check of where its traffic really went.
Keeping tunnels running and closing them
Background tunnels
Each -f -N tunnel left an ssh process behind:
ssh -f -N -L 15432:127.0.0.1:5432 root@203.0.113.10
ssh -f -N -R 9000:127.0.0.1:8080 root@203.0.113.10
ssh -f -N -D 1080 root@203.0.113.10
pgrep -af "ssh .*-N" lists them, and killing the process closes the tunnel. ExitOnForwardFailure=yes, used in every test command, makes SSH exit instead of running without its tunnel when a port is already taken, which saves a confusing debugging session.
For long-running tunnels
Adding ServerAliveInterval 60 lets SSH notice a dead connection. A systemd service with Restart=on-failure around the ssh command, or the autossh tool, brings the tunnel back after a drop. Defining the forward in ~/.ssh/config with LocalForward, RemoteForward or DynamicForward turns a long command into ssh dbtunnel.

Controlling tunnels on the server
Turning forwarding off
Everything above worked because the server’s sshd allowed it, which is the default. The sshd_config manual for Ubuntu 24.04 lists AllowTcpForwarding with the values yes (the default), no, local and remote, and says that by default all forwarding requests are permitted. With the server restarted under AllowTcpForwarding no, a remote forward failed immediately on the client:
Error: remote port forwarding failed for listen port 9000
A local forward still opened its listening port on the PC, which is a confusing detail: the failure only appeared when something connected, as channel 1: open failed: administratively prohibited: open failed, and the connection was dropped.
Allowing only specific destinations
PermitOpen narrows local forwards to named destinations. With PermitOpen 127.0.0.1:5432, the tunnel to the database worked exactly as before, while a second tunnel aimed at the server’s own SSH port, 127.0.0.1:22, returned nothing. On a shared bastion host, that combination is the difference between “users can reach the database” and “users can reach anything the bastion can.”
The same manual describes GatewayPorts as controlling whether remote hosts may connect to ports forwarded for the client, with no as the default, yes forcing the wildcard address, and clientspecified letting each client choose, matching what the remote forward tests showed.
Which one to use
Quick reference
| Goal | Flag | Example |
|---|---|---|
| Reach a service that only the server can see | -L |
-L 15432:127.0.0.1:5432 |
| Let the server (or others) reach a service on this machine | -R |
-R 9000:127.0.0.1:8080 |
| Route a browser or tool through the server | -D |
-D 1080, then a SOCKS5 proxy setting |
Safety defaults worth keeping
All three tests showed the same pattern: by default, a tunnel is reachable only from the machine where it listens. 0.0.0.0 on the client, or GatewayPorts on the server, widens that to everyone on the network. That is sometimes exactly the goal, as with a remote forward that publishes a home service through a cloud server. It is also how a database that was carefully bound to localhost ends up open to a coffee shop’s Wi-Fi.
A tunnel goes exactly where the command line says it goes. The trouble is almost always in the half of the line people read least carefully: the address before the first colon.
Sources
- ssh(1), Ubuntu 24.04 manual page: https://manpages.ubuntu.com/manpages/noble/man1/ssh.1.html
- sshd_config(5), Ubuntu 24.04 manual page: https://manpages.ubuntu.com/manpages/noble/man5/sshd_config.5.html
