Tutorial

How to set up SSH remote port forwarding

SSH remote port forwarding opens a port on the SSH server that leads back to a service on your side. Run ssh -N -R with the server port first, then the local destination, and check from the server with curl.

Level
Intermediate
Reading time
9 min
Applies to
Windows, OpenSSH command line, Linux
Transputty steps checked against
Transputty 0.1 (development build, not released)

Before you start

  • An SSH server you can log in to, for example deploy@bastion.example.com
  • Permission to read (and for public access, change) that server's sshd_config
  • A service on your side to expose, for example a dev web server on port 3000

Short answer

To set up SSH remote port forwarding, run ssh -N -R 8080:localhost:3000 deploy@bastion.example.com on the machine that has the service. Port 8080 on the SSH server then leads to port 3000 on your machine. By default it is reachable only from the server itself; public access needs GatewayPorts in the server's sshd_config.

The remote port forwarding guide explains the idea; this page is the how-to. Transputty, covered near the end, is in development with no public build yet; see release status.

What must the server allow?

Remote forwarding is decided by the SSH server. Check /etc/ssh/sshd_config on it:

Setting Default What it means for -R
AllowTcpForwarding yes Must be yes or remote. With no or local the server refuses the request
GatewayPorts no no: forwards listen on the server's loopback only. yes: always on all interfaces. clientspecified: the client's bind address decides
PermitListen any port If set, restricts which addresses and ports a remote forward may listen on

After a change, an administrator validates and reloads the server (the service is named ssh on Debian and Ubuntu, sshd on most others):

sudo sshd -t && sudo systemctl reload sshd

For public access, the server's firewall and any cloud security group must also allow the port. Ports below 1024 can only be bound when you log in as root.

Example 1: share a local dev web server

Your app runs on localhost:3000 on your computer. You want it to answer on port 8080 of bastion.example.com.

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

Add -o ExitOnForwardFailure=yes so that ssh exits with an error, rather than staying connected without the forward, when the server refuses or the port is taken.

Match the address your dev server listens on. In this command localhost:3000 is resolved by the ssh client. With OpenSSH_for_Windows 9.5p2 we saw localhost tried over IPv6 ([::1]) only: a server listening on 127.0.0.1 returned an empty reply, and worked once the command said -R 8080:127.0.0.1:3000. If your dev server prints http://127.0.0.1:3000, use that address.

Config-file equivalent, started with ssh -N share-dev:

Host share-dev
    HostName bastion.example.com
    User deploy
    RemoteForward 8080 localhost:3000
    ExitOnForwardFailure yes

Verify from the server

Open a second, normal SSH session to the server and run:

curl -I http://localhost:8080/
ss -ltn | grep 8080

curl should print your app's response headers. ss shows where the port is bound: 127.0.0.1:8080 means loopback only; 0.0.0.0:8080 or *:8080 means every interface.

Who can reach it now?

  • A teammate with an account on the server can use it as it is: curl http://localhost:8080 on the server, or their own local forward, ssh -N -L 8080:localhost:8080 teammate@bastion.example.com, and then http://localhost:8080 in their browser. Nothing is public, and no server setting changes.
  • A webhook from an outside service needs a public port. With GatewayPorts clientspecified on the server:
ssh -N -R 0.0.0.0:8080:localhost:3000 deploy@bastion.example.com

This puts your dev server on the internet over plain HTTP, with whatever authentication it has. Anyone who finds the port can use it. Stop the forward when you are done.

The webhook URL is then http://bastion.example.com:8080/. A common, safer variant keeps the forward on loopback and lets a web server on the bastion (nginx, Caddy) proxy a public HTTPS address to localhost:8080.

Example 2: reach a machine behind NAT

A machine at home or in a lab cannot accept inbound connections, but it can make outbound SSH connections. Let it publish its own SSH port on the bastion. On the hidden machine (Linux here), run:

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

From your own computer, jump through the bastion to that port:

ssh -J deploy@bastion.example.com -p 2222 labuser@localhost

Here localhost is resolved on the bastion, so the session lands on port 2222 there and travels back to the hidden machine. It stays on loopback and needs no GatewayPorts.

For Remote Desktop on a hidden Windows PC, run this on that PC (with RDP enabled):

ssh -N -R 13389:127.0.0.1:3389 deploy@bastion.example.com

Then, on your own computer, pair it with a local forward and connect:

ssh -N -L 13389:localhost:13389 deploy@bastion.example.com
mstsc /v:localhost:13389

Use the two commands in separate windows; the first stays open. The hidden machine's ssh must stay running for any of this to work, and it will not restart by itself after a network drop. Key-based login makes that practical: see using SSH private keys.

How do I do this in PuTTY?

In Connection › SSH › Tunnels:

  1. Select the Remote radio button.
  2. Source port: 8080. For a remote forward this is the port on the server.
  3. Destination: localhost:3000, the service as seen from your PC.
  4. Click Add. The list shows R8080 localhost:3000.
  5. Save the session on the Session panel, then Open.

The checkbox Remote ports do the same (SSH-2 only) asks the server to accept connections from other hosts; the server's GatewayPorts setting still has the final say.

How do I add a remote rule in Transputty?

