ssh: connect to host 10.20.0.2 port 22: Connection refused arrived in 0.14 seconds. With a firewall rule added on the server, the same command waited, and waited, and gave up 134 seconds later with Connection timed out. Both errors mean SSH didn’t connect, yet they point in opposite directions: the first proves the machine is up and answering, the second proves almost nothing at all. SSH’s other failure messages, “No route to host,” “Network is unreachable,” “Permission denied” and the oddly worded kex_exchange_identification, each narrow the problem in their own way. Every message below was produced on purpose in a two-machine test network, with the timing measured.
The test setup
Two machines, one cable
Two Linux network namespaces stood in for a client and a server, connected directly with addresses 10.20.0.1 and 10.20.0.2. The server ran OpenSSH’s sshd in some tests and an nftables firewall in others. Every connection attempt used OpenSSH’s client with BatchMode=yes, so it failed instead of asking for a password.
The question each error answers
An SSH connection is first a TCP connection to port 22, then an SSH protocol exchange, then authentication. Each error message belongs to one of those three stages. Knowing which stage failed is most of the diagnosis.

Connection refused: something answered “no”
Nothing listening on the port
With no sshd running, the attempt failed in 0.14 seconds:
ssh: connect to host 10.20.0.2 port 22: Connection refused
The server’s kernel received the connection request, found no program listening on port 22, and sent back a reset. That reply is the useful part: the machine is on, reachable, and the network path works.
sshd listening on the wrong address
A server running sshd but configured with ListenAddress 127.0.0.1 produced exactly the same refusal from the network. OpenBSD’s sshd_config manual says the default is “to listen on all local addresses,” so this only happens when someone has changed it, but the symptom looks identical to sshd not running at all. ss -tlnp on the server shows which address it’s bound to.
A firewall that rejects
An nftables rule with reject with tcp reset also gave Connection refused, in 0.01 seconds. A firewall can imitate a closed port on purpose, so “refused” means “something on that path said no,” usually the server itself.
Connection timed out: silence
A firewall that drops
Changing the rule to drop, which silently discards the packets, produced a very different result:
ssh: connect to host 10.20.0.2 port 22: Connection timed out
With -o ConnectTimeout=8, that took 8.02 seconds. Without it, it took 134 seconds.
Why 134 seconds
The client keeps resending its initial connection request, waiting longer each time. The Linux tcp(7) manual says the tcp_syn_retries setting has a default of 6, “which corresponds to retrying for up to approximately 127 seconds.” The test machine was set to that default, and the measured 134 seconds is the same retry schedule plus a few seconds of overhead.
What a timeout can mean
Silence has many causes, and a timeout can’t tell them apart: a firewall dropping packets anywhere along the path, a server that’s switched off, the wrong IP address on a network that doesn’t answer, a router not forwarding the port, or a cloud security group with no rule for port 22. The fix starts with finding out which hop goes quiet.
Failing faster
OpenBSD’s ssh_config manual describes ConnectTimeout as the timeout used when connecting, “instead of using the default system TCP timeout.” Setting it in ~/.ssh/config or on the command line turns a two-minute hang into a few seconds, which matters most in scripts that loop over many hosts. A host-specific block keeps the short timeout from affecting slow links elsewhere:
Host nas
HostName 10.20.0.2
ConnectTimeout 5
With that in ~/.ssh/config, ssh nas gives up after five seconds while every other host keeps the system default.

