SSH Connection Refused on Linux Server

An “SSH connection refused” error indicates that the connection reached the host, but no service accepted it. This runbook provides a structured, low-risk approach to identify and resolve the issue without locking yourself out.


Symptoms

  • SSH client returns connection refused

  • Server responds to ping, but SSH fails

  • Issue appeared after hardening or firewall changes

  • Console access still works


Environment

  • Linux servers (AlmaLinux, Rocky Linux, Ubuntu, Debian)

  • OpenSSH server

  • Tested on systemd-based distributions


Common Root Causes

  • sshd service stopped or failed

  • SSH listening on a non-standard port

  • Firewall blocking port 22 (or custom port)

  • SELinux denying sshd access

  • IP-based access restrictions


Fix Path (SAFE)

Step 1: Verify SSH Service Status

systemctl status sshd

If stopped:

systemctl start sshd
systemctl enable sshd

Step 2: Confirm Listening Port

ss -tulnp | grep ssh

Expected:

LISTEN 0 128 0.0.0.0:22

If a custom port is used, note it for firewall checks.


Step 3: Check Firewall Rules

firewalld

firewall-cmd --list-all
firewall-cmd --add-service=ssh --permanent
firewall-cmd --reload

nftables / iptables

iptables -L INPUT -n

Step 4: SELinux Validation (If Enabled)

getenforce

If enforcing:

semanage port -l | grep ssh

Allow custom port if required:

semanage port -a -t ssh_port_t -p tcp 2222

Verification

ssh user@server_ip
journalctl -u sshd --since "5 minutes ago"

Connection should succeed without errors.


Rollback

firewall-cmd --remove-service=ssh --permanent
firewall-cmd --reload
systemctl stop sshd

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *