skip to content

An OpenSSH remote forward (ssh -R 8080:localhost:3000) answers on the server itself but not from other hosts; why, and what changes that?

level: middleimportance: should knowfreq 24%

answer

  1. loopback is the safe default
  2. the server's switch, not the client's
  3. three values, one lets the client choose
  4. PermitListen narrows the listeners

basics

~20 s

sshd's GatewayPorts defaults to no, which holds every remote forward to the server's loopback address. GatewayPorts clientspecified honours the bind address the client asks for; GatewayPorts yes binds every remote forward to the wildcard address.

solid answer

~40 s

A remote forward's listener belongs to `sshd`, so the **server's** `GatewayPorts` decides where it binds. The default `no` forces remote forwards to be available to the local host only, so `-R 8080:localhost:3000` listens on the VM's loopback and other hosts get nothing. `clientspecified` lets the client choose, as in `-R 0.0.0.0:8080:localhost:3000`; `yes` binds every remote forward to the wildcard address whatever the client asked. Narrow it with `PermitListen`, which limits the addresses and ports a remote forward may listen on, and scope both in a `Match` block. The client's own `GatewayPorts` (or `-g`) is a different switch: it widens **local** (`-L`, `-D`) forwards on the laptop and has no effect on the server.

code

ini · 4 lines
ini
# sshd_config on demo-vm.example.net (Match blocks go after the global section)
Match User alice
    GatewayPorts clientspecified
    PermitListen 0.0.0.0:8080

go deeper

for a junior

Recall that forwarded ports listen on loopback by default and that, for -R, the server's GatewayPorts is the switch that changes it.

for a middle

Explain the three sshd values, how the bind address travels in the tcpip-forward request, and why the client's GatewayPorts and -g only touch -L and -D.

for a senior

Choose the least exposure: loopback plus a proxy, or clientspecified with PermitListen in a Match block, and mention that the published service now needs its own authentication.

for a principal

Treat a wildcard remote forward as publishing an unreviewed service on a server address, and decide who may do that and how it is noticed.

## The symptom For a demo, a developer publishes a webhook receiver on her laptop through a cloud VM: ```bash ssh -N -R 8080:localhost:3000 [email protected] ``` On the VM, a request to `localhost:8080` reaches the laptop. A colleague who browses to `demo-vm.example.net:8080` gets a refused connection. Nothing is broken: the listener exists, but only on the VM's **loopback** interface. ## Why loopback is the default A forwarded port is a hole into somebody's machine. If every `-R` forward listened on all interfaces, any user allowed to log in could publish a service from their laptop on the server's public address, and any `-L` or `-D` forward on a laptop in a café would be open to the café network. Both OpenSSH programs therefore bind forwarded listeners to loopback unless they are told otherwise, and the switch that tells them is called **`GatewayPorts`** in both files, with a different meaning in each. ## The server side: sshd's GatewayPorts For a remote forward the listener is opened by `sshd`, so the server's setting decides. `sshd_config(5)` gives three values: | `GatewayPorts` (sshd) | Effect on `-R` listeners | |---|---| | `no` (default) | forced to loopback, whatever address the client requested | | `yes` | forced to the wildcard address, whatever address the client requested | | `clientspecified` | bound to the address the client requested | On the wire, the client's `tcpip-forward` request always carries an *address to bind* (RFC 4254 section 7.1). With no bind address on the command line, OpenSSH's client asks for `localhost`; with `-R 0.0.0.0:8080:...` or `-R '*:8080:...'` it asks for a wildcard. Under `no` that request is not honoured, which is why adding `0.0.0.0` on the client alone changes nothing. The bind address a client can request on `-R` (and on `-L` and `-D` for its own listeners) takes these forms: - **omitted**: OpenSSH's client requests `localhost`, the loopback addresses only; - **`localhost`**, **`127.0.0.1`** or **`::1`**: loopback, for both families or one; - **empty or `*`**: every interface, all protocol families; - **`0.0.0.0`** or **`::`**: every IPv4 or every IPv6 address; - **a specific address**: one interface of the listening host. `yes` is the bluntest choice: it exposes *every* user's remote forwards on every interface, including ones they meant to keep private. `clientspecified` puts the decision in the request, so a forward stays on loopback unless the user explicitly asks for more. ## Narrowing what may listen: PermitListen `PermitListen` limits the addresses and ports a remote forward may listen on, as `port` or `host:port` entries, with `any` (the default) and `none`. Two details matter: - `GatewayPorts` can still restrict which addresses a permitted listener may use. - Because `ssh` requests a listen host of `localhost` when none was given, the man page warns that `localhost` is treated differently from explicit `127.0.0.1` and `::1` entries. Both `GatewayPorts` and `PermitListen` are accepted inside a `Match` block, so the wider setting can apply to one user or group only. ## The client side: a different switch with the same name `ssh_config`'s `GatewayPorts` (or `ssh -g`) only concerns listeners the **client** opens: local (`-L`) and dynamic (`-D`) forwards. With the default `no` they bind to the laptop's loopback; with `yes` they bind to the wildcard address. An explicit bind address on `-L` or `-D` (`localhost`, an address, empty or `*`) overrides it per forward. Setting it on the laptop does nothing for a `-R` listener on the server. ## Fixing the demo, in order of preference 1. **Keep the forward on loopback** and let something already exposed on the VM, such as a reverse proxy, forward to `127.0.0.1:8080`; the VM's existing access controls stay in charge. 2. **Allow this user to choose**: `GatewayPorts clientspecified` and a `PermitListen` entry for the one port, inside `Match User alice`, then run `ssh -N -R 0.0.0.0:8080:localhost:3000 [email protected]`. 3. **Avoid `GatewayPorts yes`** server-wide unless every remote forward on that host is meant to be public. Whichever you pick, the VM's own packet filter must also admit the port, and the receiver on the laptop is now reachable by anyone who can reach that address, so give it its own authentication.

  • Does adding `-g` to the `ssh -R` command line fix the demo?
    No. `-g` sets the client's `GatewayPorts`, which only affects listeners the client opens, the local and dynamic forwards on the laptop. A remote forward's listener is opened by `sshd`, so only the server's `GatewayPorts` (and `PermitListen`) decide where it binds.
  • Why is `clientspecified` usually preferred over `yes` on a shared server?
    `yes` forces every user's remote forwards onto the wildcard address, including forwards they meant to keep private on loopback. `clientspecified` keeps loopback as the default and widens a forward only when the client explicitly asks, and `PermitListen` can still restrict which ports may be widened.

  • Thinks adding 0.0.0.0 on the client is enough under the server's default.
  • Believes ssh -g changes where sshd binds a remote forward.
  • Says GatewayPorts yes is the safe way to share one demo port.
  • Assumes the server's GatewayPorts also governs -L listeners on the laptop.