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 noorAllowTcpForwarding localinsshd_config.localallows onlyssh -Lforwards. The default isyes.DisableForwarding yesinsshd_config, which turns off all forwarding.no-port-forwardingorrestrictin front of your key in the server'sauthorized_keysfile.
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.
Related
- Port forwarding: local, remote, dynamic
- SSH port forwarding through a jump host: -J with -L
- Keep an SSH tunnel running: autossh, ServerAliveInterval and systemd
- SSH keep-alives and idle timeouts
- SSH "channel 3: open failed: connect failed: Connection refused" with port forwarding
- SSH error messages, what each one means
- bind_address in ssh -L and ssh -R: localhost, * and GatewayPorts
- ssh -R with port 0: let the server pick the port, and find out which one
Sources
- ssh(1), option -R
- ssh_config(5), ExitOnForwardFailure and RemoteForward
- sshd_config(5), AllowTcpForwarding, DisableForwarding, GatewayPorts, PermitListen, ClientAliveInterval
- ss(8), filters such as sport
- sshd(8), -T and -C, and the authorized_keys options no-port-forwarding, permitlisten and restrict
- OpenSSH portable source: ssh.c (client messages), serverloop.c and channels.c (server checks and log lines)