How to Fix ‘Connection Refused’ (Port 22) SSH Error in Linux

Attempting to connect to a remote virtual private server (VPS), cloud instance, or Raspberry Pi via Secure Shell (SSH) only to receive the error: ssh: connect to host [hostname] port 22: Connection refused terminates administrative access immediately. Unlike a connection timeout (which implies packets are silently dropped by a network firewall or routing blackhole), Connection refused is an explicit TCP rejection: the destination host received your TCP SYN packet on port 22, but the operating system kernel immediately replied with a TCP RST (reset) packet. This signifies that either the SSH daemon (sshd) is not actively running, it is listening on an alternative custom port, or a host-based firewall (such as UFW or iptables) is actively rejecting incoming connections. This guide covers how to diagnose and resolve SSH connection rejections in Linux environments.

What Causes “Connection Refused” on SSH Port 22?

TCP reset responses on SSH port 22 originate from service, port, or firewall misconfigurations:

  • SSH Daemon (sshd) Is Inactive or Crashed: The OpenSSH server service is stopped, disabled, or failed during boot due to syntax errors in /etc/ssh/sshd_config.
  • Non-Standard SSH Port Configuration: The server administrator altered the default SSH port (e.g. to port 2222 or 4422 for security hardening), but the client is querying default port 22.
  • Host Firewall (UFW / Firewalld / iptables) Blocking Port 22: The local firewall rules on the destination server reject incoming traffic on TCP port 22.
  • OpenSSH Server Package Not Installed: On fresh desktop or container installations, only the SSH client (openssh-client) is installed, while the server daemon (openssh-server) is missing.
  • Local IP Address Conflict or Wrong Destination: Querying an incorrect local IP address assigned to another device on the local network that does not run an SSH server.

Method 1: Verify and Start the SSH Service on the Host Server

If you have console access (via cloud provider web console, KVM, or local monitor), verify whether sshd is active.

  1. Check the status of the SSH service:
    sudo systemctl status sshd
    (On Ubuntu/Debian, the service may be named ssh instead of sshd: sudo systemctl status ssh).
  2. If the service displays inactive (dead) or failed, start it:
    sudo systemctl start ssh
  3. Enable the service to start automatically on system boot:
    sudo systemctl enable ssh
  4. If the package is not installed at all, install OpenSSH Server:
    sudo apt update && sudo apt install openssh-server -y

Method 2: Check Which Port SSH Is Actually Listening On

Verify whether the SSH daemon is listening on an alternative custom port or bound exclusively to localhost (127.0.0.1).

  1. Use ss or netstat to inspect listening TCP sockets:
    sudo ss -tulpn | grep -i ssh
  2. Inspect the output:
    • If it displays 0.0.0.0:22 or [::]:22, the server is listening on all interfaces on port 22.
    • If it displays 0.0.0.0:2222, your SSH daemon is configured to listen on port 2222. Connect from your client using:
      ssh -p 2222 user@server_ip
    • If it displays 127.0.0.1:22, SSH is bound only to the local loopback interface and will reject remote network connections.
  3. To review port directives in the configuration file:
    grep -i "^Port" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*

Method 3: Configure Host Firewalls (UFW and Firewalld)

Ensure that incoming TCP traffic on the SSH port is explicitly permitted by local firewalls.

  1. On Ubuntu / Debian (UFW):
    sudo ufw status verbose
    sudo ufw allow 22/tcp
    sudo ufw reload
  2. On RHEL / CentOS / AlmaLinux / Rocky (Firewalld):
    sudo firewall-cmd --permanent --add-service=ssh
    sudo firewall-cmd --reload
  3. Inspect iptables filter tables:
    sudo iptables -L INPUT -v -n
    Ensure there are no REJECT or DROP rules positioned ahead of SSH accept rules.

Method 4: Validate sshd_config Syntax and Restart Daemon

Corrupted configuration directives can prevent the OpenSSH daemon from binding to network sockets.

  1. Run a syntax test on the OpenSSH configuration:
    sudo sshd -t
  2. If errors are reported, edit the configuration file to resolve syntax mistakes:
    sudo nano /etc/ssh/sshd_config
  3. Ensure Port 22 and ListenAddress 0.0.0.0 are properly defined without comment hashes (#).
  4. Restart the service:
    sudo systemctl restart ssh

SSH Connection Error Diagnostic Matrix

Error Message Network Layer Event Primary Root Cause
connect to host port 22: Connection refused TCP RST packet received from host sshd service stopped, wrong port queried, or UFW REJECT rule active
connect to host port 22: Connection timed out No response packets received (dropped) Network firewall (AWS Security Group, router NAT) dropping traffic
connect to host port 22: No route to host ICMP Host Unreachable packet received Routing failure, incorrect subnet, or server powered off
Permission denied (publickey) TCP handshake succeeded, SSH auth failed Mismatched SSH private/public key or incorrect username

Frequently Asked Questions

Why do I get Connection Refused when connecting to a Docker container via SSH?

Most Docker containers do not run an SSH daemon. The standard method for managing Docker containers is through docker exec -it [container_name] /bin/bash rather than running separate SSH daemons inside container images.

How do I test if port 22 is open without installing an SSH client?

You can test TCP socket connectivity from any client computer using nc (Netcat) or curl:

nc -zv server_ip 22
If open, it will return “Connection to server_ip 22 port [tcp/ssh] succeeded!”

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.