Troubleshooting

SSH tunnel "Connection refused" and "channel N: open failed: connect failed"

A refusal through an SSH tunnel comes from one of three places: the SSH server's own port, the local port your app connects to, or the destination the server tries to reach. The wording and where it appears tell you which.

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

Before you start

  • The exact ssh command (or tunnel settings) you are using
  • The exact error text and where it appeared: the ssh window or your application
  • A way to run commands on the SSH server, if the destination turns out to be the problem

Short answer

"Connection refused" through an SSH tunnel has three possible sources. If ssh itself prints it with port 22, the SSH server is not accepting connections. If your application reports it for 127.0.0.1, the tunnel is not listening on that local port. If the ssh window prints channel N: open failed: connect failed: Connection refused, the SSH server could not reach the destination.

The last section explains how Transputty, an SSH tunnel manager for Windows 11, reports the same failures. Transputty is in development and has no public build yet; see release status.

The exact error messages

From ssh, before the tunnel exists:

ssh: connect to host bastion.example.com port 22: Connection refused

From the application, when it connects to the local end of the tunnel (psql and mysql on Windows shown; 10061 is the Windows code for "refused"):

psql: error: connection to server at "127.0.0.1", port 5433 failed: Connection refused (0x0000274D/10061)
    Is the server running on that host and accepting TCP/IP connections?
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3307' (10061)

In the ssh window, while the tunnel is running and an application connects:

channel 3: open failed: connect failed: Connection refused
channel 3: open failed: administratively prohibited: open failed

The number after channel varies. With the last two, the application usually does not say "refused" at all. It reports that the server closed the connection, for example server closed the connection unexpectedly from psql or ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet' from mysql.

Quick diagnosis

Symptom Likely cause Check
ssh: connect to host … port 22: Connection refused sshd is not running, listens on another port, or a firewall rejects the port ssh -v, the right -p, the server's status
The application is refused at 127.0.0.1:PORT No tunnel is listening there: ssh exited, the forward failed to bind, or the port number differs List listeners on the port
Refused on localhost, fine on 127.0.0.1 The tunnel is bound to IPv4 only and the app tried ::1 Use 127.0.0.1
channel N: open failed: connect failed: Connection refused The SSH server reached the destination host but nothing listens on that port or interface nc -vz from the SSH server
channel N: open failed: administratively prohibited: open failed sshd policy forbids the forward AllowTcpForwarding, PermitOpen, key options
The app's connection is dropped with no line in the ssh window Same destination problems, with a server that does not report the reason Test the destination from the SSH server

Cause 1: the SSH server refuses the connection

This fails before any forwarding is involved, so think of it as a plain SSH login problem.

ssh -v -p 22 deploy@bastion.example.com

The verbose output ends with connect to address 203.0.113.10 port 22: Connection refused. Common reasons: the SSH service is stopped; it listens on a non-standard port (add -p 2222, or Port in ssh_config); the name resolves to the wrong machine; or a firewall actively rejects the port. A refusal that starts suddenly on a server that worked yesterday often means sshd is down or a protection tool has blocked your address after failed logins.

Cause 2: nothing is listening on the local port

Your application connects to a port on your own computer, and nobody is there. Check what is listening.

Windows:

netstat -ano | findstr :5433

macOS and Linux:

lsof -iTCP:5433 -sTCP:LISTEN

No output means no tunnel. The usual reasons:

  • ssh is no longer running. A plain ssh -N -L removes the local port the moment the session ends, for any reason. See SSH tunnel keeps disconnecting.
  • The forward never started. If the local port was taken, ssh printed a bind warning, logged in anyway and kept running without the forward. Scroll up in the ssh window, and see Local port already in use. Adding -o ExitOnForwardFailure=yes makes ssh stop instead.
  • The numbers differ. In -L 5433:db.internal:5432 the application must use 5433, the first number. Using 5432 is the classic slip.

The localhost trap: IPv6 and IPv4

localhost is two addresses: 127.0.0.1 and ::1. With -L 5433:db.internal:5432, OpenSSH listens on both. With an explicit IPv4 bind, -L 127.0.0.1:5433:db.internal:5432, or with a tunnel tool that binds IPv4 only, ::1 stays closed:

psql: error: connection to server at "::1", port 5433 failed: Connection refused (0x0000274D/10061)

Many clients try both addresses and connect without comment. Some try only the first one the system returns, and on current Windows that can be ::1. If an application is refused on localhost, give it 127.0.0.1.

Cause 3: the SSH server cannot reach the destination

The tunnel is up, and ssh prints a channel N: open failed line each time the application connects. The destination in a local forward is opened by the SSH server, so test it from there:

ssh deploy@bastion.example.com
nc -vz db.internal 5432

If nc is refused as well, the tunnel is not at fault. Work through these:

  • Wrong host from the server's point of view. localhost in -L 5433:localhost:5432 means the SSH server itself. If the database is on another machine, name that machine.
  • The service listens on another interface. A database bound to 127.0.0.1 accepts connections only from its own host; a bastion cannot reach it. On the database host, ss -ltn shows the bound address. Either forward through an SSH server on the database host with destination 127.0.0.1, or change the service's listen address.
  • Wrong port, or the service is down.
  • A firewall on the destination rejects the SSH server's address. A reject shows as refused; a silent drop shows as a hang followed by connect failed: Connection timed out.

