SSH port forwarding, also called SSH tunneling, sends TCP traffic for another service through an encrypted SSH connection. Local forwarding with ssh -L brings a remote service to your machine, remote forwarding with ssh -R exposes a local service on the server, and dynamic forwarding with ssh -D creates a SOCKS proxy for many destinations.
Key takeaways
- Local forwarding (-L) brings a remote service to your machine; remote forwarding (-R) exposes a local service on the server.
- Dynamic forwarding (-D) turns SSH into a SOCKS proxy so apps can reach many destinations through one tunnel.
- By default tunnels bind to localhost only, and remote forwarding needs GatewayPorts set on the server to be reachable from outside.
You need to reach a database that only listens on a server, or show a colleague a website running on your laptop. Opening those services to the internet is risky. SSH port forwarding gives you a safer path through a connection you already trust.
What is SSH port forwarding?
SSH port forwarding, also called SSH tunneling, sends TCP traffic for another service through an encrypted SSH connection, so a service reachable from one machine becomes reachable from another without a VPN.
You already use SSH to log in to a server. Port forwarding reuses that same encrypted session as a carrier. Your application connects to a local port, SSH carries the bytes to the other end, and the other end delivers them to the real service.
There are three types, and the flag tells you where the listening port opens. The rule is simple: the listener always opens on the flag's named side. -L opens a port locally, -R opens one remotely, and -D opens a local SOCKS proxy. Our what is my IP tool can help you confirm which address a tunnel actually exits from.
Forwarding only works for TCP traffic. UDP services, such as most DNS and some game traffic, need a different tool. It also only encrypts traffic inside the tunnel: the last hop from the SSH server to the destination service is unencrypted unless that service itself uses TLS.
How does SSH port forwarding work?
When you start a forward, SSH opens a listening port, accepts connections to it, wraps each connection in the encrypted SSH session, and hands it to the destination host and port at the far end.
The destination in the middle of a forward command resolves from the far end, not from your laptop. In ssh -L 8080:localhost:80 user@server, the word localhost means port 80 on the server, not on your laptop. That single detail trips up almost everyone the first time.
Tunnels are also temporary by default. Close the terminal or lose the connection, and the tunnel dies. For tunnels that must survive, use autossh or define the forward in ~/.ssh/config, as shown in this hands-on SSH tunneling guide.
What is local port forwarding (-L)?
Local forwarding with -L creates a listening port on your machine and sends connections on it through the SSH server to a destination the server can reach.
The syntax is ssh -L [bind_address:]local_port:host:hostport user@server. The bind address is optional and defaults to loopback, so only your machine can use the tunnel.
The classic example is a database. PostgreSQL runs on port 5432 on the server, bound to localhost and invisible from the network. This command brings it to your laptop on port 5433:
ssh -f -N -L 5433:localhost:5432 user@server
Then point your database client at 127.0.0.1 on port 5433. The -f flag backgrounds SSH, and -N tells it not to run a remote command, which is what you want for a tunnel-only session.
The destination does not have to be the SSH server itself. It can be any machine the server can reach, which makes -L the standard way to reach services behind a bastion host. For example, ssh -f -N -L 8080:10.0.0.5:80 user@bastion reaches a web service at 10.0.0.5 through the bastion.
To let other machines on your LAN use a local tunnel, add 0.0.0.0 as the bind address, as in ssh -L 0.0.0.0:8080:localhost:80 user@server. Do this only when you mean to share the service, since it exposes the forwarded port to your whole network.
What is remote port forwarding (-R)?
Remote forwarding with -R is the mirror image of -L: it creates a listening port on the server, and connections to it tunnel back to a host and port your machine can reach.
The syntax is ssh -R [bind_address:]remote_port:host:hostport user@server. Use it when you are behind NAT or a firewall and want to expose a local service temporarily through a public server you can SSH into.
A common example is sharing a development server. Your app runs on your laptop at localhost:3000, and you want a colleague to preview it through your VPS:
ssh -f -N -R 8080:localhost:3000 [email protected]
Port 8080 on the VPS now forwards to your laptop. By default, only localhost on the VPS can reach it. To expose it publicly, set GatewayPorts yes in sshd_config on the VPS and add an explicit bind address, as in ssh -f -N -R 0.0.0.0:8080:localhost:3000 [email protected].
That public exposure is the main security risk of -R. Anyone who can reach the VPS port can reach your local service, so use a high, unguessable port, keep the tunnel short-lived, and put authentication on the service itself. Never forward a service with no login, such as a database, with -R.
What is dynamic port forwarding (-D)?
Dynamic forwarding with -D turns your SSH connection into a SOCKS proxy on your machine, so applications can reach many different destinations through one tunnel instead of one fixed destination per forward.
Start one with ssh -D 1080 user@server. Then point a browser or other SOCKS-aware app at localhost:1080. Each request tells the proxy where to connect, and the SSH server makes that connection on your behalf.
To test it, route curl through the proxy with curl --socks5-hostname localhost:1080 http://internal.example.com. The --socks5-hostname flag matters: it sends DNS through the proxy too, which avoids DNS leaks when you access internal hostnames.
Dynamic forwarding is handy for browsing internal web apps through a jump host, or calling APIs in a private network without a separate tunnel per service. It is not a full VPN: only apps you configure to use the proxy send traffic through it, and UDP traffic does not go through a SOCKS proxy.
OpenSSH 7.6 and later also supports reverse dynamic forwarding: ssh -R 1080 user@server with no destination creates a SOCKS proxy on the remote server instead of locally. That needs GatewayPorts yes to bind beyond loopback.
Local vs remote vs dynamic: which one should you use?
Choose by asking where the service lives and where you want to reach it from: use -L to pull a remote service to you, -R to push a local service to the remote side, and -D when you need many destinations through one proxy.
| Type | Flag | Listening port opens on | Best for | Main risk |
|---|---|---|---|---|
| Local | -L | Your machine | Reaching a remote database, dashboard or API | Sharing the port with 0.0.0.0 by mistake |
| Remote | -R | The SSH server | Exposing a local dev server or a home service | Public exposure if GatewayPorts is on |
| Dynamic | -D | Your machine, as a SOCKS proxy | Browsing many internal sites through one tunnel | Apps that ignore the proxy still leak traffic |
| Reverse dynamic | -R with no destination | The SSH server, as a SOCKS proxy | Letting the remote side reach your network | Same public-exposure risk as -R |
A useful memory aid: it is always the left-hand side that opens a new port. -L puts local on the left, so your machine listens. -R puts remote on the left, so the server listens.
If you only remember one rule, remember this: -L brings something to you, -R sends something away, and -D gives you a proxy for everything else.
What server settings control SSH port forwarding?
Two settings in /etc/ssh/sshd_config control forwarding: AllowTcpForwarding, which defaults to yes, and GatewayPorts, which defaults to no and controls whether remote forwards can bind beyond localhost.
Check the current values with sudo grep -i AllowTcpForwarding /etc/ssh/sshd_config and the same for GatewayPorts. If AllowTcpForwarding is set to no or local, remote forwarding will fail, often silently.
GatewayPorts has three values. no forces remote forwards to loopback only. yes forces them to bind to the wildcard address. clientspecified lets the client choose the bind address. The OpenBSD sshd_config manual page documents all three.
One trap: specifying 0.0.0.0 in -R when GatewayPorts is no produces no error. The server silently falls back to loopback, which is the most common cause of a tunnel that works locally but is not reachable from outside.
After editing sshd_config, reload the service with sudo systemctl reload sshd. Never set GatewayPorts yes on a shared server without thinking through who can reach the forwarded ports.
How do you keep an SSH tunnel running?
Use autossh to restart a dropped tunnel automatically, or store the forward in ~/.ssh/config so you can start it with a short alias instead of a long command.
A typical autossh command is autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -f -N -L 5432:localhost:5432 user@server. The -M 0 option disables the old monitoring port, and the keepalive options tell OpenSSH to restart the connection after three missed probes.
For a config-file version, add a host block to ~/.ssh/config:
Host db-tunnel
HostName server.example.com
User alice
LocalForward 5433 localhost:5432
Then start it with ssh -f -N db-tunnel. You can list several LocalForward or RemoteForward lines in one block, and add more forwards on the command line as well.
What security mistakes should you avoid?
The biggest mistakes are binding a tunnel to 0.0.0.0 without meaning to, forwarding a service with no authentication, and leaving a tunnel running long after you need it.
- Check the bind address. Default to localhost. Only use
0.0.0.0when you have a reason, and check who else is on that network. - Do not forward databases publicly. A database behind a tunnel is safe; the same database exposed through a remote forward with no password is not.
- Use key-based authentication. Disable password login on the SSH server where you can, and protect your private key with a passphrase.
- Close tunnels you no longer need. A forgotten tunnel is an open door. Check running tunnels with
ps aux | grep sshfrom time to time. - Limit forwarding per user. On a shared server, use
AllowTcpForwardingandPermitOpeninsshd_configto restrict which destinations a user can forward to. - Remember the last hop is not encrypted. SSH encrypts the tunnel, not the connection from the server to the destination. Use TLS on the service itself when the data is sensitive.
If you manage servers for a small team, put these rules in writing. Our small business cybersecurity checklist covers SSH hardening alongside backups, updates and access control.
Why is my SSH tunnel not working?
Work through the failure in order: the port, the server settings, the bind address, and the connection itself, because each one fails in a different way.
- Port already in use: another process is listening on your chosen port. Check with
ss -tlnp | grep 5433and pick a different local port. - Permission denied or silent failure: check
AllowTcpForwardingon the server. Hardened servers often set it tono. - Not reachable from outside: check
GatewayPorts. With the defaultno, remote forwards bind to loopback only. - Tunnel dies after a few minutes: add keepalives, as in
ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -f -N -L 8080:localhost:80 user@server. - Wrong localhost: remember that the destination host in
-Lresolves from the server, and in-Rit resolves from your machine. - DNS leaks with -D: use
--socks5-hostnamein curl, or the equivalent SOCKS remote-DNS option in your browser, so DNS goes through the proxy.
When in doubt, run the same command without -f -N in the foreground and read the error. SSH usually tells you exactly what went wrong, and a DNS lookup can confirm whether the destination hostname resolves from the right side.
The bottom line
SSH port forwarding is one of the most useful tools you already have installed. Three flags cover most needs: -L to reach a remote service, -R to share a local one, and -D to proxy many destinations at once.
Keep tunnels on localhost by default, use keys and keepalives, and close a tunnel when you are done. You can check the network side of your setup with the free what is my IP tool before you rely on a tunnel in production.
Frequently asked questions
What is SSH port forwarding?
SSH port forwarding sends traffic for another service through an encrypted SSH connection. It lets you reach a service that only the SSH server can see, or expose a local service through that server, without opening the service itself to the internet.
What is the difference between local and remote port forwarding?
Local forwarding with -L opens a listening port on your machine and sends its traffic to a destination the server can reach. Remote forwarding with -R opens a listening port on the server and sends its traffic back to a destination your machine can reach.
What does ssh -D do?
The -D flag creates a SOCKS proxy on your machine. Instead of wiring one port to one destination, it lets each application choose its own destination, and the SSH server makes the connection on its behalf.
Is SSH port forwarding safe?
The traffic inside the tunnel is encrypted by SSH, which is safer than exposing a service directly. The risk is scope: a tunnel bound to 0.0.0.0 or a remote forward with GatewayPorts yes can expose an internal service to anyone who can reach that port, so keep tunnels on localhost unless you need wider access.
Why is my SSH tunnel not working?
Check three things: AllowTcpForwarding in sshd_config, which defaults to yes but may be disabled on hardened servers; GatewayPorts, which must be yes or clientspecified for a remote forward to bind beyond localhost; and whether another process already uses your chosen port.
How do I stop an SSH tunnel running in the background?
Find the process with pgrep -f ssh and kill it, for example kill $(pgrep -f "ssh -f -N -L 5432"). If you used autossh, stop that process instead, since it will restart a killed ssh process.
Does SSH port forwarding work on Windows?
Yes. Windows 10 and 11 include the OpenSSH client, so the same ssh -L, -R and -D commands work in PowerShell or Command Prompt, and the same options work in PuTTY under its Tunnels settings.
What is the difference between SSH port forwarding and router port forwarding?
Router port forwarding changes NAT or firewall rules so incoming internet traffic reaches a device on a network. SSH port forwarding happens entirely inside an existing encrypted SSH connection and needs no router changes.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against the OpenBSD ssh and sshd_config manual pages and a hands-on SSH tunneling guide.
How we write and fact-check →