rescueshell Start trial

SSH "Connection timed out" or "Connection refused"

These errors come before any login. Your machine tried to open a TCP connection and did not get one, so keys, passwords and host keys are not the problem yet. The messages point to different places, so read which one you have, then work through a short order of checks. Besides timed out and refused, this page covers Network is unreachable and No route to host.

This page describes standard OpenSSH and TCP behaviour. The commands are the usual ones, and wording can differ a little between versions and platforms. We did not run them against a particular network for this page.

If the log shows a line such as channel 2: open failed: connect failed: Connection refused after the session was already up, that is a different problem: a forward was used and its own connect to the target failed. See channel open failed: connect failed.

A line such as Timeout, server not responding. is not a connect-time timeout either. It comes after the session was open, when ssh ended it because keepalive probes went unanswered. See Timeout, server not responding.

A line such as Control socket connect(PATH): Connection refused names a file path, not a host and port. ssh failed to connect to a local control socket, so it is not a TCP refusal of the SSH port. See Control socket connect(PATH).

What each message means

ssh: connect to host server.example.com port 22: Connection timed out

Timed out means nothing answered on that path. The packets were dropped or never arrived, so the connection attempt gave up. macOS and some other systems word it Operation timed out. A wrong address, a firewall that drops traffic, a device that is off and a network that blocks outbound SSH all look like this.

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

Refused means a machine answered and said no: nothing accepted the TCP connection on that port. The path works. Usually sshd is not running, or it listens on another port or address. A firewall rule that rejects instead of dropping often gives the same message, though the wording of a rejection can differ, as the next two messages show.

ssh: connect to host server.example.com port 22: Network is unreachable
ssh: connect to host server.example.com port 22: No route to host

Network is unreachable and No route to host are a third kind. Like the other two they come before any login: the TCP connection was not opened. They are not timed out, where nothing answered, and not refused, where a machine answered and said no. They point to a path or routing problem reported while trying to reach that address, so keys, passwords and host keys are not the cause. This page does not map each message to one operating system behaviour or one cause, and the wording can differ between versions and platforms.

If you got as far as Permission denied, you are past this page. See SSH "Permission denied (publickey)". A changed host key warning also means the connection was made. Three look-alikes sit on other pages. Could not resolve hostname is a name problem, where ssh never had an address to try. A kex_exchange_identification or banner exchange line comes after a connection was accepted, so it is a handshake problem and not an unreachable host. And a network that blocks outbound SSH often looks like a timeout; see SSH blocked on a work network.

1. Check the name, address and port

Run the command again with -v:

ssh -v user@server.example.com

If you would like help reading the output, the ssh -v output reader checks a pasted log for well-known messages and links the matching guide. It runs in your browser and sends nothing.

The first lines show the address ssh resolved the name to and the port it is connecting to. Check both against what you expect. If the port is not 22, say so on the command line:

ssh -p 2222 user@server.example.com

Typing the address instead of the name rules DNS in or out. If the address works and the name does not, the name is the problem. If you connect through a Host entry in your SSH config, remember it can change the host name, port or user without it showing in the command you typed.

Then see which message you get. A limit on how long ssh waits saves time when the answer is a timeout, and a plain TCP test takes ssh out of the picture:

ssh -v -o ConnectTimeout=10 user@server.example.com
nc -vz -w 5 server.example.com 22

nc is netcat, and its flags differ a little between versions. The point is only to see whether a plain TCP connection to that port opens, without ssh in the way.

2. If it timed out

Nothing answered, so work along the path from your machine to the device.

3. If it was refused

The machine is reachable, so look at sshd on it. You need a console, someone on site or a session you already have open for this.

If this started right after you changed a firewall or sshd, see locked out of SSH after a firewall change for what gets you back in.

4. If it says unreachable or no route

Something on the way reported that it could not get to that address. The following are generic possibilities, not a diagnosis. We did not run them for this page, and the output differs between systems.

If these do not find it, the plain TCP test in step 1 is still a useful second opinion. This is a path problem to find, not an sshd problem, so sshd settings are not the place to start.

If the machine is a RescueShell device

A path that starts from the device can help when the internet cannot reach it. The agent on the device keeps an outbound HTTPS connection to our relay, so a missing port forward, a changed address or a stale hostname does not stop a shell from opening. That needs the agent installed and running first, and needs nothing inbound.

It does not help when sshd is not accepting a local connection on the device. The session still ends at the device's own SSH server, so a stopped sshd, a changed port or a ListenAddress without the loopback address stops it too, and so does a device that is off or has no network. The outbound connection is not a fix for a routing problem on the machine you typed on, and a device with no network of its own cannot make it. The lockout guide lists which lockouts a second path survives. If a shell does not open, see the shell does not open.

Try it on one device

The first device is free to try with no card, for 30 days from when it connects, not from when you sign up. Start a trial.