Guide

SSH remote port forwarding (ssh -R)

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

Short answer

ssh -R [bind_address:]port:host:hostport user@server asks the SSH server to listen on port. Each connection to that port is carried back through the SSH connection, and the SSH client connects to host:hostport from your side. By default the server's listener is bound to loopback, so only programs on the server can use it; the server's GatewayPorts setting controls whether it can be opened to others.

Everything here works with standard OpenSSH. Transputty, the Windows SSH tunnel manager this site belongs to, is in development with no public build yet; see the release status.

What is remote port forwarding?

Remote forwarding is SSH port forwarding in the reverse direction. With a local forward you pull a remote service toward you. With a remote forward you push a service on your side out to the SSH server, using a connection that your machine opened outbound. That last point is why it works through NAT and firewalls that block incoming connections: nothing ever connects in to you.

client on the server                          your PC
        |                                        |
        v                                        v
bastion.example.com:8080  ==SSH (encrypted)==>  ssh client  -->  localhost:3000
   sshd listens here         (you dialed out)     connects out     dev server

The syntax, part by part

ssh -R [bind_address:]port:host:hostport user@server
Part Meaning Decided by
bind_address Optional. The address on the server to listen on. Honored only if the server's GatewayPorts allows it The SSH server
port The port on the server to listen on. 0 lets the server pick The SSH server
host The destination, as a name or address seen from your computer Your computer
hostport The destination port Your computer

The destination does not have to be your own machine. -R 8080:printer.lan:80 exposes a device on your local network to the server, with your computer relaying.

The ssh_config form puts the listener first and the destination second:

Host demo
    HostName bastion.example.com
    User deploy
    RemoteForward 8080 localhost:3000
    ExitOnForwardFailure yes

What is it used for?

Showing a local development server or receiving webhooks. Your app runs on localhost:3000. A remote forward makes it port 8080 on a server that has a public address:

ssh -N -R 8080:localhost:3000 deploy@bastion.example.com

On its own that port is reachable only from the bastion. To publish it, the usual pattern is a web server on the bastion (nginx, Caddy, Apache) that terminates HTTPS and proxies to 127.0.0.1:8080.

From a Windows client, write the destination as 127.0.0.1:3000 if the local service listens on IPv4 only. In our test the Windows OpenSSH client resolved localhost to the IPv6 address ::1 and tried nothing else.

Reaching a machine behind NAT (reverse SSH). A machine at a customer site or at home dials out and offers its own SSH port:

ssh -N -R 2222:localhost:22 deploy@bastion.example.com

From the bastion, ssh -p 2222 user@localhost now logs in to the hidden machine. The same shape works for Remote Desktop with -R 13389:localhost:3389.

Letting a server reach a service on your side. A build server needs a license server or a database that lives on your network; a remote forward gives it a loopback port for that one service.

OpenSSH 7.6 and later also accept -R port with no destination, which makes the client act as a SOCKS proxy for connections arriving at the server. It is the mirror image of dynamic forwarding.

GatewayPorts: who can reach the listener on the server?

The OpenSSH manual is explicit that, by default, the server binds remote forwards to the loopback interface only. The reason is safety: a remote forward creates a new listening port on a machine other people administer and other people use, and a public listener that leads into someone's private network is not something a server should hand out without the administrator's say. The control is GatewayPorts in the server's sshd_config:

GatewayPorts Effect on remote-forward listeners
no (default) Always bound to loopback. A bind_address from the client is ignored
yes Always bound to the wildcard address, so reachable on all of the server's interfaces
clientspecified The client chooses: -R 8080:... binds to loopback, -R 0.0.0.0:8080:... or -R *:8080:... binds to all interfaces, -R 203.0.113.10:8080:... to one address

clientspecified is the considered choice when public binds are needed at all, because each forward has to ask for them. Remember that a firewall or cloud security group in front of the server must also allow the port.

Do not confuse this with the client option of the same name. GatewayPorts in ssh_config (and -g) affects local forwards; for -R only the server's setting matters.

Other server-side controls

Directive Relevant values Effect on -R
AllowTcpForwarding yes or all, remote, local, no yes, all and remote permit remote forwards. local and no refuse them
PermitListen any (default), none, port, host:port Limits which ports, and optionally which listen addresses, a remote forward may bind
GatewayPorts no, yes, clientspecified See above

PermitListen 8080 allows only port 8080; several entries are separated by spaces. One documented subtlety: when no listen host is given, ssh requests the name localhost, and PermitListen treats that name differently from the explicit addresses 127.0.0.1 and ::1. If a host-qualified rule does not match, check which form the client is sending.

A restricted tunnel-only account might look like this in sshd_config:

Match User tunnel
    AllowTcpForwarding remote
    PermitListen 8080
    GatewayPorts no
    X11Forwarding no
    PermitTTY no

Ports below 1024 can be requested only when logging in as root on the server.

Port 0: let the server choose

If you ask for port 0, the server allocates a free port and reports it to the client, which prints it:

ssh -N -R 0:localhost:3000 deploy@bastion.example.com
Allocated port 41523 for remote forward to localhost:3000

This avoids collisions when several people share a server. The drawback is that a plain ssh reconnect may be given a different port.

ExitOnForwardFailure

If the server refuses the listener, ssh prints Warning: remote port forwarding failed for listen port 8080 and, by default, stays connected with no forward at all. For anything unattended that is the wrong outcome. Set:

