SSH port forwarding sends TCP traffic through an encrypted SSH connection. Use ssh -L 8080:localhost:80 user@server to reach a service on the server's side through a port on your machine, ssh -R to expose a port from your machine on the server, and ssh -D 1080 user@server for a SOCKS proxy.
All three forms are options of the standard OpenSSH client. Replace user, server and the port numbers with your own values.
The three kinds of SSH tunnel at a glance
| Type | Flag | Who listens | Typical use |
|---|---|---|---|
| Local forwarding | -L | Your machine (the client) | Reach a database or web interface that only the server can see |
| Remote forwarding | -R | The SSH server | Let the server (or others) reach a service running on your machine |
| Dynamic forwarding | -D | Your machine, as a SOCKS proxy | Send an application's traffic out through the server |
Local port forwarding (ssh -L)
The syntax from the ssh manual is -L [bind_address:]port:host:hostport. Connections to port on your machine are forwarded over the tunnel, and the SSH server opens the connection to host:hostport.
ssh -L 5433:localhost:5432 user@db-server
While this session is open, connecting to localhost:5433 on your machine reaches port 5432 on db-server. Here localhost in the middle is resolved from the server's point of view, so it means the server itself. Port 5432 is PostgreSQL's default, so you could now run psql -h localhost -p 5433 locally.
The destination does not have to be the SSH server. To reach another machine on the server's network:
ssh -L 8080:intranet-host:80 user@gateway
Traffic is encrypted only between your machine and gateway. The last hop, from gateway to intranet-host, is a normal connection.
Choose a local port above 1023. The manual says that only the superuser can forward privileged ports.
Remote port forwarding (ssh -R)
The syntax is -R [bind_address:]port:host:hostport. The server listens on port and sends each connection back through the tunnel, and your side opens the connection to host:hostport.
ssh -R 9000:localhost:3000 user@server
Now, on server, connecting to localhost:9000 reaches port 3000 on your own machine. This is how you let a remote machine call a development server that sits behind your home or office router.
By default only the server itself can use that port. The manual states that TCP listening sockets on the server are bound to the loopback interface only. A bind address such as 0.0.0.0:9000 works only if the server's GatewayPorts option is enabled. Privileged ports can be forwarded only when logging in as root on the remote machine.
If you give a remote port of 0 in ssh_config's RemoteForward, the server allocates the listening port dynamically.
Dynamic port forwarding (ssh -D)
The syntax is -D [bind_address:]port. It makes ssh act as a SOCKS server on your machine. The manual says SOCKS4 and SOCKS5 are supported.
ssh -D 1080 user@server
Point an application's SOCKS proxy setting at localhost:1080, and its connections go out from server. Unlike -L, you do not name a destination up front: the application chooses one per connection. Only applications configured to use the proxy are tunnelled. Everything else on your machine still uses your normal connection.
Run a tunnel in the background without a shell
If you only want the tunnel, two more options help. -N means "do not execute a remote command", which the manual describes as useful for just forwarding ports. -f asks ssh to go to the background just before command execution.
ssh -f -N -o ExitOnForwardFailure=yes -L 5433:localhost:5432 user@db-server
ExitOnForwardFailure defaults to no. Setting it to yes makes ssh terminate the connection if it cannot set up all the requested forwardings, for example when the local port is already taken. If the local port is already taken, see how to find and free a port that is already in use.
Jump through another host with -J
When the server you want sits behind a bastion host, -J destination connects to the target by first making an SSH connection to the jump host. Multiple hops are separated by commas.
ssh -J user@bastion -L 5433:localhost:5432 user@db-server
Save a tunnel in your config file
The same forwards can live in ~/.ssh/config, so you only type the host name. The directives are LocalForward, RemoteForward and DynamicForward, and the first argument is the listening side:
Host dbtunnel
HostName db-server
User user
LocalForward 5433 localhost:5432
ExitOnForwardFailure yes
ServerAliveInterval 30
Then run ssh -N dbtunnel. ServerAliveInterval defaults to 0, which means no keep-alive messages are sent. Setting it to a number of seconds makes ssh send a probe after that long without data from the server. For the file's other options, see the SSH config file guide.
Who can reach your forwarded port
For -L and -D, the manual says a bind address of localhost binds the port for local use only, while an empty address or * makes it available from all interfaces. When you give no bind address, the behaviour follows the GatewayPorts setting of your ssh client.
ssh -L 127.0.0.1:8080:intranet-host:80 user@gateway
Writing the loopback address explicitly keeps the behaviour the same regardless of configuration. Binding to * exposes the forwarded service to everyone who can reach your machine, and the SSH encryption does not authenticate those people.
Server settings that allow or block forwarding
These are sshd_config options on the server, as documented in the sshd_config manual:
| Option | Values | Default | Effect |
|---|---|---|---|
AllowTcpForwarding | yes, all, no, local, remote | yes | Whether TCP forwarding is permitted, and in which direction |
GatewayPorts | no, yes, clientspecified | no | Whether remote forwards bind beyond loopback |
PermitOpen | any, none, or host:port list | all requests permitted | Limits which destinations -L forwards may reach |
DisableForwarding | (see manual) | not stated in the manual | Disables X11, agent, TCP and StreamLocal forwarding and overrides the other forwarding options |
A server administrator who wants to restrict tunnelling should know the caveat in the manual: disabling TCP forwarding does not improve security unless users are also denied shell access, since they can run their own forwarders.
If the tunnel does not work
- The local port is already in use. Pick another local port, or find the process holding it. With
ExitOnForwardFailure=yesssh tells you immediately instead of staying connected. - Connection refused through the tunnel. The tunnel itself may be fine. Check that the destination service is actually listening on the host and port you gave, from the server's point of view for
-L. - The remote forward is only reachable on the server itself. That is the default. Enable
GatewayPortson the server if you need a non-loopback bind, and understand who can then reach it. - Forwarding is refused. The server may set
AllowTcpForwarding no, a restrictivePermitOpen, orDisableForwarding. Only the server's administrator can change these.
Frequently asked questions
- What is the difference between ssh -L and ssh -R?
- With -L, your own machine listens on a port and sends connections through the SSH server to a destination. With -R, the SSH server listens on a port and sends connections back through the tunnel to your machine or to a host your machine can reach.
- Why can't other computers connect to my forwarded port?
- By default the forwarded port is bound to the loopback address only. For -R, the server's GatewayPorts option must also be enabled before a remote bind address is honoured. For -L, you can set a bind address such as 0.0.0.0 or *, which makes the port reachable from other machines, so only do that deliberately.
- How do I run an SSH tunnel in the background?
- Add -N so that no remote command runs, and -f so that ssh goes to the background just before command execution. Together with -o ExitOnForwardFailure=yes, ssh exits instead of staying connected when a forward could not be set up.
- Does disabling AllowTcpForwarding stop tunnelling?
- Only if users also have no shell access. The sshd_config manual notes that disabling TCP forwarding does not improve security unless users are also denied shell access, because they can install their own forwarders.