Troubleshooting guide
SSH connection refused: how to fix it
Connection refused on port 22 means the host answered but nothing accepted the connection there. Either sshd is not listening, or it is listening elsewhere.
3 min read11 sectionsUpdated
On this page
Common causes
- sshd stopped or failed to start after a configuration change.
- A non-standard port.
- A firewall rule on the host or in a security group.
- fail2ban after several failed logins.
- The instance is still booting, or was re-created with a different IP.
- A full disk, which can prevent sshd from working normally.
# What does the client see?
ssh -vvv -p 22 user@host 2>&1 | tail -8
nc -vz host 22
# On the server (via cloud console)
systemctl status ssh --no-pager
sudo sshd -t
sudo ss -ltnp | grep sshd
sudo fail2ban-client status sshd
sudo ufw statusFixes
Run sudo sshd -t to validate the configuration, fix any error, and restart. Remove your IP from fail2ban with sudo fail2ban-client set sshd unbanip YOUR_IP. Open the port in the firewall and security group. When you change SSH settings, keep the current session open and test from a new one before you log out.
If you are locked out: use your provider's serial console, or attach the disk to another instance to repair the configuration.
Catch it automatically
A TCP port check on your SSH port tells you within a minute if access is lost, ideally from an allowed source. Restrict SSH by IP or use a bastion or a tunnel, because a port open to the internet will be scanned constantly.
What is the difference between refused and timed out for SSH?
Refused means the host answered and rejected the connection. Timed out suggests a firewall dropping packets or a host that is off.
Why did SSH stop after I edited sshd_config?
A syntax error stops sshd from restarting. Run sshd -t, and test in a second session.
Does changing the SSH port improve security?
It reduces log noise but does not replace key-based login and restricted access.
Check it now, free
- Open port checker: Is a TCP port open, closed or filtered from the internet?
Related reading
- PostgreSQL connection refused: how to fix it: Fix PostgreSQL connection refused on port 5432: server not running, listen_addresses, pg_hba.conf, Docker hostnames and firewalls, with commands for every case.
- Redis connection refused: how to fix it: Fix Redis connection refused on port 6379: server stopped, bind address, protected mode and Docker networking, with redis-cli and ss commands for each cause.
- Cloudflare error 522: connection timed out: Fix Cloudflare error 522 "Connection timed out": the origin did not complete the TCP connection in time. Overloaded servers, dropped packets and wrong DNS.
- HikariCP "Connection is not available": how to fix it: Fix HikariCP Connection is not available, request timed out: use leak detection, size the pool sensibly, avoid long transactions and read pool metrics.
- "Your connection is not private": how to fix it: Fix "Your connection is not private" (NET::ERR_CERT_*): expired or mismatched certificates, wrong device clocks, missing intermediates and Wi-Fi portals.
- NET::ERR_CERT_DATE_INVALID: how to fix it: Fix NET::ERR_CERT_DATE_INVALID ("Your connection is not private"): an expired or not-yet-valid certificate, or a wrong device clock. Check, renew, reload.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.