Guide

SSH port forwarding (SSH tunneling) explained

SSH port forwarding, also called SSH tunneling, carries TCP connections for other programs through an encrypted SSH connection. It comes in three types: local (-L), remote (-R) and dynamic (-D).

Short answer

SSH port forwarding (SSH tunneling) makes a TCP port on one side of an SSH connection lead to a host and port on the other side. Local forwarding (ssh -L) reaches a service behind the SSH server, remote forwarding (ssh -R) exposes a service on your side to the server, and dynamic forwarding (ssh -D) creates a SOCKS proxy. SSH encrypts the traffic between the SSH client and the SSH server only.

This guide uses standard OpenSSH throughout. Transputty, the Windows SSH tunnel manager this site belongs to, is in development and has no public build yet; see the release status. How Transputty handles forwarding is described in one section near the end.

Want to make one now? Create your first SSH tunnel on Windows is a short walk-through that needs nothing installed.

What is an SSH tunnel?

An SSH tunnel is a TCP connection carried inside an encrypted SSH session. The SSH client or server listens on a port, and every connection made to that port is relayed through the SSH connection and delivered to a host and port on the other side. The technique is also called SSH port forwarding.

A typical case: a database accepts connections only from inside a private network, and you can SSH into one server on that network. That server is often called a bastion or jump host: the one machine you are allowed to log in to, which can in turn reach the private ones. An SSH tunnel lets a program on your PC connect to a port on your own machine. SSH carries that connection to the bastion, which passes it on to the database. To your program, the database appears to be local.

your PC :5433  ==SSH (encrypted)==>  bastion.example.com  -->  db.internal:5432
 (listener)                             (SSH server)            (plain TCP)

That is a local forward. A database client on your PC connects to 127.0.0.1:5433, and the connection comes out of bastion.example.com toward db.internal:5432, a host your PC cannot reach directly. The local port is 5433 and not 5432 so that it cannot collide with a PostgreSQL server installed on your own PC.

How it works underneath

An SSH connection is more than a remote shell. Inside the one encrypted TCP connection between client and server, the protocol can open many independent channels. A shell is one kind of channel. A forwarded TCP connection is another.

With port forwarding, one side listens on a TCP port. Every time a program connects to that port, SSH opens a channel, the other side makes a TCP connection to the real destination, and bytes are copied in both directions. The program that connected does not need to know SSH is involved: it talks to an ordinary port.

What is and isn't encrypted?

SSH encrypts and authenticates one leg only: the connection between the SSH client and the SSH server. Two other legs exist in every tunnel:

  • Application to listener. Usually loopback (127.0.0.1, an address that only programs on the same computer can reach), so it never touches a network.
  • SSH server to destination. An ordinary TCP connection. If the destination is the SSH server itself (localhost), it stays inside that machine. If it is a third host, the data crosses that network exactly as the application sent it.

So a tunnel protects traffic across the untrusted part of the path, typically the internet, and relies on the destination network being trusted. If the application protocol has its own encryption (HTTPS, TLS for PostgreSQL or MySQL), keep it on. The two layers do not conflict.

The three types of port forwarding side by side

Local (-L) Remote (-R) Dynamic (-D)
Listener is on Your computer (SSH client) The SSH server Your computer (SSH client)
Connections flow Client side to server side Server side to client side Client side to server side
Destination Fixed when the tunnel is created Fixed when the tunnel is created Chosen per connection by the application (SOCKS)
Destination is reached from The SSH server The SSH client The SSH server
Typical use Reach a database, admin page or RDP host behind a bastion Show a local dev server to someone, reach a machine behind NAT Browse or run tools as if from the server's network
Example ssh -L 5433:db.internal:5432 deploy@bastion.example.com ssh -R 8080:localhost:3000 deploy@bastion.example.com ssh -D 1080 deploy@bastion.example.com

Local port forwarding

Local forwarding is the common case: "I need to reach something that only the server can reach." The SSH client listens on a port on your machine and the SSH server connects to the destination. One forward leads to exactly one destination host and port. Syntax, bind addresses, LocalForward and the usual pitfalls are covered in local port forwarding (ssh -L).

Remote port forwarding

Remote forwarding runs the other way. The SSH server listens on a port, and connections arriving there are carried back to your machine, which connects to the destination. It is how you expose a local development server, or reach a machine that sits behind NAT and can only make outbound connections. The server's GatewayPorts setting decides who can reach the listener. See remote port forwarding (ssh -R).

