An SSH tunnel is an encrypted connection created by the SSH protocol that carries other network traffic inside it. Using local, remote or dynamic port forwarding, it lets you reach a service you could not connect to directly — such as a database behind a firewall — by sending that traffic through a server you already trust, encrypted end to end between your computer and that server.
Key takeaways
- An SSH tunnel wraps another service's traffic inside an encrypted SSH session, so it can cross untrusted networks safely.
- Local forwarding (-L) reaches remote services, remote forwarding (-R) exposes local services, and dynamic forwarding (-D) creates a SOCKS proxy.
- Tunnels protect traffic only up to the SSH server — the hop from the server to the destination is usually unencrypted.
You already trust SSH to log in to your servers. What many people miss is that the same connection can carry other traffic too — a database query, an admin panel, a web browser — wrapped in the same encryption, to places you could never reach directly.
What is an SSH tunnel?
An SSH tunnel is an encrypted connection created by the Secure Shell (SSH) protocol that carries other network traffic inside it, letting you reach a service you could not connect to directly.
SSH was built to give you a secure remote shell on another machine. But the protocol can carry more than terminal keystrokes over one connection. Tunneling, also called port forwarding, takes a port on one machine and maps it across the encrypted link to a host and port reachable from the other side.
RFC 4251 describes the connection protocol as multiplexing the encrypted tunnel into several logical channels, with standard methods for forwarding arbitrary TCP/IP ports.
The application at either end never knows it is talking through a tunnel. It just sees an ordinary open socket — your database client connects to localhost, and SSH does the rest.
How does an SSH tunnel work?
When you connect to a server with SSH, your client creates an encrypted session with that server, and any data inside the session can be of any type — not just terminal traffic.
Here is the flow for a forwarded connection. First, your SSH client listens on a local port. When an application connects to that port, the client wraps the bytes, sends them through the encrypted SSH channel to the SSH server, and the server opens a plain connection onward to the real destination and relays the response back.
That last detail matters for security: the tunnel protects traffic between your computer and the SSH server. The hop from the server to the destination is usually ordinary, unencrypted traffic on a private network — fine on a trusted LAN, but not a guarantee of end-to-end encryption.
What are the three types of SSH tunnel?
OpenSSH offers three kinds of forwarding, selected with a single flag: -L for local, -R for remote, and -D for dynamic. Which one you pick depends on which side starts the connection and which side can reach the destination, as the ssh(1) manual page sets out.
| Type | Flag | Listener opens on | Typical use |
|---|---|---|---|
| Local forwarding | -L | Your computer | Reach a database or admin panel behind a firewall |
| Remote forwarding | -R | The SSH server | Let the server reach a service on your computer |
| Dynamic forwarding | -D | Your computer | SOCKS proxy for browser or tool traffic |
How does local port forwarding work?
Local forwarding reaches a service you cannot connect to directly, usually because it sits behind a firewall, by opening a port on your own computer and sending its traffic through the SSH server.
The syntax is ssh -L local_port:destination_host:destination_port user@ssh_server. The destination is resolved from the SSH server, so localhost in the destination means the server itself.
A common example is a database that only listens on the server. This command makes it appear on your own machine:
ssh -N -L 5433:localhost:5432 [email protected]
Now your database client connects to localhost:5433, and SSH carries the traffic to port 5432 on the server. The database never has to be exposed to the internet. Add -N when you only want forwarding and no shell, and -o ExitOnForwardFailure=yes so a failed forward stops the command instead of silently running without the tunnel.
How does remote port forwarding work?
Remote forwarding is the mirror image: it opens a port on the SSH server and forwards connections back through SSH to a service on your computer.
The syntax is ssh -R remote_port:local_host:local_port user@ssh_server. A typical use is showing a local development site to a colleague, or letting a remote server call a webhook on your machine:
ssh -N -R 8081:localhost:3000 [email protected]
Anyone who can reach port 8081 on the server now reaches your local app on port 3000. By default the remotely forwarded port binds only to loopback on the server, so it can be reached from the server itself rather than the wider network — which is usually what you want.
Be careful with remote forwarding: you are exposing a local service to a remote machine. Only forward services you are happy for that server to reach, and never forward something sensitive without authentication of its own.
How does dynamic port forwarding work?
Dynamic forwarding turns your SSH connection into a SOCKS proxy, so applications can route arbitrary traffic through the SSH server without a fixed destination.
Start one with ssh -N -D 1080 [email protected]. SSH now exposes a SOCKS5 proxy on localhost:1080. Configure your browser or tool to use that proxy, and every connection it makes goes out from the server, with the server's IP address.
Unlike -L, you do not specify one destination when creating the tunnel. The application decides where it wants to connect, and the SSH server makes the outbound connection on its behalf. That makes dynamic forwarding handy for secure browsing on public Wi-Fi or for reaching several internal services at once.
Note that a SOCKS proxy is not a VPN: only applications configured to use the proxy send traffic through it, and DNS lookups may still leak unless the application is set to resolve names through the proxy.
When should you use an SSH tunnel?
SSH tunnels shine whenever you already have SSH access and need to reach one specific thing securely, without setting up a VPN or opening a firewall port.
- Databases: connect to PostgreSQL, MySQL or MongoDB that only listens on localhost on the server.
- Admin panels: reach a web dashboard, monitoring page or internal tool that is not exposed publicly.
- Development: test a local site from a remote server, or debug a webhook locally.
- Public Wi-Fi: route browser traffic through your own server with dynamic forwarding.
- Jump hosts: reach servers with no public SSH by hopping through one that has it, using
-J.
For a wider look at protecting the network around those services, see our guide to how to secure your Wi-Fi.
SSH tunnel vs VPN: what's the difference?
An SSH tunnel and a VPN both encrypt traffic, but they work at different scales and solve different problems.
| SSH tunnel | VPN | |
|---|---|---|
| Scope | Specific ports or applications you choose | All traffic from the device (or a whole network) |
| Setup | One command, no extra software | VPN client and server configuration |
| Encryption | Up to the SSH server only | Up to the VPN gateway, for all routed traffic |
| Best for | Reaching one service quickly | Protecting everything on an untrusted network |
| Authentication | SSH keys or passwords | Certificates, keys or credentials |
If you need to reach one database a few times a week, a tunnel is the simpler tool. If you need every app on your laptop protected on hotel Wi-Fi, use a VPN. Many engineers use both.
You can confirm what the far end of a connection sees with our free what is my IP tool, which shows the address a site observes — useful when checking a dynamic-forwarding proxy is actually in use.
Is SSH tunneling secure? Risks and best practices
Tunneling is secure when used correctly, but it has sharp edges. The tunnel inherits the security of your SSH setup — and it can also bypass the firewall rules that were protecting a service.
- Use key authentication. Disable password login on the server. A tunnel is only as strong as the SSH login that creates it.
- Bind forwards to localhost. Do not add a bind address that exposes the tunnel to your whole LAN or the internet unless you mean to.
- Limit what can be forwarded. On the server,
AllowTcpForwardingandPermitOpencontrol which destinations an account may reach. - Remember the last hop is plain. If the destination service itself does not use TLS, the final leg of the journey is unencrypted.
- Close tunnels when you finish. A forgotten tunnel left running on a shared machine is an open door to the private service behind it.
Why is my SSH tunnel not working?
Most tunnel failures come from a small set of causes. Work through them in order.
- Port already in use: if the local port is taken, SSH fails or silently skips the forward. Pick another port, or add
-o ExitOnForwardFailure=yesso failures are loud. - Wrong destination host: the destination is resolved from the server, not from your machine. A name that resolves locally may not resolve on the server.
- Service not listening: check on the server that the service is actually running and listening on the expected port.
- Forwarding disabled: the server may have forwarding turned off (
AllowTcpForwarding noin its configuration). Hardened servers often disable it. - Firewall on the server: the server itself may block the outbound connection to the destination.
Run SSH with -v for verbose output when debugging: it shows each forwarding request and whether the server accepted it.
The bottom line
An SSH tunnel is the simplest way to reach a private service securely: one command, no extra software, and encryption from your computer to a server you trust. Use -L to reach things, -R to expose things, and -D when you need a quick proxy.
Just remember its limits: the tunnel ends at the SSH server, it only carries what you forward, and it bypasses firewalls by design — so guard the keys that create it. If you want to check how your connection appears from the outside, our free DNS lookup tool can verify the records behind the hostnames you tunnel to.
Frequently asked questions
What is an SSH tunnel in simple terms?
It is a secure, encrypted passage inside an SSH connection that carries traffic for another service, such as a database or website, so that traffic can reach a destination it could not reach directly and cannot be read on the way.
What is the difference between local and remote port forwarding?
Local forwarding opens a port on your computer and sends traffic through the SSH server to a destination. Remote forwarding opens a port on the SSH server and sends traffic back to a service on your computer.
What is dynamic port forwarding?
Dynamic forwarding turns your SSH connection into a SOCKS proxy, usually on localhost port 1080. Applications that support SOCKS can send any traffic through it, and the SSH server makes the outbound connection for each destination.
Is an SSH tunnel the same as a VPN?
No. A VPN routes all of your device's traffic through an encrypted tunnel. An SSH tunnel carries only the traffic you explicitly forward, for specific ports or applications. A tunnel is lighter and more targeted; a VPN is broader.
Is SSH tunneling secure?
Traffic between your computer and the SSH server is encrypted and authenticated. But the hop from the SSH server to the final destination is usually plain, unencrypted traffic, so the tunnel does not protect data beyond the server.
What port does SSH use?
SSH uses TCP port 22 by default. A tunnel still uses that same SSH connection, so no extra ports need to be opened on the firewall for the forwarded traffic itself.
Can I use an SSH tunnel on Windows?
Yes. Windows 10 and 11 offer the OpenSSH Client as an optional Windows feature, which you install from Settings or with Add-WindowsCapability in PowerShell. Once installed, the same ssh -L, -R and -D commands work in PowerShell or Command Prompt. PuTTY also supports tunneling through its GUI.
Why would I use an SSH tunnel instead of opening a firewall port?
Opening a port exposes a service to the whole internet. A tunnel keeps the service listening only on localhost and lets only authenticated SSH users reach it, which shrinks the attack surface considerably.
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 OpenSSH ssh(1) manual, RFC 4251, RFC 4253 and Microsoft Learn's OpenSSH documentation.
How we write and fact-check →