Guide

SSH local port forwarding (ssh -L)

Local port forwarding makes a port on your own computer lead to a host and port that the SSH server can reach. The SSH client listens; the SSH server makes the final connection.

Short answer

ssh -L [bind_address:]port:host:hostport user@server makes the SSH client listen on port on your computer. Each connection to that port is carried through the encrypted SSH connection, and the SSH server then connects to host:hostport. The destination name is resolved by the server, so localhost means the SSH server itself. The listener is loopback-only unless you ask otherwise.

The commands here are standard OpenSSH and work without any extra software. Transputty, the Windows SSH tunnel manager this site belongs to, is in development with no public build yet; see the release status.

New to tunnels? Create your first SSH tunnel on Windows walks through one from start to finish; this page is the reference.

What is local port forwarding?

Local port forwarding is the type of SSH port forwarding where the listening port is on the client. You use it when a service is reachable from the SSH server and not from where you are sitting: a database in a private subnet, an admin page bound to localhost on the server, a Windows machine's Remote Desktop port behind a bastion.

psql on your PC
   |
   v
127.0.0.1:5433  ==SSH (encrypted)==>  bastion.example.com  -->  db.internal:5432
 ssh listens here                       sshd connects out         PostgreSQL

The syntax, part by part

ssh -L [bind_address:]port:host:hostport user@server
Part Meaning Decided by
bind_address Optional. The local address the listener binds to. Left out, the client's GatewayPorts setting applies, which means loopback by default Your computer
port The local port to listen on Your computer
host The destination host, as a name or address seen from the SSH server The SSH server
hostport The destination port The SSH server
user@server The SSH server that will make the outgoing connection

A real example:

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

The local and destination ports do not have to match. This example listens on 5433 because a PostgreSQL server installed on your own PC would already hold 5432, and a client pointed at 127.0.0.1:5432 would then reach the local database, not the tunnel. Any free local port will do.

IPv6 addresses go in square brackets, for example -L 8080:[2001:db8::10]:80. OpenSSH also accepts Unix socket paths in place of either side.

Who resolves host?

The SSH server does, each time a connection is opened. The client passes the destination string along untouched, and the server performs the lookup and the TCP connect at that moment. Three consequences:

  1. Names that exist only in the server's DNS or /etc/hosts work.
  2. localhost and 127.0.0.1 in the destination refer to the SSH server, not to your PC.
  3. A wrong destination is not reported when the tunnel starts. ssh connects and looks healthy; the error appears only when a program uses the port, as channel 2: open failed: connect failed: Connection refused in the ssh output and a dropped connection in the application.

The two classic shapes

The service runs on the SSH server itself. Many services listen only on loopback for safety. Use 127.0.0.1 as the destination, which here means the SSH server:

ssh -L 8080:127.0.0.1:80 deploy@web01.example.com
your PC :8080  ==SSH==>  web01.example.com  --loopback-->  127.0.0.1:80

Here the forwarded data never crosses a network unencrypted. localhost usually works as the destination too, but on some servers it resolves to the IPv6 address ::1 first, and a service that listens only on IPv4 then refuses the connection.

The service runs on a third host behind the SSH server. The SSH server acts as a relay:

ssh -L 3306:mysql.internal:3306 deploy@bastion.example.com
your PC :3306  ==SSH==>  bastion.example.com  --plain TCP-->  mysql.internal:3306

The leg from the bastion to mysql.internal is not encrypted by SSH, and the database sees the connection as coming from the bastion's address. Firewall rules and database grants must allow the bastion, not your PC.

LocalForward in ssh_config

Anything you type more than twice belongs in ~/.ssh/config (on Windows, C:\Users\you\.ssh\config). LocalForward takes the listener first and the destination second, separated by a space:

Host staging-db
    HostName bastion.example.com
    User deploy
    LocalForward 5433 db.internal:5432
    LocalForward 127.0.0.1:6379 cache.internal:6379
    ExitOnForwardFailure yes
    ServerAliveInterval 30

Then ssh -N staging-db starts both forwards.

Bind address: who can connect to the listener?

By default a local forward listens on the loopback address, so only programs on your own machine can use it. Three client-side controls change that:

What you write Listener binds to
-L 5433:db.internal:5432 Loopback only (with the default GatewayPorts no)
-L localhost:5433:db.internal:5432 Loopback only, whatever GatewayPorts says
-L 0.0.0.0:5433:db.internal:5432 or -L *:5433:... All interfaces
-L 192.0.2.25:5433:db.internal:5432 That one local address
-g, or GatewayPorts yes in ssh_config All interfaces, for forwards with no explicit bind address

For -L, the setting that matters is the client's GatewayPorts (the -g flag), because the client owns the listener. The GatewayPorts directive in the server's sshd_config is a different setting that governs remote forwards and has no effect here.

A local forward bound to a non-loopback address lets any machine that can reach your computer on that port connect to the destination through your SSH login, with no authentication of its own. Your operating system firewall may also ask for permission. Bind to a specific address, not 0.0.0.0, when you can.

Running a tunnel without a shell: -N and -f

By default ssh also opens a shell. For a tunnel you usually want neither a shell nor a command:

ssh -N -L 5433:db.internal:5432 deploy@bastion.example.com
  • -N tells ssh not to execute a remote command. The session exists only for forwarding, and you stop it with Ctrl+C.
  • -f sends ssh to the background just before command execution, after any password or passphrase prompt. ssh -f -N ... is the usual pairing on Linux and macOS.
  • -o ExitOnForwardFailure=yes makes ssh exit if it cannot set up a requested forward, for example because the local port is taken. Without it you get a warning and a session that forwards nothing. It does not react to failures of individual forwarded connections later on.

