Fix SSH "Connection refused" and "Connection timed out"
"Connection refused" means your request reached the server but nothing answers on that port. "Timed out" means it never arrived. Here is how to find the cause and fix it.
Last updated:
What is the difference between the two errors?
Connection refused: the server is up and answered with a refusal, so SSH is usually stopped, running on a port other than 22, or rejected by a firewall.
Connection timed out: no answer at all, so the server is usually off, the address is wrong, a firewall silently drops the request, or your network blocks the port.
Step 1: check the server is running and the address is right
Open your provider's panel, check the server is running and the IP is the one you are using. Then try again in verbose mode, which shows where the connection stops.
$ ssh -v root@SERVER_IPStep 2: use the server console and start SSH
The server console (VNC) does not depend on SSH, so it works when SSH does not. Log in there and check the service. It is called ssh on Ubuntu and Debian, and sshd on AlmaLinux, Rocky and CentOS.
$ sudo systemctl status ssh $ sudo systemctl restart ssh
Step 3: check the port
If SSH runs on a port other than 22 you must name it when connecting. These commands show the real port and the one in the configuration.
$ sudo ss -tlnp | grep ssh $ sudo grep -i '^Port' /etc/ssh/sshd_config
$ ssh -p 2222 root@SERVER_IPStep 4: check the firewall, inside and outside
There can be two firewalls: one inside the system (ufw or firewalld) and one in the provider's panel. Both must allow the port. Use ufw on Ubuntu and Debian, and firewall-cmd on the Red Hat family.
$ sudo ufw status $ sudo ufw allow 22/tcp
$ sudo firewall-cmd --permanent --add-service=ssh $ sudo firewall-cmd --reload
Step 5: try from another network
Connect over mobile data instead of your network. If that works, the problem is your network or your address being banned on the server. fail2ban bans addresses after failed logins, and you can lift the ban from the console.
$ sudo fail2ban-client status sshd $ sudo fail2ban-client set sshd unbanip YOUR_IP