Dynamic port forwarding

Dynamic forwarding turns the SSH client into a SOCKS proxy. There is one local listener and no fixed destination: each application connection tells the proxy where it wants to go, and the SSH server connects there. See dynamic port forwarding (ssh -D) for the mechanism and using an SSH SOCKS proxy for browser, curl and DNS settings.

How are jump hosts (ProxyJump) different from port forwarding?

A jump host is an SSH server you pass through to reach another SSH server. ProxyJump (-J) is built on the same forwarding mechanism, but what it carries is a second SSH connection rather than your application's traffic:

ssh -J deploy@bastion.example.com deploy@db.internal

Your client logs in to the bastion, asks it to open a TCP connection to db.internal:22, and runs a complete, separately authenticated SSH session to db.internal through that channel. The bastion relays ciphertext it cannot read. Port forwarding then happens on the inner connection:

ssh -J deploy@bastion.example.com -L 5433:127.0.0.1:5432 deploy@db.internal

Use a plain forward when the bastion can reach the service directly. Use a jump host when the service only accepts connections from a machine further inside, and you have an SSH login there. The jump host must allow TCP forwarding, because that is how it relays the inner connection.

What must the SSH server allow?

Forwarding is on by default in OpenSSH, and administrators often restrict it. The relevant sshd_config directives:

Directive Default What it controls
AllowTcpForwarding yes yes or all permits forwarding, no forbids it, local permits only local-style forwards (including dynamic), remote permits only remote forwards
PermitOpen any Which destination host:port pairs local and dynamic forwards may connect to
PermitListen any Which addresses and ports remote forwards may listen on
GatewayPorts no Whether remote-forward listeners may bind to non-loopback addresses (no, yes, clientspecified)

The same limits can be attached to a single key in authorized_keys with options such as permitopen="db.internal:5432", permitlisten, no-port-forwarding and restrict. The OpenSSH manual also notes that turning forwarding off does not improve security for users who have shell access, since they can run their own forwarders.

When the server refuses, the client says so: administratively prohibited: open failed for a local or dynamic forward, or remote port forwarding failed for listen port 8080 for a remote one.

SSH tunnel or VPN: which one do I need?

An SSH tunnel can replace a VPN for a narrow need: a few specific TCP services, a person comfortable pointing each application at a local port, and an SSH login on a server that can reach the targets. An SSH tunnel is not a full VPN: it carries TCP only, and it does not route the whole computer's traffic.

SSH port forwarding VPN
Scope One port per forward, or per-application through SOCKS Routes whole subnets, or all traffic, at the IP layer
Setup An existing SSH account; no extra server software A VPN server, client software and routing or firewall changes
Client rights An ordinary user account (on Linux and macOS, only for ports above 1023) Usually administrator rights to install a network adapter
UDP and ICMP No. TCP only Yes
Application changes Each application is pointed at 127.0.0.1:port or a SOCKS proxy None; addresses work as if you were on the network
DNS for internal names Only through SOCKS with remote DNS, or by using IP addresses Can be pushed to the client
Best when A developer or administrator needs specific services through a bastion Many people or many services, UDP, or software that cannot use a proxy

OpenSSH can also build a real layer-3 tunnel with -w and PermitTunnel. That is a different feature from port forwarding, needs root at both ends, and is rarely the simplest VPN.

How do I keep a tunnel alive?

Tunnels drop when an idle connection is forgotten by a NAT device or firewall, when a laptop sleeps, when the network changes, or when the server restarts. Two things help: keepalives (ServerAliveInterval), which detect a dead connection and keep idle ones from being dropped, and something that restarts the tunnel, because ssh itself never reconnects. Both are covered in keeping an SSH tunnel connected.

Common errors

Message Meaning More
bind: Address already in use Another program holds the local port Local port already in use
channel 2: open failed: connect failed: Connection refused The SSH server reached the destination host, and nothing is listening on that port there SSH tunnel "connection refused"
administratively prohibited: open failed AllowTcpForwarding or PermitOpen on the server forbids it Ask the server administrator
Timeout, server ... not responding or Broken pipe The SSH connection died SSH tunnel keeps disconnecting

Is there a GUI for SSH port forwarding?

