NNESTECHUB
← BACK TO JOURNAL

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.

How to Fix SSH Connection Refused on a Linux Server

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 sshd

If the service is inactive, inspect its logs and configuration before restarting:

sudo sshd -t
sudo systemctl restart ssh
sudo systemctl enable ssh

Step 2: Confirm the Listening Port

sudo grep -Ei '^\s*Port' /etc/ssh/sshd_config
sudo ss -lntp | grep ssh

Connect to a custom port explicitly:

ssh -p 2222 user@server_ip

Step 3: Correct Firewall Rules

sudo ufw status verbose
sudo ufw allow 22/tcp
sudo ufw reload

When 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

ErrorLikely causeNext action
Connection refusedService stopped or wrong portCheck sshd and listener
Connection timed outFirewall or route dropAudit network rules
Permission deniedKey or user mismatchCheck authorized_keys
Host key changedServer rebuilt or riskVerify fingerprint before updating
JOIN THE CONVERSATION

What do you think?

0 COMMENTS

Be the first person to share a thought.

KEEP READINGExplore all stories →