ssh -N -o ExitOnForwardFailure=yes -R 8080:localhost:3000 deploy@bastion.example.com

Now ssh exits when a forward cannot be established, so a supervisor can notice and retry. This matters most after a dropped connection: the old listener on the server can linger until the server notices the dead session, and an immediate reconnect finds the port still in use. Server-side ClientAliveInterval shortens that window; see keeping an SSH tunnel connected.

Security cautions

  • You are opening a door into your side. Whoever can reach the listener reaches the destination with no further login from SSH. On a shared server, loopback still means every local user and every process there.
  • Development servers are rarely hardened. Debug pages, admin consoles and default credentials become reachable. Put authentication and TLS in front when the port is public.
  • Check policy. A remote forward from a corporate network to an outside server bypasses the perimeter firewall by design. Many organizations forbid that, and some monitor for it.
  • Prefer narrow rules. Forward one port to one service, use PermitListen and a dedicated account on the server, and stop the tunnel when you are done.

Remote forwarding or a hosted tunnel service?

Hosted tunnel services such as ngrok and Cloudflare Tunnel solve the "expose a local service" problem with an agent on your machine that dials out to the provider's network. The comparison below is deliberately general; plans and features differ, so check each provider's documentation.

ssh -R Hosted tunnel service
What you need An SSH server you control with a reachable address An account with the provider and its agent program
Public URL and TLS certificate You arrange them (reverse proxy, DNS, certificate) Typically provided
Who relays the traffic Your own server The provider's network
Protocols Any TCP service Depends on the service and plan
Access controls, request inspection Whatever you build on your server Often built in
Cost The server you already run Depends on the provider and plan

If you already run a public SSH server and need a TCP port, ssh -R is enough. If you need a public HTTPS URL in a minute and have no server, a hosted service is the shorter path.

How Transputty handles remote forwards

Transputty is in development and has no public build yet. In the current internal build for Windows 11, a remote forward is a rule of kind "Remote (-R)". When that kind is selected, "Listen address" and "Port" describe the listener on the server, and "Destination host" and "Port" describe what this PC connects to, for example localhost and 3000. Remote rules sit on the same connection as local and SOCKS rules and are switched on and off individually.

A rule with Kind set to 'Remote (-R)': the server listens on 127.0.0.1:8443 and connections arrive at localhost:3000 on this PC. The diagram is drawn in that direction. The app is connected to local test servers with fictional host names.

If the server declines the listener, the rule shows "Server refused to listen" with a hint to check AllowTcpForwarding and GatewayPorts on the server or whether the port is already in use there. Unlike ssh with ExitOnForwardFailure, the SSH connection stays up and its other rules keep working, with the failed rule marked on its own.

After a reconnect. When the SSH connection drops and comes back, Transputty asks the server for the listener again. If the server still holds the port for the dead session, which happens when it has not yet noticed that the old session is gone, the request is refused and the rule shows "Server refused to listen". In 0.1 a refused remote rule is not requested again automatically, not even after later reconnects, so you have to retry the rule yourself. ClientAliveInterval on the server shortens the time a dead session keeps its port. For a reverse tunnel that must recover with nobody present, ssh with ExitOnForwardFailure=yes under a supervisor is the more dependable choice today.

Limits: the app has no control for requesting a public bind on the server, so use localhost or 127.0.0.1 as the listen address and publish through a reverse proxy on the server if you need to. Transputty then asks for a loopback listener, but the server has the final say: with GatewayPorts yes in its sshd_config, sshd opens the port on every interface whatever the client asks. Check on the server with ss -ltn. Whether a remote forward is permitted is always the server's decision. The SOCKS form of -R is not supported. A walk-through is in how to set up SSH remote port forwarding.

Common questions

What does ssh -R 8080:localhost:3000 deploy@bastion.example.com do?

ssh -R 8080:localhost:3000 deploy@bastion.example.com logs in to bastion.example.com and asks the SSH server to listen on its port 8080. Each connection to that port is carried back through the SSH connection to your computer, where the SSH client connects to localhost port 3000. A web server running on your machine becomes reachable as port 8080 on the bastion, by default only from the bastion itself.

Why is my remote forward only reachable from the server itself?

Because the OpenSSH server binds remote forwards to the loopback address unless told otherwise. The sshd_config directive GatewayPorts defaults to no, which forces loopback whatever the client asks for. An administrator must set GatewayPorts to yes or clientspecified before other hosts can connect, or put a reverse proxy on the server in front of the loopback port.

What is the difference between ssh -L and ssh -R?

The side that listens. With -L the SSH client listens on your computer and the server connects to the destination, so you reach something on the server's side. With -R the SSH server listens and your client connects to the destination, so the server's side reaches something on yours. The argument order is the same: listening port first, destination second.

What is a reverse SSH tunnel?

A reverse SSH tunnel is a remote forward (ssh -R) used to reach a machine that cannot accept incoming connections, for example one behind NAT. The hidden machine connects out to a public SSH server with -R 2222:localhost:22. Someone logged in to that server can then run ssh -p 2222 user@localhost and land on the hidden machine through the tunnel it opened.

What does 'remote port forwarding failed for listen port' mean?

The SSH server declined to open the listener. The usual causes are that the port is already in use on the server, that AllowTcpForwarding or PermitListen in sshd_config forbids it, or that the port is below 1024 and the login is not root. The SSH session itself stays up 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.