Yes. On Windows, PuTTY (Connection › SSH › Tunnels), Bitvise SSH Client and MobaXterm set up forwards in a window, and Core Tunnel does the same on macOS. Transputty is a Windows GUI for port forwarding that is still in development. A GUI tunnel manager is useful when you run several tunnels daily and want status, reconnection and saved logins in one place: see what an SSH tunnel manager does and the comparison of SSH tunneling tools.

The other ways to run a tunnel:

  • The ssh command. Present on macOS and Linux, and included with Windows 10 and 11 as the OpenSSH client. Add -N for a tunnel with no shell: ssh -N -L 5433:db.internal:5432 deploy@bastion.example.com.
  • An ssh_config file. LocalForward, RemoteForward, DynamicForward and ProxyJump under a Host entry let you start a set of tunnels with ssh -N name.
  • PuTTY. Forwards are added under Connection › SSH › Tunnels and saved with the session; plink does the same from a command line.
  • A supervisor. autossh or a systemd unit restarts ssh when it exits.

For a single tunnel you start now and then, plain ssh is enough: see your first SSH tunnel on Windows.

How Transputty handles port forwarding

Transputty is an SSH tunnel manager for Windows 11 that is in development; there is no public build yet. In its current internal build, a saved connection is an SSH server plus one or more rules, and each rule is one of the three types: "Local (-L)", "Remote (-R)" or "SOCKS (-D)". Several rules share one SSH connection, each rule reports its own state, and a rule can be switched off while the others keep running.

Jump hosts are configured under "Jump hosts (ProxyJump)", up to eight hops, each with its own login and its own host key check. The editor shows the equivalent ssh command for what you have configured, as a reference; Transputty has its own SSH engine and does not run ssh.

One connection with a local (L), a remote (R) and a SOCKS (D) rule, the selected rule's fields and diagram, and the equivalent ssh command at the bottom. The app is connected to local test servers with fictional host names.

Limits worth knowing: listeners bind to 127.0.0.1 and the editor refuses other listen addresses, the SOCKS proxy is SOCKS5 only, and there is no UDP, X11, agent forwarding, Unix-socket forwarding, ProxyCommand or terminal. Transputty forwards ports and nothing else.

Common questions

What is an SSH tunnel?

An SSH tunnel is a TCP connection carried inside an encrypted SSH session. The SSH client or server listens on a port, and every connection made to that port is relayed through the SSH connection and delivered to a host and port on the other side. SSH tunnels are used to reach a database, admin page or other service that only the SSH server can reach. The technique is also called SSH port forwarding.

Is SSH port forwarding the same as SSH tunneling?

Yes. Both names describe the same feature of the SSH protocol: an SSH client or server listens on a TCP port and carries each connection made to that port through the encrypted SSH connection to a destination on the other side. The SSH standard (RFC 4254) calls it TCP/IP port forwarding; tunneling is the everyday name.

Can I use SSH port forwarding instead of a VPN?

Yes, for a narrow need. SSH port forwarding can replace a VPN when you need a few specific TCP services, such as a database or an internal website, and already have an SSH login on a server that can reach them. SSH port forwarding is not a full VPN: it carries TCP only (no UDP or ping), does not route the whole computer's traffic, and each application has to be pointed at a forwarded port or a SOCKS proxy.

Is traffic through an SSH tunnel encrypted end to end?

Only the part between the SSH client and the SSH server is encrypted by SSH. From the SSH server to the final destination the traffic travels exactly as the application sent it. If the application protocol is itself encrypted, for example HTTPS or PostgreSQL with TLS, that protection continues all the way; if it is plain text, the last leg is plain text.

What is the difference between local, remote and dynamic port forwarding?

Local forwarding (ssh -L) opens a port on your computer that leads to one fixed destination reachable from the SSH server. Remote forwarding (ssh -R) opens a port on the SSH server that leads back to a destination reachable from your computer. Dynamic forwarding (ssh -D) opens a SOCKS proxy on your computer, and each application connection chooses its own destination.

Does SSH port forwarding work with UDP?

No. Standard SSH port forwarding carries TCP connections only, and the SOCKS proxy that ssh -D provides handles TCP connections only. Protocols that need UDP, such as most VoIP, WireGuard or classic DNS queries sent directly, need a VPN or another tool.

Sources and further reading

Transputty keeps SSH tunnels running on Windows

Local, remote and SOCKS5 forwards in one window, automatic reconnection with ports that stay bound, and a clear reason when something fails. In development; no public build yet.