Backgrounding with -f has not always worked in the Windows build of OpenSSH. On Windows, leaving ssh -N running in its own terminal window is the dependable choice.

Several forwards, and ports below 1024

Repeat -L for more forwards on the same connection. The command is written on one line so that it pastes into PowerShell, Command Prompt and Unix shells alike:

ssh -N -L 5433:db.internal:5432 -L 6379:cache.internal:6379 -L 8443:admin.internal:443 deploy@bastion.example.com

On Unix-like systems, only root can listen on ports below 1024, and the ssh manual states that only the superuser can forward privileged ports. Running ssh as root to get port 443 is rarely worth it; listen on 8443 and use that. Windows has no such rule: a low port does not need administrator rights. A port on Windows can still be unavailable because a system service holds it or because it falls in a range reserved by Hyper-V, WSL 2 or Docker Desktop, which you can list with netsh interface ipv4 show excludedportrange protocol=tcp. If the port is simply taken by another program, see local port already in use.

Typical uses

Service Default port Example forward Then connect to
PostgreSQL 5432 -L 5433:db.internal:5432 127.0.0.1:5433
MySQL / MariaDB 3306 -L 3306:mysql.internal:3306 127.0.0.1:3306
Microsoft SQL Server 1433 -L 1433:mssql.internal:1433 127.0.0.1,1433
Redis 6379 -L 6379:127.0.0.1:6379 127.0.0.1:6379
Web admin interface 80 / 443 -L 8443:admin.internal:443 https://localhost:8443/
Remote Desktop (RDP) 3389 -L 13389:win01.internal:3389 127.0.0.1:13389

Two practical notes. For RDP, use a local port other than 3389, since Windows may already be listening there. For HTTPS, the browser checks the certificate against the name you typed, so https://localhost:8443/ will show a name mismatch warning, and sites that depend on their host name may misbehave; a SOCKS proxy handles web browsing better than a fixed forward.

Step-by-step versions are in the tutorials: create your first SSH tunnel on Windows, set up SSH local port forwarding, PostgreSQL through an SSH tunnel and MySQL through an SSH tunnel.

How Transputty handles local forwards

Transputty is in development and has no public build yet. In the current internal build for Windows 11, a local forward is a rule of kind "Local (-L)" on a saved connection. The fields map directly onto the ssh -L syntax:

ssh -L part Transputty field
bind_address "Listen address", 127.0.0.1 by default
port "Port" (leave it empty and a free port is chosen automatically)
host "Destination host", as seen from the SSH server
hostport The destination "Port"
A rule with Kind set to 'Local (-L)': listen address 127.0.0.1, port 15432, destination host reports-db.internal, port 5432, and the flow diagram below the fields. This connection also goes through a jump host, shown at the top. The app is connected to local test servers with fictional host names.

A connection can carry several rules, and rule changes apply to a running connection without dropping it. If the local port is taken, the rule shows "Port in use" while the other rules keep working, and a callout names the program holding the port when Windows reports it and offers the next free port. Presets for PostgreSQL, MySQL, SQL Server, MongoDB, Redis, Web, NAS and RDP pre-fill the ports; they do nothing database-specific. When the connection drops, the local port stays bound while Transputty reconnects; that page also explains the cost, which is that a plain TCP check of the port succeeds while the tunnel is down.

Limits: the editor accepts only loopback listen addresses, so sharing a forward with your LAN (the -g case above) is not something the app offers. Transputty needs no administrator rights, for low ports or any others. When Windows refuses a listen port with error 10013, the rule shows a callout saying the port needs administrator rights. That error usually means the port is in an excluded (reserved) range or another program holds it exclusively, so elevation does not help: choose a port outside the reserved ranges. Unix-socket forwards are not supported.

Common questions

What does ssh -L 5433:db.internal:5432 deploy@bastion.example.com do?

ssh -L 5433:db.internal:5432 deploy@bastion.example.com logs in to bastion.example.com as deploy and makes the SSH client listen on port 5433 of your own computer (loopback only). Each connection to 127.0.0.1:5433 is carried through the SSH connection, and the bastion connects to db.internal port 5432 on your behalf. Your database client uses 127.0.0.1:5433 as if the database were local.

In ssh -L, which machine resolves the destination host name?

The SSH server. The client sends the destination name and port unchanged, and the server looks the name up and connects each time a forwarded connection is opened. That is why names such as db.internal, which exist only in the server's network, work, and why localhost in an -L destination means the SSH server itself.

Is local port forwarding encrypted?

Between your computer and the SSH server, yes: the forwarded data travels inside the SSH connection. Between the SSH server and the destination it is an ordinary TCP connection. When the destination is localhost on the SSH server, that last leg never leaves the machine. When it is a third host, keep the application's own TLS switched on.

Can other computers on my network use my local forward?

Not by default. OpenSSH binds a local forward to the loopback address, so only programs on your computer can connect. To expose it, give a bind address such as 0.0.0.0, or use the client option -g (GatewayPorts yes in ssh_config). Anyone who can reach that port then reaches the destination with no login of their own, so do this deliberately.

Why do I get 'connection refused' when I use the forwarded port?

The tunnel was created, but the SSH server could not open the destination: the host name or port is wrong from the server's point of view, or the service listens on a different address. A common case is a destination of localhost resolving to ::1 on the server while the service listens only on 127.0.0.1. Run ssh with -v to see the channel error.

Can I forward several ports over one SSH connection?

Yes. Repeat the -L option, or add several LocalForward lines under one Host entry in ssh_config. All forwards share the single SSH connection and its login. If one local port is already taken, ssh warns and carries on with the others unless ExitOnForwardFailure is set to yes.

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.