Network security

What Is an SSH Tunnel? Local, Remote and Dynamic Forwarding Explained

An SSH tunnel turns a connection you already trust into a secure passage for databases, admin panels and browsing. Here is how each type works, with commands you can copy.

Cysoro Security TeamPublished · 8 min read
-L-R-Dssh -N -L5433:localhost:5432 $ ssh user@servertunnel established NETWORK SECURITY SSHtunnel One connection, many safe paths.
Quick answer

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.

How an SSH tunnel carries trafficYour computerdatabase client onlocalhost:5433SSH serverport 22, encryptedsessionPrivate servicedatabase onlocalhost:5432encryptedplain LANThe encrypted part runs from your computer to the SSH server.The last hop, server to service, is usually unencrypted.
An SSH tunnel encrypts traffic only up to the SSH server. Illustration by Cysoro.

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.

TypeFlagListener opens onTypical use
Local forwarding-LYour computerReach a database or admin panel behind a firewall
Remote forwarding-RThe SSH serverLet the server reach a service on your computer
Dynamic forwarding-DYour computerSOCKS 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 tunnelVPN
ScopeSpecific ports or applications you chooseAll traffic from the device (or a whole network)
SetupOne command, no extra softwareVPN client and server configuration
EncryptionUp to the SSH server onlyUp to the VPN gateway, for all routed traffic
Best forReaching one service quicklyProtecting everything on an untrusted network
AuthenticationSSH keys or passwordsCertificates, 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.

SSH tunnel safety checklistUse SSH keys, not passwordsdisable password login on the serverBind forwards to localhostnever expose a tunnel to the whole networkLimit forwarding per accountuse PermitOpen and AllowTcpForwardingRemember the last hop is plaindo not send secrets to a plain serviceKeep the server patchedan old OpenSSH is a way inClose tunnels when donea forgotten tunnel is an open doorA tunnel bypasses firewalls by design — treat it with the same care as a firewall rule.
Six rules for safe SSH tunneling. Illustration by Cysoro.
  • 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, AllowTcpForwarding and PermitOpen control 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.
Tunnels bypass firewalls by designThat is the point — and the risk. A tunnel that reaches an internal database also lets anyone with access to your laptop reach it. Lock your screen, use keys, and never leave a tunnel running on a machine you do not control.

Why is my SSH tunnel not working?

Most tunnel failures come from a small set of causes. Work through them in order.

  1. Port already in use: if the local port is taken, SSH fails or silently skips the forward. Pick another port, or add -o ExitOnForwardFailure=yes so failures are loud.
  2. 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.
  3. Service not listening: check on the server that the service is actually running and listening on the expected port.
  4. Forwarding disabled: the server may have forwarding turned off (AllowTcpForwarding no in its configuration). Hardened servers often disable it.
  5. 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

  1. OpenBSD — ssh(1), OpenSSH remote login client manual
  2. IETF — RFC 4251, The Secure Shell (SSH) Protocol Architecture
  3. IETF — RFC 4253, The Secure Shell (SSH) Transport Layer Protocol
  4. Microsoft Learn — Can't install OpenSSH Features in Windows
Written & fact-checked byCysoro Security Team

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.

  • Email authentication
  • Phishing & BEC
  • Domain & DNS security
  • Small-business security

Checked against the OpenSSH ssh(1) manual, RFC 4251, RFC 4253 and Microsoft Learn's OpenSSH documentation.

How we write and fact-check →

Free tools for this topic

No account needed.

See where your domain stands

Free for up to 3 domains. No card, no installs.

Start free