Skip to content

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

Key points

  • Refused is different from a timeout, which suggests a firewall dropping packets.
  • A bad sshd_config stops sshd restarting.
  • fail2ban can block your own IP.
  • Keep a console path for lockouts.

What it means

The TCP connection was rejected immediately, so the machine is reachable. Either sshd is not running, listens on a different port or address, or a firewall rule rejects rather than drops.

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.

Diagnose it

From a client, then from the provider's console if you can get in.

bash
# 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 status

Fixes

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.

Safer access

Once back in, disable password login with PasswordAuthentication no, use keys, and restrict the source addresses that may connect.

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.

Frequently asked questions

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

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.