Strovent SystemsSign in
Reference

SSH "remote port forwarding failed for listen port": why ssh -R is refused

The server would not listen on the port you asked for with ssh -R: an old session still holds it, forwarding is turned off, or the port is below 1024.

Warning: remote port forwarding failed for listen port 8080

You asked for a remote forward (ssh -R or RemoteForward in ssh_config), and the server said no. The login itself worked: by default ssh prints this warning and carries on with the session, without the forward. With ExitOnForwardFailure yes, the same refusal ends the connection instead, and the message starts with Error:.

The client only learns that the request failed, not why. The reason is on the server side.

Find the reason

Run the same command with -v. If the server has forwarding turned off, a line like this appears just before the warning:

debug1: Remote: Server has disabled port forwarding.

If you can read the server's log, it names the cause. A port that is already taken shows up as a bind error with Address already in use, followed by cannot listen to port: 8080. A request blocked by a rule shows up as Received request from ... to remote forward to host ... port 8080, but the request was denied.

Cause 1: the port is already in use on the server

This is the usual cause with reverse tunnels that reconnect. The old SSH session died without a clean close, for example after a network drop, but the server has not noticed yet. Its sshd process for that session is still alive and still listening on the port, so the new session cannot have it.

On the server, see what holds the port:

sudo ss -ltnp 'sport = :8080'

If it is an sshd process from your user's old session, end that process, or wait until the server drops the dead session. To make the server notice dead sessions sooner, set server alive checks in /etc/ssh/sshd_config:

ClientAliveInterval 30
ClientAliveCountMax 3

With these values the server closes a session whose client stopped answering after about 90 seconds, which frees the port. See SSH keep-alives and idle timeouts.

If another program owns the port, pick a different port for the forward.

Cause 2: forwarding is turned off on the server

Any of these settings makes the server refuse every remote forward:

  • AllowTcpForwarding no or AllowTcpForwarding local in sshd_config. local allows only ssh -L forwards. The default is yes.
  • DisableForwarding yes in sshd_config, which turns off all forwarding.
  • no-port-forwarding or restrict in front of your key in the server's authorized_keys file.

Check what the server actually uses with sudo sshd -T | grep -i forwarding. Match blocks can change these settings for some users, so test with sudo sshd -T -C user=user1,host=client.example.com,addr=192.0.2.10 for a specific login.

Cause 3: the port is not on the allowed list

PermitListen in sshd_config, or permitlisten="..." on the key in authorized_keys, limits which ports a remote forward may use. A port outside the list is refused, and the server log says the request was denied. Use a port from the list, or ask the administrator to add yours.

Cause 4: a port below 1024

The ssh manual says privileged ports can be forwarded only when you log in as root on the remote machine. Forward a high port such as 8080 instead, and point the service there.

The forward works, but other machines cannot connect

That is a different problem. By default the server binds remote forwards to its loopback address only, so the port answers on the server itself but not from the network. GatewayPorts in sshd_config controls this: no (the default) keeps loopback only, yes binds to all addresses, and clientspecified lets the client choose, for example ssh -R '*:8080:localhost:3000' [email protected], where * means all addresses. See Port forwarding: local, remote, dynamic.

Make scripts and tunnels fail loudly

For an unattended tunnel, a session without its forward is useless. Add -o ExitOnForwardFailure=yes, so ssh exits when a forward cannot be set up and whatever restarts it gets a chance to try again. See Keep an SSH tunnel running: autossh, ServerAliveInterval and systemd.

Sources

Spotted a mistake? Email [email protected].