The Three Kinds
| Option | Opens a port on | Typical use |
|---|---|---|
-L | Your computer | Reach a private service on the server |
-R | The server | Show a local service to the server |
-D | Your computer | SOCKS proxy through the server |
Local Forwarding with -L
A database on the server listens only on 127.0.0.1:3306, so it cannot be reached from the internet. To use it from your computer:
ssh -L 3307:127.0.0.1:3306 user@your_server_ip
The pattern is local_port:destination:destination_port. The destination is resolved on the server, so 127.0.0.1 means the server itself. While the session is open, connect your client to port 3307 on your own machine:
mysql -h 127.0.0.1 -P 3307 -u app_user -p
This keeps the database closed to the world while you work with it. What 127.0.0.1 means is explained in what is localhost.
Remote Forwarding with -R
The reverse: an application runs on port 3000 on your computer, and you want to reach it from the server:
ssh -R 8080:127.0.0.1:3000 user@your_server_ip
On the server, curl http://127.0.0.1:8080 now reaches your local application. By default the server binds that port to its loopback address only. Making it reachable from outside requires GatewayPorts in the server's sshd_config; leave it off unless you need it.
Dynamic Forwarding with -D
ssh -D 1080 user@your_server_ip
This opens a SOCKS proxy on port 1080 of your computer. Set a browser or tool to use SOCKS5 at 127.0.0.1, port 1080, and its traffic leaves through the server. It helps to test how a site looks from the server's network, or to reach internal addresses the server can see.
Tunnels Without a Shell: -N and -f
ssh -N -f -L 3307:127.0.0.1:3306 user@your_server_ip
-N: do not run a command or open a shell; only forward.-f: go to the background after login.
With -f, also add -o ExitOnForwardFailure=yes: if the port cannot be opened, ssh then exits with an error instead of running in the background without the tunnel. To close a background tunnel, find it with pgrep -af "ssh -N" and stop it with kill followed by its process ID. A tunnel you use daily can live in the SSH config file as a LocalForward line.
Security: Mind the Bind Address
A forwarded port is as exposed as the address it is bound to. By default, -L and -D bind to your computer's loopback address, so only you can use them. If you write -L 0.0.0.0:3307:127.0.0.1:3306, anyone who can reach your computer on port 3307 reaches the database. The same applies to -R on the server when GatewayPorts is on. Keep tunnels on 127.0.0.1 unless you have a reason and a firewall rule for anything else.
Server-Side Settings
Changing sshd_config can lock you out. Keep your current session open, make sure a non-root sudo user can log in with a key in a second session, and keep your provider's console at hand as the way back.
AllowTcpForwarding: on by default in OpenSSH. Set it tonofor accounts that should never tunnel, for example in aMatch Userblock for an upload-only user.GatewayPorts: off by default; controls whether remote forwards can listen on public addresses.
After a change, test the files and check the effective values:
sudo sshd -t
sudo sshd -T | grep -Ei 'allowtcpforwarding|gatewayports'
Then reload the service, not restart it, as shown in how to restart the SSH service, and test a new login from a second terminal before you close the first.
When a Tunnel Fails
"Address already in use" means the local port is taken; pick another or find the process with lsof. A warning that remote port forwarding failed, or a channel that fails with "administratively prohibited", points to the server's settings above or, for -R, to a port already taken on the server. A channel that fails with "Connection refused" means the destination does not answer on that port.