No route, network unreachable
No route to host
Two different situations produced No route to host. A firewall rule rejecting with an ICMP “administratively prohibited” message returned it in 0.01 seconds. An address on the local subnet with no machine behind it, 10.20.0.99, returned it after 3.08 seconds, the time the client spent asking the local network who had that address and getting no answer.
Network is unreachable
An address on a network the client had no route to at all, 10.30.0.5, failed instantly:
ssh: connect to host 10.30.0.5 port 22: Network is unreachable
This one never leaves the client. Its own routing table has nowhere to send the packet, which points to a missing default gateway, a VPN that isn’t connected, or a typo in the address.
Quick table
| Message | Time in test | What it proves |
|---|---|---|
| Connection refused | 0.01–0.14 s | Host reachable; nothing accepting on that port, or a firewall rejecting |
| Connection timed out | 8 s (with ConnectTimeout) / 134 s (default) | No answer at all; dropped, down, or wrong address |
| No route to host | 0.01 s or 3 s | Firewall rejection, or no machine at that local address |
| Network is unreachable | 0.01 s | The client has no route to that network |
When TCP works but SSH doesn’t
Something else on port 22
A test program that accepted connections on port 22 and closed them without speaking SSH produced:
kex_exchange_identification: read: Connection reset by peer
Connection reset by 10.20.0.2 port 22
kex_exchange_identification is the very first step of the SSH protocol, where both sides announce their versions. An error there means the TCP connection succeeded and the thing on the other end then hung up. In practice that points to a different service on the port, a security tool such as fail2ban cutting off the address, or sshd shedding load.
sshd turning connections away
The sshd_config manual describes MaxStartups, the limit on connections that haven’t finished logging in yet. Beyond it, “Additional connections will be dropped until authentication succeeds or the LoginGraceTime expires.” With the default of 10:30:100, sshd starts refusing some new attempts at random once 10 are pending, which is why a server under a password-guessing attack can occasionally reset legitimate logins.
The host key changed
One more failure happens after TCP works and before login. The client connected once and saved the server’s identity key in its known-hosts file. The server’s host key was then regenerated, as happens after a reinstall, and the next attempt stopped with a block of @ signs:
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
...
Offending ED25519 key in /tmp/sshd/kh:1
remove with:
ssh-keygen -f '/tmp/sshd/kh' -R '10.20.0.2'
Host key for 10.20.0.2 has changed and you have requested strict checking.
The message gives both possibilities itself: an attacker sitting in the middle of the connection, or a legitimately replaced key. On a home lab machine that was just rebuilt, the second is almost always the answer, and the ssh-keygen -R line it prints removes the old entry. On a machine nobody touched, it deserves a closer look before anyone types a password.

Permission denied: the network is fine
Getting all the way to authentication
With sshd running normally and no key loaded on the client:
test@10.20.0.2: Permission denied (publickey,keyboard-interactive).
That took 0.66 seconds and means everything up to authentication worked. The list in parentheses is the methods the server accepts; here, password login was turned off, so only keys and keyboard-interactive were offered.
Reading the verbose log
ssh -v shows how far a connection got. The test produced these lines in order:
debug1: Connecting to 10.20.0.2 [10.20.0.2] port 22.
debug1: Connection established.
debug1: Remote protocol version 2.0, remote software version OpenSSH_9.6p1 [package suffix trimmed]
debug1: Authentications that can continue: publickey,keyboard-interactive
“Connection established” means TCP worked. The remote version line means the SSH protocol started. “Authentications that can continue” means the problem is keys, usernames or server settings, not the network.
A troubleshooting order
Work up the stack
- “Network is unreachable”: check the client’s IP, gateway and VPN.
- “No route to host” or a timeout: check the address, whether the server is on, and every firewall and router between the two.
- “Connection refused”: the server is up; check that
sshdis running and listening on the right address and port. kex_exchange_identificationor a reset: something answered but isn’t speaking SSH properly; check fail2ban, MaxStartups and what’s bound to the port.- “Permission denied”: the network is fine; check the username, key and
sshd_configauthentication settings.
A test that skips SSH entirely
Bash can test a TCP port with no extra tools: timeout 3 bash -c 'echo > /dev/tcp/10.20.0.2/22'. With nothing listening, it printed Connection refused immediately. A hang until the timeout expires means the packets are being dropped, the same fork in the road as the SSH errors.
Sources
- tcp(7), Linux manual page: https://man7.org/linux/man-pages/man7/tcp.7.html
- ssh_config(5), OpenBSD manual pages: https://man.openbsd.org/ssh_config
- sshd_config(5), OpenBSD manual pages: https://man.openbsd.org/sshd_config