Run ssh with -v to watch each attempt. A healthy forward logs these lines, and a failed one adds the open failed line right after:

debug1: Local connections to LOCALHOST:5433 forwarded to remote address db.internal:5432
debug1: Local forwarding listening on 127.0.0.1 port 5433.
debug1: Connection to port 5433 forwarding to db.internal port 5432 requested.
debug1: channel 2: new direct-tcpip [direct-tcpip] (inactive timeout: 0)

If the third line never appears when your application connects, the application is not reaching the tunnel at all, and you are back at cause 2.

Cause 4: the server prohibits forwarding

channel 2: open failed: administratively prohibited: open failed

sshd refused by policy, without trying the destination. Its own log records refused local port forward: originator 127.0.0.1 port 61365, target db.internal port 5432. Ask the administrator, or check with root on the server:

sudo sshd -T | grep -i -E 'allowtcpforwarding|permitopen'
  • AllowTcpForwarding no (or remote) disables local forwards. It may sit inside a Match block that applies only to your user or group.
  • PermitOpen host:port limits forwards to the listed destinations. The destination in your -L must match an entry as written.
  • In ~/.ssh/authorized_keys, the options restrict, no-port-forwarding or permitopen="host:port" on your key have the same effect for that key only.

After a change to sshd_config, sshd must be reloaded and your tunnel reconnected.

How Transputty reports these failures

Transputty uses the same protocol, so the causes are identical. This is what version 0.1 shows, according to its source code.

  • SSH server refuses (cause 1). The engine records error code CONNECT with the message "connection refused". The connection shows the pill "Reconnecting" with a countdown and the reason "Server unreachable", and a callout titled "Can't reach bastion.example.com" with a Retry now button. Transputty retries with backoff. The editor's Test connection button checks that the server answers without logging in.
  • Local port (cause 2). The listener stays open while Transputty reconnects (the rule pill reads "Port held"), and a client that connects during a reconnect is held for up to 10 seconds instead of being refused. If the tunnel is not back by then, the client's connection is reset. This has a cost: a held port passes a plain port check such as Test-NetConnection although the tunnel is down, so test with a real request. If the connection is switched off, the port is closed and applications are refused as with any tunnel. Rules listen on 127.0.0.1, so the IPv6 note above applies: use 127.0.0.1 in the application.
  • Destination or policy (causes 3 and 4). The engine classifies these as CHANNEL_OPEN_FAILED, with the message "the SSH server could not connect to db.internal:5432" or "the SSH server does not allow TCP forwarding (AllowTcpForwarding)", and resets the application's connection. In version 0.1 the rule itself stays "Active", because its listener is healthy, so do not read a green rule as proof that the destination is reachable. When a jump host cannot reach the next hop, the whole connection fails with the tag "Destination unreachable".
A refused SSH connection: the pill 'Reconnecting' with a countdown, the reason 'Server unreachable', the rule's 'Port held' pill, and the callout 'Can't reach home.fernhill.example' with 'Retry now'. The app is connected to local test servers with fictional host names.

For the concepts behind these messages, see the local port forwarding tutorial and the SSH port forwarding guide.

Common questions

What does channel 3: open failed: connect failed: Connection refused mean?

The SSH connection itself is fine. The SSH server tried to open the destination named in your forward, for example db.internal:5432, and that host refused the connection. Nothing is listening on that address and port as seen from the SSH server, or the service is bound to a different interface. Test the destination from the SSH server.

What does open failed: administratively prohibited mean?

The SSH server's configuration refused the forward before trying to connect. The usual causes are AllowTcpForwarding no in sshd_config, a PermitOpen list that does not include your destination, or a restrict or permitopen option on your key in authorized_keys. The server administrator has to allow the forward; nothing on the client can override it.

Why is localhost refused when 127.0.0.1 works?

localhost can resolve to the IPv6 address ::1 first. If the tunnel listens only on the IPv4 address 127.0.0.1, an application that tries only ::1 is refused. Use 127.0.0.1 in the application, or make the tunnel listen on both addresses. Many clients try both and never show the problem.

Is connection refused the same as a timeout?

No. Refused means a machine answered immediately and said nothing is listening on that port, so the address is reachable and the port is closed. A timeout means no answer came back at all, which points to a firewall silently dropping packets, a wrong address or a host that is down.

The tunnel starts without errors, so why is my app refused?

ssh does not test the destination when the tunnel starts. A local forward only opens a listening port on your computer; the SSH server tries the destination each time an application connects. A wrong destination therefore shows up later, as a channel open failed line in the ssh window and a dropped connection in the application.

How this tutorial was checked

2026-11-26: Reproduced on Windows 11 with the built-in ssh client (OpenSSH_for_Windows 9.5p2), a real OpenSSH server (OpenSSH for Windows 10.4p1) on the same PC, and psql 18 and mysql 8.4.7 as the applications: ssh refused on a closed port; the applications refused when no tunnel was listening, and when only 127.0.0.1 was listening but ::1 was used; 'administratively prohibited' with AllowTcpForwarding no and with a PermitOpen that did not match, plus the server log line; the ssh -v lines shown; and the application errors when the destination was closed. Not reproduced: the line 'open failed: connect failed: Connection refused' itself. A Linux OpenSSH server sends it; the Windows server used here closed the channel without that line. No Linux server was tested, and the nc, ss and sshd -T commands were not run. The Transputty behaviour is described from the 0.1 source code and was not observed 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.