These are the steps in the 0.1 development build.

  1. Open the connection's editor: right-click the connection and choose Edit…, or use the pencil button in the inspector.
  2. Under Forwarding rules, click Add rule, then set Kind to "Remote (-R)".
  3. Listen address and Port now describe the server side; the rule list shows them as server :8080. Use localhost or 127.0.0.1 as the listen address and 8080 as the port.
  4. Destination host and Port are reached from this PC: localhost and 3000.
  5. Click Save. On a running connection the new rule starts at once and the other rules are not interrupted. Save & reconnect restarts the whole connection, which is not needed for a rule change. If the connection is off, click Save & connect.

If the server says no, only that rule fails. It shows "Server refused to listen", with the hint "Check AllowTcpForwarding / GatewayPorts on bastion.example.com, or the port may already be in use there." The connection stays up and its other rules keep working, where ssh with ExitOnForwardFailure would exit.

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 from the dead session, which is likely because Transputty's first retry comes after about a second, the request is refused. The rule then shows "Server refused to listen" and is not retried automatically: click Retry on it once the server has let go of the port. ClientAliveInterval in the server's sshd_config makes it notice dead sessions sooner. For a reverse tunnel that must recover with nobody watching, ssh with ExitOnForwardFailure=yes under something that restarts it is the better tool today.

Limits to know: the editor has no control for a public server-side bind. Transputty's engine declines a remote forward on a non-loopback server address unless that is explicitly allowed, and the 0.1 editor offers no such option, so Transputty always asks the server for a loopback listener. The server has the final say: with GatewayPorts yes in its sshd_config, the port is opened on every interface whatever the client asks. Check with ss -ltn on the server. Publishing on purpose through GatewayPorts is a job for ssh or a reverse proxy on the server today.

A 'Remote (-R)' rule in the editor. In this capture the server port is 8443, so the rule list shows 'server :8443', and the destination is localhost:3000 on this PC. The host names are fictional and the servers are local test servers.

Security cautions

  • You are publishing a service. A dev server usually has no authentication, debug pages and verbose errors. On a public bind, anyone who finds the port can use it.
  • Prefer loopback plus a second hop. A teammate's local forward, or a reverse proxy with TLS and a password, is safer than GatewayPorts yes.
  • Prefer clientspecified to yes. With yes, every user's remote forwards become public whether they meant it or not.
  • Restrict on the server. PermitListen in sshd_config (globally or in a Match User block) limits which ports an account may open.
  • It bypasses the firewall on your side. A remote forward lets the server's users into your network at that one address and port. Check that your organisation allows it.

How do I clean up?

  1. Stop the client: Ctrl+C in the ssh window, close the PuTTY session, or in Transputty turn off the rule's switch or click Disconnect.
  2. On the server, confirm the port is gone: ss -ltn | grep 8080 should print nothing.
  3. Revert any GatewayPorts or firewall change you made only for this, and reload sshd.

A remote forward exists only while its SSH connection is up; nothing is left configured on the server afterwards. For the opposite direction, see local port forwarding, and for all three kinds side by side, the SSH port forwarding overview.

Common questions

What is the syntax of ssh -R?

ssh -R [bind_address:]port:host:hostport user@server. The first port is opened on the SSH server. host and hostport name the destination as seen from your computer, where the ssh command runs. So ssh -R 8080:localhost:3000 makes port 8080 on the server lead to port 3000 on your machine.

Why can I reach my remote forward from the server but not from other machines?

By default sshd binds remote forwards to the server's loopback address, so only programs on the server can connect. To accept outside connections, an administrator must set GatewayPorts to yes or clientspecified in sshd_config, and the server's firewall must allow the port. With clientspecified you also need a bind address in the command, such as -R 0.0.0.0:8080:localhost:3000.

What does GatewayPorts clientspecified mean?

GatewayPorts clientspecified lets the SSH client choose the address a remote forward listens on. With GatewayPorts no, the server always uses loopback. With yes, it always uses all interfaces. With clientspecified, -R 8080:localhost:3000 stays on loopback while -R 0.0.0.0:8080:localhost:3000 or -R '*:8080:localhost:3000' listens publicly. It is the most precise of the three settings.

Why does my remote forward return an empty reply on Windows?

The destination name may resolve to an address your service is not listening on. In our test with OpenSSH_for_Windows 9.5p2, a destination of localhost was tried over IPv6 only, so a dev server bound to 127.0.0.1 got no connection. Write the address the service actually uses: -R 8080:127.0.0.1:3000 for IPv4.

Is remote port forwarding safe?

Remote port forwarding is as safe as what you expose. A loopback remote forward is reachable only by users of the SSH server. A public one, with GatewayPorts, puts your local service on the internet with whatever authentication it has, which for a dev server is usually none. Keep forwards on loopback where possible and stop them when you are done.

How this tutorial was checked

2026-11-26: The basic ssh -R command was run on Windows 11 with the built-in client (OpenSSH_for_Windows 9.5p2): the server-side port listened on 127.0.0.1 and returned the local web server's page through curl.exe. The server was the Transputty project's own test SSH server on the same PC, not a real OpenSSH sshd. Also seen in that run: with the destination written as localhost, the Windows client tried IPv6 only. The other example commands were checked for syntax with ssh -G and not connected. Not run: every sshd_config change (AllowTcpForwarding, GatewayPorts, PermitListen) and the ss, systemctl and firewall commands, which are written from the OpenSSH manual. The PuTTY steps are from the PuTTY manual and were not run. The Transputty steps and its behaviour after a reconnect are described from the 0.1 source code and were not exercised in the app.

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.