Skip to content
Hosting Operations7 min read

How to fix ssh connection refused port 22: Practical Guide

Step-by-step guide to diagnose and resolve SSH connection refused errors on port 22. Covers firewall rules, service status, and network configuration.

Written by Abdul AbrorTechnical Hosting Support Engineer
img IX mining rig inside white and gray room
On this page

TL;DR — Key takeaways

  • SSH connection refused on port 22 typically indicates the SSH service is not running, firewall rules are blocking the port, or the service is listening on a different port
  • Verify SSH service status with systemctl or service commands, then check firewall rules using iptables, ufw, or firewalld depending on your distribution
  • Always test SSH changes from a secondary connection or console access to avoid locking yourself out of the server
  • Common causes include stopped sshd service, incorrect firewall configuration, cloud provider security groups blocking port 22, or SSH configured to listen on a non-standard port

The 'connection refused' error on SSH port 22 is one of the most common issues encountered when managing remote servers. Unlike timeout errors that suggest network routing problems, a connection refused error means your client successfully reached the server, but the server actively rejected the connection attempt on port 22.

This guide walks through systematic troubleshooting steps to identify and resolve SSH connection refused errors. Whether you manage a single VPS or support multiple hosting environments, these diagnostic techniques will help you restore SSH access safely and efficiently.

Understanding SSH Connection Refused Errors

When you attempt to connect via SSH and receive 'connection refused', the TCP handshake reached your server but found no service listening on port 22, or the service rejected the connection. This differs from connection timeout errors, which indicate network-level blocking or unreachable hosts.

The SSH daemon (sshd) must be running and bound to port 22 for connections to succeed. Connection refused means one of three things: the SSH service is stopped, it's listening on a different port, or firewall rules are rejecting the connection before it reaches the service.

Understanding this distinction helps you troubleshoot effectively. Connection refused is a server-side issue, while timeouts typically point to network or cloud provider security group problems.

Check SSH Service Status

For older systems using SysVinit or Upstart:

Check status: sudo service ssh status or sudo /etc/init.d/ssh status

Start if needed: sudo service ssh start

If the service fails to start, check logs immediately with: sudo journalctl -u sshd -n 50 or sudo tail -n 50 /var/log/auth.log to identify configuration errors or missing dependencies.

  • Run: sudo systemctl status sshd (or ssh on Debian/Ubuntu)
  • Look for 'Active: active (running)' in the output
  • If stopped, start with: sudo systemctl start sshd
  • Enable automatic startup: sudo systemctl enable sshd

Verify SSH Port Configuration

If SSH is listening on a different port, connect using: ssh -p PORT_NUMBER user@server_ip

