What the message looks like
ssh: connect to host server.example.com port 22: Connection refusedCommon causes
- sshd is not running, for example after an update or a failed config test.
- The service listens on a different port, such as
Port 2222, while the client uses 22. - sshd listens on a single address or IPv6 only, so the IPv4 address has no listener.
- A firewall rejects with REJECT. DROP would produce a timeout instead.
- The hostname points to a different server where SSH is not running.
Check
- Status and listener
systemctl status ssh --no-pager | head -n 5; ss -lntp | grep ssh - Configured ports and addresses
grep -iE '^\s*(Port|ListenAddress|AddressFamily)' /etc/ssh/sshd_config - Test the port from the client
nc -vz server.example.com 22 - Check bans on the server
fail2ban-client status sshd 2>/dev/null | tail -n 5
Fix
- Start the service and read the journal:
systemctl start ssh, thenjournalctl -u ssh -n 30. - If the port changed, update client, firewall and documentation – both sides must match.
- With fail2ban, unban your own IP:
fail2ban-client set sshd unbanip IP. - For IPv6-only listening set
AddressFamily anyor check the IPv4 address. - Run
sshd -tbefore restarting so a broken config does not lock everyone out.
Typical scenario
On a freshly installed Ubuntu 24.04 the port was set to 2222 in /etc/ssh/sshd_config and systemctl restart ssh was run. The server still listens on 22 and not on 2222, because ssh.socket starts the service through socket activation there and does not read the port line. From outside you get Connection refused. First check with ss -tlnp | grep sshd which ports are really listened on.
FAQ
Refused versus timeout?
Refused means the packet arrived and was rejected, so there is no listener. Timeout means no answer at all: firewall DROP or a wrong address.
I locked myself out – now what?
Log in through the provider console, check the service and config, and unban your IP. Fail2ban only affects new connections.
How do I change the SSH port safely?
Keep the existing session open, open the new port in the firewall first and test it in a second terminal before closing the first session. On systems with socket activation you must also adjust the port in the ssh.socket unit, not only in the configuration file.
Can Fail2ban cause Connection refused?
Yes, depending on the ban action. If Fail2ban drops packets, you see a timeout; if it rejects them with a reset, you see refused. Check with fail2ban-client status sshd whether your address is banned, and unban it if needed with fail2ban-client set sshd unbanip.
Why does SSH work over IPv4 but not over IPv6?
The service may listen only on IPv4 through ListenAddress or AddressFamily inet, or the firewall does not let IPv6 through. Test ssh -4 and ssh -6 separately and look with ss -tlnp for an entry for [::]:22.