How to Fix SSH Connection Refused on a Linux Server
Restore remote access by checking sshd, confirming the listening port, correcting UFW rules, and auditing cloud security groups.

ssh: connect to host port 22: Connection refused means the network request reached the host, but nothing accepted the connection on that port. This differs from a timeout, which often points to routing or firewall drops.
Common Causes
- The SSH daemon is stopped or failed.
- The service listens on a custom port.
- UFW, nftables, or a cloud firewall rejects the connection.
- The hostname points to the wrong server.
Step 1: Check the SSH Service from the Provider Console
Use the cloud provider’s serial, recovery, or web console so you do not depend on SSH.
# Ubuntu / Debian
sudo systemctl status ssh
# RHEL / Fedora / CentOS
sudo systemctl status sshdIf the service is inactive, inspect its logs and configuration before restarting:
sudo sshd -t
sudo systemctl restart ssh
sudo systemctl enable sshStep 2: Confirm the Listening Port
sudo grep -Ei '^\s*Port' /etc/ssh/sshd_config
sudo ss -lntp | grep sshConnect to a custom port explicitly:
ssh -p 2222 user@server_ipStep 3: Correct Firewall Rules
sudo ufw status verbose
sudo ufw allow 22/tcp
sudo ufw reloadWhen using a custom port, allow that port before restarting SSH so you do not lock yourself out.
Step 4: Audit the Cloud Security Group
Add an inbound TCP rule for the actual SSH port. Restrict the source to your public IP with a /32 mask instead of exposing administrative access to the entire internet.
SSH Error Matrix
| Error | Likely cause | Next action |
|---|---|---|
| Connection refused | Service stopped or wrong port | Check sshd and listener |
| Connection timed out | Firewall or route drop | Audit network rules |
| Permission denied | Key or user mismatch | Check authorized_keys |
| Host key changed | Server rebuilt or risk | Verify fingerprint before updating |
0 COMMENTS
Be the first person to share a thought.