How to fix ssh connection refused port 22: Troubleshooting Checklist
Systematic guide to diagnose and fix SSH connection refused errors on port 22. Includes step-by-step checks, common causes, and a quick-reference checklist.

On this page
TL;DR — Key takeaways
- SSH connection refused on port 22 typically indicates the SSH service is not running, the port is blocked by a firewall, or the service is listening on a different port
- Start diagnostics by verifying the SSH service status on the server, then check firewall rules, then confirm network-level access and port configuration
- Always create a backup access method before modifying SSH or firewall settings to prevent lockout from your server
- Connection refused differs from connection timeout: refused means the port actively rejected the connection, while timeout means no response was received
When you see 'Connection refused' while attempting to connect via SSH on port 22, it means your client reached the server but the connection was actively rejected. This is distinct from a timeout, which indicates the server never responded. Understanding this difference is the first step in efficient troubleshooting.
This guide walks through the most common causes of SSH connection refused errors in order of likelihood, with concrete diagnostic commands and fixes for each scenario. Whether you're a hosting customer locked out of your VPS or a support engineer investigating a ticket, this checklist will help you systematically identify and resolve the issue.
Understanding Connection Refused vs Other SSH Errors
Connection refused means the TCP handshake reached the target machine, but nothing was listening on port 22 to accept the connection. This is fundamentally different from other SSH errors and narrows down where to look.
If you see a timeout instead, the packets never reached the server or no response came back—pointing to network-level issues, incorrect IP addresses, or cloud security groups blocking traffic. If you see 'Connection reset by peer' or 'No route to host,' those indicate different failure modes entirely.
The 'connection refused' message confirms network routing is working and the server is reachable, so your troubleshooting should focus on the SSH service state, local firewall rules, and port configuration rather than network-layer problems.
Check if SSH Service is Running
The most common cause of connection refused on port 22 is that the SSH daemon (sshd) is not running. This happens after a failed service restart, system reboot without auto-start enabled, or manual shutdown.
If you have console access through your hosting provider's control panel or IPMI, log in directly and check the service status. On systemd-based systems (Ubuntu 16.04+, CentOS 7+, Debian 8+), run: systemctl status sshd or systemctl status ssh depending on the distribution.
If the service is inactive or failed, check the service logs for errors: journalctl -u sshd -n 50. Common failures include configuration syntax errors in /etc/ssh/sshd_config, missing host keys, or permission problems on key files. Once you've addressed the underlying issue, start the service with: sudo systemctl start sshd and enable it to start on boot: sudo systemctl enable sshd.
On older init-based systems, use: service sshd status and service sshd start. If you don't have console access and can't reach the server, contact your hosting provider to restart the SSH service from their side.
Verify SSH is Listening on Port 22
Even if the SSH service is running, it may be configured to listen on a non-standard port. Check which port sshd is actually using by running: sudo ss -tlnp | grep sshd or sudo netstat -tlnp | grep sshd.
Look for a line showing the process listening on 0.0.0.0:22 or :::22. If you see a different port number like 2222 or 2200, that's your answer—the SSH daemon was reconfigured to use that port instead. Update your SSH client command to specify the correct port: ssh -p 2222 user@hostname.
If nothing is listening on any port, the service isn't actually running despite what systemctl might show. This can happen if sshd failed to bind due to an address conflict or permission issue. Check /var/log/auth.log or journalctl -u sshd for bind errors.
To change the SSH port back to 22, edit /etc/ssh/sshd_config, find the Port directive, set it to 22, then restart sshd. Before doing this, ensure port 22 isn't already in use by another service.
Inspect Firewall Rules Blocking Port 22
A local firewall on the server can block port 22 even when sshd is running and listening. The two most common firewall tools on Linux are iptables and its frontend ufw (Uncomplicated Firewall), plus firewalld on RHEL-based systems.
Check ufw status with: sudo ufw status verbose. If it shows active and port 22 is not listed in the allowed rules, add it: sudo ufw allow 22/tcp. If you changed SSH to a custom port, allow that port instead and consider removing the old rule.
For iptables directly, list current rules: sudo iptables -L -n -v. Look for a DROP or REJECT rule affecting port 22 in the INPUT chain. To allow SSH, insert a rule: sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT. Remember to save the rules so they persist after reboot: sudo iptables-save > /etc/iptables/rules.v4 or use your distribution's persistence mechanism.
On firewalld systems (CentOS, Fedora, RHEL), check with: sudo firewall-cmd --list-all. Add SSH if missing: sudo firewall-cmd --permanent --add-service=ssh followed by sudo firewall-cmd --reload.
Before modifying firewall rules, document the current state and have a backup access method. Locking yourself out by misconfiguring the firewall is a common issue. If possible, test rule changes without making them permanent first.
Check Cloud and Hosting Security Groups
If you're running on a cloud platform like AWS, Google Cloud, Azure, DigitalOcean, or Linode, a security group or network firewall at the infrastructure level may be blocking port 22 before traffic even reaches your server's local firewall.
Log into your cloud provider's control panel and locate the security group or firewall configuration attached to your instance. Verify there's an inbound rule allowing TCP traffic on port 22 from your IP address or from 0.0.0.0/0 if you need global access.
For AWS EC2, check the security group's inbound rules. For Google Cloud, check VPC firewall rules. For Azure, inspect Network Security Groups. Each platform has a slightly different interface, but the concept is the same: ensure port 22 TCP is explicitly allowed.
If you recently created the instance from a template or snapshot, the security group may have inherited restrictive rules. Create a new rule allowing SSH or modify the existing group. Changes typically take effect immediately, but test from a different network if possible to confirm external accessibility.
Validate Network Routing and DNS Resolution
While connection refused usually means the packets reached the server, it's worth confirming you're connecting to the correct IP address. Use: ping hostname and verify the resolved IP matches your server's actual address. DNS propagation issues or stale records can direct you to the wrong machine.
If you're connecting through a VPN, proxy, or NAT gateway, confirm the routing allows outbound connections to port 22. Some corporate and public networks block SSH traffic entirely. Test from a different network (mobile hotspot, different ISP) to rule out client-side restrictions.
Use telnet or nc to test raw TCP connectivity: telnet server-ip 22 or nc -zv server-ip 22. If you get 'Connection refused,' the server actively rejected it. If you get a timeout, packets aren't reaching the server. If you see 'Connected' or an SSH version banner, port 22 is open and sshd is responding—indicating the issue may be with your SSH client configuration or keys.
For IPv6-enabled servers, ensure you're connecting over the correct protocol. Some configurations listen on IPv4-only or IPv6-only. Explicitly specify the address family if needed: ssh -4 user@hostname or ssh -6 user@hostname.
Review SSH Configuration and Restart Safely
Syntax errors in /etc/ssh/sshd_config can prevent sshd from starting or cause it to reject connections. Before restarting SSH after any configuration change, test the config file: sudo sshd -t. This validates syntax without disrupting the running service.
Common misconfigurations include: PermitRootLogin set to 'no' when you're trying to connect as root, AllowUsers or AllowGroups restrictions that exclude your username, and ListenAddress directives that bind to the wrong interface.
If you must restart sshd, keep your current session open and test the new connection in a separate terminal before closing your existing session. This prevents lockout if the restart fails. Use: sudo systemctl restart sshd then immediately test: ssh user@hostname in another window.
For mission-critical systems, consider setting up a secondary SSH daemon on an alternate port as a backup access method before making changes to the primary service. Clone the sshd configuration, change the port, and run a second instance.
Quick troubleshooting checklist
- Confirm the error message is 'Connection refused' and not 'Connection timed out' or another error type
- Access server console or out-of-band management to log in directly if SSH is unavailable
- Check SSH service status: sudo systemctl status sshd (or ssh)
- Verify sshd is listening on port 22: sudo ss -tlnp | grep sshd
- Review recent service logs for errors: journalctl -u sshd -n 50
- Check local firewall rules: sudo ufw status or sudo iptables -L -n
- Allow SSH through firewall if blocked: sudo ufw allow 22/tcp
- Verify cloud security group allows inbound TCP port 22
- Test SSH config syntax before restarting: sudo sshd -t
- Confirm DNS resolves to correct IP: ping hostname
- Test raw connectivity from client: nc -zv server-ip 22
- Try connecting from a different network to rule out client-side blocks
- Restart SSH service only after validating config and keeping existing session open
- Document all changes and create rollback plan before modifying production SSH settings
FAQ
What does SSH connection refused on port 22 mean?
SSH connection refused on port 22 means your SSH client successfully reached the server, but the connection was actively rejected because nothing is listening on that port. This typically indicates the SSH service is not running, is listening on a different port, or a firewall rule is blocking access. It differs from a timeout, which means the server never responded at all.
How do I check if SSH service is running on Linux?
On systemd-based Linux distributions (Ubuntu 16.04+, CentOS 7+, Debian 8+), check SSH service status with: sudo systemctl status sshd or sudo systemctl status ssh. On older systems, use: sudo service sshd status. If the service is inactive or failed, start it with: sudo systemctl start sshd and enable auto-start on boot with: sudo systemctl enable sshd.
Why is my SSH connection refused even though the service is running?
If sshd is running but connections are refused, the service may be listening on a non-standard port instead of 22, or a firewall is blocking the connection. Check which port sshd is using with: sudo ss -tlnp | grep sshd. Verify local firewall rules with: sudo ufw status or sudo iptables -L. Also check cloud provider security groups if you're on AWS, Google Cloud, Azure, or similar platforms.
How do I allow SSH through UFW firewall?
To allow SSH connections through UFW (Uncomplicated Firewall), run: sudo ufw allow 22/tcp for the default port, or sudo ufw allow 2222/tcp if you changed SSH to a custom port. Check current rules with: sudo ufw status verbose. If UFW is not enabled yet, activate it with: sudo ufw enable, but ensure SSH is allowed first to prevent lockout.
What should I do if I locked myself out after changing SSH settings?
If you're locked out after changing SSH or firewall settings, access your server through your hosting provider's console, VNC, or IPMI out-of-band management. From console access, check the SSH service status, review /etc/ssh/sshd_config for errors, verify firewall rules, and restart the SSH service. For cloud servers, use the provider's web-based console or serial console feature to regain access.
Related articles
- Hosting OperationsSelf-Hosted App Deployment Fails? Check DNS, SSL, Reverse Proxy, and Logs FirstTroubleshoot failed self-hosted app deployments by checking DNS, SSL, reverse proxy routing, container status, logs, and ports.
- Hosting OperationsSelf-Hosted PaaS on a VPS: What to Check Before Installing Coolify, Dokploy, or CapRoverA hosting support checklist for preparing a VPS before installing self-hosted PaaS tools like Coolify, Dokploy, or CapRover.
- Hosting OperationsLinux Server Security Lessons from the Arch Linux Malware Package IncidentPractical Linux server security checklist for VPS admins after package malware concerns, with safe checks, rollback steps, and support guidance.
- Hosting OperationsAWS Lightsail Hong Kong VPS Latency: Practical Hosting Guide for IndonesiaLearn how to test AWS Lightsail Hong Kong VPS latency, compare regions, migrate safely, and troubleshoot hosting performance.
- Hosting OperationsCloudflare Tomorrow Watchlist: A Practical Hosting Operations GuidePractical Cloudflare troubleshooting checklist for DNS, SSL, caching, WAF, origin health, safe testing, and rollback planning.
- Hosting OperationsNetwork Safety Checklist for AI Agent Skills in Hosting OperationsAudit AI agent skills safely with network checks, secret protection, sandbox testing, rollback steps, and hosting support troubleshooting guidance.