sodcalculator

10 Tech How-To

SSH Connection Refused: Causes & Fixes

Guide 10-56027 · Updated · Step by step

If you see “ssh: connect to host … port 22: Connection refused,” the SSH daemon (sshd) is not running, not listening on the expected port, or a firewall is blocking the connection. This guide walks you through the most common causes and the exact commands to diagnose and fix each one.

What “Connection Refused” Actually Means

Unlike a timeout (which means packets are being dropped, usually by a firewall), “connection refused” is an active rejection. Your client sent a TCP SYN packet to the server’s IP and port, and something responded with a RST (reset) packet. That response comes from either the target machine itself or a network device that knows no service is listening there. In practical terms, the SSH service is either not running, not bound to the address you’re connecting to, or a firewall is actively rejecting the connection rather than silently dropping it.

Check If the SSH Service Is Running

The first thing to verify is whether sshd is actually running on the server. On most Linux distributions, you can check with:

  1. Run sudo systemctl status sshd (or ssh on some distros like Ubuntu). If the service is inactive or failed, you’ve found the problem.
  2. Start it with sudo systemctl start sshd and enable it to start at boot with sudo systemctl enable sshd.
  3. If you don’t use systemd, try sudo service ssh start or check your init system’s documentation.

If the service is running but you still get refused, check that it’s listening on the expected interface and port. Use sudo netstat -tlnp | grep ssh or sudo ss -tlnp | grep ssh. You should see a line like LISTEN 0 128 0.0.0.0:22 or [::]:22. If it’s listening only on 127.0.0.1:22, then sshd is bound to localhost only, and remote connections will be refused.

Verify the Port and IP Address

A common mistake is connecting to the wrong port or IP. SSH defaults to port 22, but many admins change it to reduce bot scans. Check the server’s /etc/ssh/sshd_config for a Port line. If it’s set to, say, 2222, then you must connect with ssh -p 2222 user@server.

Also verify the IP address. If the server has multiple interfaces (e.g., a private and public IP), make sure you’re connecting to the one sshd is bound to. You can check with ip addr on the server. If you’re using a hostname, ensure DNS resolves to the correct address with getent hosts server.example.com.

Firewall Rules That Cause Refusal

Firewalls can be configured to reject (not drop) packets, which produces “connection refused” instead of a timeout. On the server, check your firewall rules:

  • ufw (Ubuntu/Debian): sudo ufw status. If active, make sure port 22 (or your custom port) is allowed: sudo ufw allow 22/tcp.
  • firewalld (RHEL/CentOS/Fedora): sudo firewall-cmd --list-all. Add the service with sudo firewall-cmd --permanent --add-service=ssh and reload with sudo firewall-cmd --reload.
  • iptables (legacy): sudo iptables -L -n. Look for a REJECT rule on port 22.

Remember that cloud providers (AWS, Azure, GCP) have their own security groups or network security groups that act as firewalls outside the OS. If the OS firewall is open but you still get refused, check the cloud console’s inbound rules.

SSH Daemon Configuration Errors

Sometimes sshd is running but refuses connections due to a configuration issue. Look for these in /etc/ssh/sshd_config:

  • ListenAddress: If set to a specific IP, sshd will only listen on that address. Remove or comment the line to listen on all interfaces.
  • AllowUsers / AllowGroups: These restrict which users can log in. If your user isn’t listed, you’ll get a “Permission denied” after connecting, but if the daemon fails to parse the file, it may refuse to start.
  • Port: Ensure it matches what you’re connecting to.

After editing the config, always test it with sudo sshd -t before restarting. If there’s a syntax error, the daemon won’t start, and you’ll be locked out. Then restart with sudo systemctl restart sshd.

Client-Side Issues

On the client machine, the problem could be local. Try connecting to a different host to confirm SSH works at all. Also check if you have a ~/.ssh/config file that might be overriding the port or hostname. Run ssh -v user@server to see verbose output; it will show exactly where the connection fails. If you’re using a non-standard port, remember the -p flag. On Windows, the built-in OpenSSH client works the same way.

Frequently Asked Questions

Why do I get “Connection refused” but only from outside the network?

This usually means sshd is listening on all interfaces (0.0.0.0) but a router or cloud security group is blocking the port. From inside the network, the connection reaches the server directly; from outside, it must pass through the router’s NAT and firewall. Check the router’s port forwarding rules and the cloud provider’s security group.

What’s the difference between “Connection refused” and “Connection timed out”?

“Connection refused” means a RST packet was received, indicating no service is listening or a firewall is actively rejecting. “Connection timed out” means no response at all, usually because a firewall is silently dropping packets or the host is unreachable. The fix differs: refused points to sshd or a REJECT rule; timeout points to a DROP rule or network issue.

Can I fix this without physical access to the server?

If you have out-of-band access (like a cloud console or a KVM), you can log in and fix the service. If not, and the SSH service is down, you may need to reboot the server (if sshd is enabled at boot) or use a rescue mode provided by your hosting provider. Always ensure sshd is enabled to start at boot to avoid this.

Why does SSH work on port 22 but not on my custom port?

If you changed the port in sshd_config but didn’t restart the service, or if a firewall only allows port 22, the custom port will be refused. Also, SELinux on RHEL-based systems can block non-standard ports; you may need to run sudo semanage port -a -t ssh_port_t -p tcp 2222.

How do I test if a port is open from my client?

Use nc -zv server.com 22 (netcat) or telnet server.com 22. If you get “Connection refused,” the port is closed; if you get “Connected,” the port is open and SSH should work. This helps isolate whether the issue is SSH-specific or network-wide.

What if I see “Connection refused” but the service is running?

Check that sshd is listening on the correct interface and port. Also check for a local firewall (like ufw) that might be rejecting connections. Run sudo ss -tlnp | grep ssh to see the actual listening address. If it shows 127.0.0.1:22, you need to change ListenAddress to 0.0.0.0 or comment it out.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →