Refused or Timed Out?
The two errors point in different directions:
- Connection refused: the server, or a firewall in front of it, sent back a rejection at once. Nothing is listening on that port, or a rule rejects the packet.
- Connection timed out: no answer came back at all. Packets are dropped on the way, or the server is down. See SSH connection timed out.
The same logic applies to web servers; see ERR_CONNECTION_REFUSED.
Check from Your Side
First make sure you use the right port. If SSH was moved, port 22 refuses:
ssh -p 2222 user@your_server_ip
nc -zv your_server_ip 22
nc -zv your_server_ip 2222
nc -zv only tests whether a port accepts connections; more examples are in the nc command guide. If one port succeeds, use that one.
Is the SSH Service Running?
You cannot use SSH to check SSH, so open the server console in your provider's control panel and log in there.
sudo systemctl status ssh # Ubuntu, Debian
sudo systemctl status sshd # AlmaLinux, Rocky Linux
If it is stopped or failed, test the configuration before starting it:
sudo sshd -t
sudo systemctl start ssh
On AlmaLinux and Rocky Linux the unit is sshd. If sshd -t prints an error, fix the line it names first: a typo in /etc/ssh/sshd_config or a drop-in is a common reason the service will not start. On recent Ubuntu releases SSH can be started on demand through ssh.socket, so an inactive ssh.service is not by itself an error; see how to restart the SSH service.
Which Port Does It Listen On?
sudo ss -ltnp | grep -E 'ssh|:22'
The output shows the address and port in use. If it listens only on 127.0.0.1, a ListenAddress line limits it to local connections. If the port is not the one you expected, compare it with the Port line. After a port change on Ubuntu 24.04 and later, the socket keeps the old port until you run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket; the steps are in how to change the SSH port.
Firewall Rules on the Server
sudo ufw status verbose # Ubuntu, Debian with ufw
sudo firewall-cmd --list-all # AlmaLinux, Rocky Linux
The SSH port must be allowed. Add a missing rule, for example sudo ufw allow 2222/tcp, or with firewalld sudo firewall-cmd --permanent --add-port=2222/tcp followed by sudo firewall-cmd --reload. Do not switch the firewall off to test; open the one port instead.
Banned by Fail2ban
After several failed logins, Fail2ban blocks your address for a while. Its standard ban actions reject the packets rather than drop them, so the ban usually shows up as "Connection refused". In the console:
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip YOUR_IP
Replace YOUR_IP with your public address. Then find out which client kept failing, often a saved wrong password or key.
Right After a Reboot or Reinstall
While the server is still booting, SSH may not be listening yet, and the port can refuse for a short time. Wait a minute and try again. After a reinstall, also expect a new host key warning; see host key verification failed.