If you need to change the port back to 22, edit /etc/ssh/sshd_config, set 'Port 22', then restart sshd. Always maintain console access or a backup connection when modifying SSH settings to avoid lockouts.

  • Open: sudo cat /etc/ssh/sshd_config
  • Look for the 'Port' directive (uncommented lines without #)
  • Default is port 22; if you see 'Port 2222' or another value, SSH is listening elsewhere
  • Verify listening ports with: sudo ss -tlnp | grep sshd or sudo netstat -tlnp | grep sshd

Diagnose Firewall Rules

Before making firewall changes, ensure you have console access through your hosting provider's control panel or VNC connection. Incorrect firewall rules can lock you out completely.

  • List current rules: sudo iptables -L -n -v
  • Look for ACCEPT rules on port 22 or REJECT/DROP rules blocking it
  • Add rule if needed: sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT
  • Save rules: sudo iptables-save | sudo tee /etc/iptables/rules.v4 (path varies by distribution)

Check Cloud Provider Security Groups

If your server runs on AWS, Azure, Google Cloud, or another cloud platform, security groups or network ACLs may block port 22 independently of the server's firewall.

For AWS EC2 instances, review Security Groups in the AWS Console. The inbound rules must include an entry allowing TCP port 22 from your IP address (or 0.0.0.0/0 for public access, though IP whitelisting is more secure).

For Azure Virtual Machines, check Network Security Groups (NSGs) attached to your VM's network interface. Ensure an inbound rule permits TCP port 22.

For Google Compute Engine, verify firewall rules in VPC Network settings allow tcp:22 for your instance's network tags.

For DigitalOcean Droplets, check Cloud Firewalls in the Networking section and ensure SSH (port 22) is allowed.

Cloud provider security controls operate before traffic reaches your server, so even if sshd is running and server firewalls allow port 22, connection refused errors can occur if cloud-level rules block the connection.

Validate Network Connectivity and DNS

Check whether you're using the correct hostname or IP address. DNS misconfigurations can cause you to connect to the wrong server entirely. Verify the IP with: host yourdomain.com or nslookup yourdomain.com and confirm it matches your server's actual IP in your hosting panel.

If you recently changed DNS records or migrated servers, DNS propagation delays may direct your SSH client to an old IP where no SSH service exists.

  • Ping the server: ping server_ip (Ctrl+C to stop after a few responses)
  • Test port 22 specifically: telnet server_ip 22 or nc -zv server_ip 22
  • If telnet connects and shows 'SSH-2.0', SSH is reachable but your SSH client may have issues
  • If telnet also shows connection refused, the problem is confirmed server-side

Advanced Troubleshooting and Prevention

For SELinux issues, verify SSH context: sudo semanage port -l | grep ssh. If port 22 is not listed or SSH is confined, temporarily set SELinux to permissive mode for testing: sudo setenforce 0 (restore with setenforce 1 after diagnosis).

Prevent future connection refused errors by:

  • Monitoring SSH service with uptime checks or process monitoring tools
  • Enabling SSH daemon automatic restart on failure: sudo systemctl enable sshd
  • Documenting custom port configurations and firewall rules in your runbook
  • Maintaining console access credentials for your hosting provider's emergency console
  • Testing SSH access after system updates or firewall changes before closing your current session

Quick troubleshooting checklist

  • Verify SSH daemon is running: sudo systemctl status sshd
  • Check SSH listening port: sudo ss -tlnp | grep sshd
  • Review server firewall rules: sudo ufw status or sudo firewall-cmd --list-all
  • Confirm cloud provider security group allows port 22
  • Test connectivity: nc -zv server_ip 22
  • Check SSH daemon logs: sudo journalctl -u sshd -n 50
  • Maintain console access before making SSH configuration changes

FAQ

What does SSH connection refused on port 22 mean?

Connection refused on port 22 means your SSH client successfully reached the server, but the server rejected the connection. This typically occurs because the SSH daemon is not running, the service is listening on a different port, or firewall rules are blocking access to port 22. Unlike timeout errors that suggest network problems, connection refused is a server-side issue.

How do I check if SSH is running on my server?

Check SSH service status with 'sudo systemctl status sshd' on systemd-based distributions or 'sudo service ssh status' on older systems. Look for 'Active: active (running)' in the output. You can also verify listening ports with 'sudo ss -tlnp | grep sshd' to confirm SSH is bound to port 22.

Why does SSH connection get refused even though the service is running?

If SSH is running but connections are refused, the most common causes are firewall rules blocking port 22, cloud provider security groups rejecting traffic before it reaches your server, or SSH configured to listen on a non-standard port. Check your firewall rules with ufw, firewalld, or iptables, and verify cloud provider network security settings allow TCP port 22.

How do I fix SSH connection refused after a server reboot?

After reboot, SSH connection refused usually means the SSH daemon did not start automatically. Start it manually with 'sudo systemctl start sshd' and enable automatic startup with 'sudo systemctl enable sshd'. Check logs with 'sudo journalctl -u sshd' to identify why automatic startup failed, such as configuration errors or missing host keys.

Can I test SSH without locking myself out?

Always maintain a secondary connection or console access when testing SSH changes. Make configuration changes in one SSH session while keeping another session open. Test the changes by connecting in a new terminal before closing your existing sessions. If using a cloud provider, keep their web-based console access available as a backup method to regain access if SSH becomes unreachable.