Strovent SystemsSign in
Reference

SSH "channel 3: open failed: connect failed: Connection refused" with port forwarding

The tunnel is up, but the SSH server could not reach the target host and port in ssh -L or -D. That host name is looked up on the server, not on your side.

channel 3: open failed: connect failed: Connection refused

You started a local forward, for example ssh -L 5432:localhost:5432 [email protected]. The login worked. Then a program connected to port 5432 on your computer, the connection closed at once, and ssh printed this line in its window. It prints one line for every failed attempt.

What the line means

When a program connects to your local port, ssh asks the server to open a connection to the target in your -L option, here localhost port 5432. The server tries, fails, and sends back a reason. ssh prints that reason.

The target is reached from the server. In -L 5432:localhost:5432, localhost is the server itself, not your computer. A name in that position is looked up by the server, with the server's DNS and hosts file.

The channel number does not matter for the fix. It numbers the channels inside this SSH connection: the shell session usually has an early number, and each forwarded connection gets a new one.

"connect failed: Connection refused"

The server reached the target address, and nothing was listening on that port, or a firewall there rejected the connection. Common reasons:

  • The service is not running, or it uses a different port than the one in your -L option.
  • The service listens on another address. A database that listens only on the server's network address does not answer on localhost, and one that listens only on 127.0.0.1 does not answer on the network address. Put the address it listens on in the forward, for example -L 5432:10.0.0.5:5432.
  • You meant a different machine. If the service runs on another host behind the SSH server, name that host in the forward. The SSH server must be able to reach it.

To see which addresses the service listens on, run this on the server:

sudo ss -ltnp 'sport = :5432'

The server tries every address a name resolves to (for example both the IPv6 and IPv4 address of localhost) and reports the failure only when all of them fail. Its log then shows connect_to localhost port 5432: failed.

Other "connect failed" reasons

The text after connect failed: is the error the server's system returned:

  • "Connection timed out" or "No route to host": the server could not reach the target at all. A firewall between them is dropping the traffic, or the address is wrong.
  • "Name or service not known" (or another name-lookup error): the server could not resolve the host name in your forward. Use a name the server can resolve, or an IP address.

"administratively prohibited: open failed"

channel 3: open failed: administratively prohibited: open failed

This time the server refused to try. Forwarding is turned off or limited for you:

  • AllowTcpForwarding no or AllowTcpForwarding remote in sshd_config. remote allows only ssh -R forwards.
  • DisableForwarding yes in sshd_config.
  • no-port-forwarding or restrict in front of your key in authorized_keys.
  • PermitOpen in sshd_config, or permitopen="..." on the key, lists the destinations you may reach, and yours is not on the list. These lists compare names literally, with no lookup: if only 127.0.0.1:5432 is allowed, a forward to localhost:5432 is refused. Use exactly the form on the list.

The server's log names the cause: refused local port forward: ... target localhost port 5432 when forwarding is turned off, or Received request from ... to connect to host localhost port 5432, but the request was denied. for a PermitOpen or permitopen refusal. Check the server's settings with sudo sshd -T | grep -i -e forwarding -e permitopen.

The same line with ssh -D

A dynamic forward (ssh -D, a SOCKS proxy) opens the same kind of channel for each site a program asks for, so a site the server cannot reach produces the same line.

Remote forwards fail the other way round

With ssh -R, the connection to the target is made by your side, not by the server. If it fails, your ssh prints connect_to localhost port 3000: failed. instead, and the fix is on your computer: start the service, or correct the target in -R. If the server refuses the -R request itself, see SSH "remote port forwarding failed for listen port".

Sources

Spotted a mistake? Email [email protected].