The usual first move after ssh: connect to host 10.20.4.17 port 22: Connection refused is to open the cloud console and inspect security groups, then regenerate a key, then ask whether the VPN is up. Almost none of that is relevant. A security group in AWS or a firewall rule in Google Cloud drops packets silently, and keys are checked only after a TCP connection exists. "Refused" means something answered your connection attempt and said no.
That one fact cuts the search space roughly in half, and getting it right early saves real time in an incident. Teams that confuse refused with timed out spend 30 minutes in network dashboards on a problem that lives in one process on one server. The lessons below come from treating the error message as evidence, and they cover what the message means, which machine sent it, the server-side causes in the order they tend to appear, and the mistakes that turn a small fix into a lockout.
Lesson one: refused is an answer, so first find who answered
TCP connections start with a SYN packet from the client. What comes back decides the error your SSH client prints, and each outcome points at a different layer. Three definitions are worth keeping straight.
Connection refused: the client received a TCP reset (RST) in reply to its SYN, or an ICMP "port unreachable" message, which Linux reports the same way. Some machine on the path is reachable and actively rejected the connection.
Connection timed out: nothing came back at all. The SYN was dropped by a firewall, a security group, a routing problem or a host that is down, and the client gave up after its retries.
No route to host: the client or a router on the path got an ICMP "host unreachable" message, or the local network could not resolve the target's link-layer address. The packet never reached a machine that could answer for port 22.
Those definitions map onto the messages you see in practice, and onto the first check for each.
| Client message | What happened on the wire | Usual layer | First check |
|---|---|---|---|
| Connection refused | RST or ICMP port unreachable returned | sshd not listening, host firewall REJECT, ban tool | ss -tlnp on the server, firewall rules |
| Connection timed out | No reply to SYN | Cloud firewall, security group, routing, host down | Security group, route, instance state |
| No route to host | ICMP host unreachable, or no ARP reply | Local network, wrong subnet, host down | IP address, subnet, gateway |
| kex_exchange_identification: Connection closed by remote host | TCP connected, then sshd or a proxy closed it | sshd limits, intrusion prevention, wrong service on port | Server logs, MaxStartups, what listens on 22 |
| Permission denied (publickey) | Full SSH handshake, authentication failed | Keys, user, authorized_keys | ssh -v, server auth log |
Timing is the fastest way to tell the first two rows apart, and a worked example shows how much signal it carries. Run time nc -vz 10.20.4.17 22 from your laptop. Suppose it returns "Connection refused" in 0.038 seconds, which is about the round-trip time to that data centre. The reset came back immediately, so a machine on the path answered at network speed. Now suppose instead it hangs and fails after about 127 seconds.
On Linux, net.ipv4.tcp_syn_retries defaults to 6, and the retransmission timer doubles from 1 second: 1 + 2 + 4 + 8 + 16 + 32 + 64 = 127 seconds before the kernel gives up. A failure at 127 seconds is a drop, and you should look at firewalls and routes.
A failure at 38 milliseconds is a reject, and you should look at the target host or a device right in front of it. SSH's own ConnectTimeout option can shorten the wait, but the distinction stays the same.
nmap -Pn -p 22 10.20.4.17 gives the same answer in different words. A port reported as "closed" returned a reset. A port reported as "filtered" returned nothing, or an ICMP error that suggests a filter.
The next question is which machine sent the reset. Usually it is the target. Sometimes it is a load balancer, a NAT gateway with no matching forward rule, a corporate proxy or an old host that now owns a recycled IP address. Two cheap checks help. First, confirm the address: ssh -v prints the IP it actually connects to, and a stale DNS record or an outdated ~/.ssh/config entry pointing at last month's VM is common after a rebuild.
Second, compare TTLs. Capture the reset with tcpdump -ni any 'tcp port 22' on the client, then compare the TTL in the reset with the TTL of a ping reply from the target. Linux hosts start at 64. If ping replies arrive with a TTL of 57 and the reset arrives with 63, the reset came from a device one hop away from you, not from the server seven hops away.
Lesson two: on the server, the cause is usually one of six things
Once you know the target host itself refused the connection, you need a way in that does not depend on SSH. On AWS, the EC2 Serial Console or Systems Manager Session Manager works without sshd. Google Cloud has a serial console as well, and most virtualisation platforms and bare-metal servers have an out-of-band console. EC2 Instance Connect will not help here, because it pushes a temporary key and then still uses SSH on port 22.
With a shell on the box, the causes below account for the large majority of refusals. They are listed in the order worth checking, because the first two take seconds.
The first is that sshd is not running. On Debian and Ubuntu the unit is called ssh; on RHEL, Fedora, Rocky and Alma it is sshd. systemctl status ssh or systemctl status sshd shows whether it is active, and journalctl -u ssh -n 50 shows why it stopped. A minimal cloud image or a container may not have an SSH server installed at all, in which case which sshd returns nothing and the fix is to install openssh-server.
The second is that sshd failed to start after a configuration change. A single typo in /etc/ssh/sshd_config or a file under /etc/ssh/sshd_config.d/ stops the daemon from starting. sshd -t checks the configuration and prints the bad line; run it after every edit and before every restart. A related failure is "sshd: no hostkeys available -- exiting", which appears when an image build deleted /etc/ssh/ssh_host_* to avoid cloning keys and nothing regenerated them on first boot. ssh-keygen -A creates any missing host keys.
The third is that sshd is listening somewhere else. ss -tlnp | grep -E 'ssh|:22' lists the sockets. A Port 2222 line, a ListenAddress 127.0.0.1 left over from a hardening guide, or an IPv6-only bind will all produce a refusal on port 22 from your address.
On Debian and Ubuntu, drop-in files in sshd_config.d are included near the top of the main file, and for most keywords sshd keeps the first value it reads, so a drop-in quietly overrides what you edited below it. Port is an exception: multiple Port lines add listeners, so a stale drop-in can leave an extra port open while the one you expect is missing.
The fourth applies to recent Ubuntu releases specifically. Since Ubuntu 22.10, SSH is socket-activated by default: systemd's ssh.socket listens on the port and starts the daemon on the first connection. On those systems, changing Port in sshd_config and restarting ssh.service is not enough. You run systemctl daemon-reload so the socket unit picks up the change, then systemctl restart ssh.socket. Skipping the reload is a reliable way to make SSH listen on the old port, or on none, after a "simple" port change.
The fifth is a host firewall configured to reject. ufw with a reject rule, firewalld, or an iptables or nftables rule with a REJECT target all send resets or ICMP errors, which the client reports as refused. ufw status verbose, firewall-cmd --list-all and nft list ruleset show the current rules. Note the difference: a DROP rule produces a timeout, a REJECT rule produces a refusal, so the error message itself tells you which kind of rule to look for.
The sixth is a ban tool. fail2ban's standard iptables actions reject banned addresses with ICMP port unreachable by default, so a developer whose laptop retried an old password a few times gets "Connection refused" while everyone else connects normally. fail2ban-client status sshd lists banned IPs, and fail2ban-client set sshd unbanip 203.0.113.25 lifts one. sshguard and CrowdSec behave similarly, with their own commands.
Containers deserve a short note, because they produce refusals that look like server faults. If a container publishes -p 2222:22, connecting to port 22 on the host reaches the host's own sshd, or nothing, not the container. If the container has no SSH server, which is normal and usually correct, the right tool is docker exec or kubectl exec, not SSH.
Lesson three: trace one refusal from laptop to socket
An illustrative incident shows how the checks chain together. A deployment job fails at 09:12 with Connection refused against 10.20.4.17, a VM rebuilt the previous evening from a new golden image. Nothing else in the subnet is affected.
- From the CI runner,
time nc -vz 10.20.4.17 22fails in 0.004 seconds. The runner sits in the same subnet, so a 4-millisecond reply means the target or its local firewall answered. ssh -v deploy@10.20.4.17confirms the client is connecting to the expected IP, and DNS returns the same address, so a recycled IP or stale record is unlikely.nmap -Pn -p 22,2222 10.20.4.17shows 22 as closed and 2222 as closed, so this is not a moved port.- The engineer opens the serial console and logs in with the break-glass account.
systemctl status sshshows the unit as failed, with an exit code of 255 at 22:41 the previous evening.journalctl -u ssh -n 30shows the line "sshd: no hostkeys available -- exiting". The new image build had removed host keys to avoid shipping identical keys to every VM, and the first-boot step that regenerates them had been dropped from the build script.ssh-keygen -Acreates the RSA, ECDSA and Ed25519 host keys,sshd -treturns nothing, andsystemctl restart sshbrings the daemon up.ss -tlnpnow shows sshd on0.0.0.0:22and[::]:22, and the CI job connects at 09:31. Because the host keys are new, the runner's known_hosts entry has to be updated, which is expected and should be done through your normal host-key distribution, not by turning off strict checking.
Total time to fix: 19 minutes, most of it getting console access. Had the team started with the security group, the first 15 of those minutes would have gone to rules that cannot produce a refusal at all.
The trace also shows where packet capture would have helped if the log had been empty. Running tcpdump -ni eth0 'tcp port 22' on the server while retrying from the runner splits the problem in two. If SYNs arrive and the server's kernel sends RSTs, nothing is listening on that address and port.
If SYNs arrive and nothing goes back, a local DROP rule is eating them, though in that case the client would have timed out rather than been refused. If no SYN arrives at all while the client still sees a reset, a device between them is answering on the server's behalf, which brings you back to the TTL comparison from lesson one.
The follow-up mattered more than the fix. The team added ssh-keygen -A to the first-boot script, added sshd -t to the image build's smoke tests, and added a check that the built image actually accepts an SSH connection before it is promoted. Those three lines cost about 10 minutes and remove this whole class of incident.
Lesson four: the mistakes that turn a refusal into a lockout
Most SSH outages that last hours were not caused by the original fault. They were caused by the fix, applied over the only session the engineer had. The patterns repeat often enough to list.
- Restarting sshd after an edit without running
sshd -tfirst. If the config is broken, the daemon stops, and on a host with no console access you are now locked out. - Working with a single open session. Keep the current session open and test the change from a second terminal before closing anything; an existing session survives an sshd restart, a new connection does not.
- Changing the SSH port before opening it in the firewall. With ufw,
ufw allow 2222/tcpcomes first, then the sshd change, then the test, and only then removal of the old rule. - Running
ufw enableon a remote host beforeufw allow OpenSSH. The default incoming policy is deny, so the next connection from your address is refused or dropped. - Forgetting socket activation on Ubuntu 22.10 and later, so the port in
sshd_configand the port systemd actually listens on drift apart. - Leaving a stale
PortorListenAddressin a drop-in file undersshd_config.d, then editing the main file and wondering why nothing changed. - Deleting host keys in a golden image without a first-boot step that recreates them, as in the trace above.
- Testing a password policy from your own laptop until fail2ban bans you, then debugging the "outage" for an hour. Check the ban list before anything else when only one person is affected.
- Turning off
StrictHostKeyCheckingto silence a warning after a rebuild, which removes the protection that tells you a host has really been replaced. - Opening port 22 to
0.0.0.0/0in a security group "to test". It cannot fix a refusal, and it exposes the host to the constant background scanning that every public IPv4 address receives.
Some of these deserve a policy rather than a reminder. A pre-commit or CI check that runs sshd -t -f against configuration changes, a rule that remote firewall changes go through configuration management with an automatic rollback, and documented break-glass console access for every environment together remove most of the risk. On a fleet of 40 or 50 servers, a broken sshd config pushed everywhere at once is a real outage, and a validation step costs seconds per run.
There are also cases where you should stop troubleshooting and do something else. If the host is a disposable instance built from an image or a Terraform module, and you can replace it in 5 minutes, replace it and fix the image instead of repairing one machine by hand.
If the server belongs to another team or a client, report the evidence (refused in 4 ms, closed on nmap, timestamp) and let the owner fix their own sshd, rather than asking for temporary root access. And if the only route to a fix is disabling the host firewall or exposing port 22 to the internet, the honest answer is that you need console access first.