SSH "Connection refused" but ping works: what to check
Ping only proves the server answers ICMP. "Connection refused" means nothing accepted your TCP connection on that SSH port: sshd is stopped, listening on another port or address, or a firewall rejects the traffic. Check the port and IP first, then sshd and the firewalls from the server console.
On this page
What it means
A working ping is reassuring, but it misleads. Ping uses ICMP, and ICMP getting through says nothing about TCP port 22. When you see ssh: connect to host 203.0.113.0 port 22: Connection refused, your connection attempt reached something that actively said no: a TCP reset, or a reject from a firewall.
So the machine is up, but nothing is accepting connections on that IP and port. Either sshd is not listening there, or a rule rejects the traffic.
This article covers the general case. For specific setups see SSH connection refused on Ubuntu, on a Raspberry Pi or the general explanation. The steps below are written for OpenSSH on Linux where sshd is started as a normal service. Windows OpenSSH Server is the same in principle but is not covered here.
Symptoms and causes
You are likely dealing with this problem if:
ssh: connect to host 203.0.113.0 port 22: Connection refusedpingto the same address replies normallyssh -v user@hostfails right after "Connecting to…", before any key exchange
The usual causes, most common first:
- sshd is not running
- The most common reason. It can happen after a reboot with the service disabled, or after a crash or a bad config file.
- sshd listens on a different port
- The
Portsetting defaults to 22, but may have been changed, while your client still tries 22. - ListenAddress limits sshd to one address
- If
ListenAddressis set to something like 127.0.0.1, remote connections are refused. - Host firewall rejects the port
- UFW, firewalld or iptables may reject traffic, or a default-drop policy may not allow the SSH port.
- Network or cloud firewall blocks the port
- A security group, provider firewall, ISP or corporate network may block port 22 or your custom SSH port.
- Wrong IP address
- The address may not belong to the server you think it does, or may now belong to a different machine.
How to fix it
Work through these in order. They start with the quickest, safest checks and move to deeper repairs; stop as soon as the problem is gone.
1.Check the port and IP with verbose output
A wrong port or address is the cheapest thing to rule out, and verbose mode shows where the attempt fails.
- Run the client in verbose mode with the port you believe sshd uses.
ssh -v -p 22 user@host - Confirm the IP address really belongs to the server you want. If it came from a provider panel, compare it there.
- If your admin or provider uses a custom port, retry with that port in
-p.
2.Get console access if you cannot log in at all
The next fixes run on the server itself, so you need a way in that does not depend on SSH.
- Use your provider's recovery or web console, or sit at the machine physically.
3.Check that sshd is running
If the service is stopped, nothing listens on the port and every connection is refused.
- Check the service status. The unit is
sshdon RHEL-family systems andsshon Debian and Ubuntu.systemctl status sshd systemctl status ssh - On older systems without systemd, use the service command.
service ssh status - If it is stopped, start it (use
sshinstead ofsshdon Debian and Ubuntu).sudo systemctl start sshd - If it will not start, test the configuration file for errors.
sudo sshd -t
4.Check the port and listen address
sshd may be running but bound to another port or only to localhost.
Before you start: Keep your console session open while editing the SSH configuration, and copy the file first (for example to sshd_config.bak) so you can restore it. Always run sudo sshd -t before restarting: a syntax error followed by a restart can leave sshd down.
- Show the effective Port and ListenAddress settings. This also picks up files pulled in by
Include(such as/etc/ssh/sshd_config.d/*.conf), which reading only the main file would miss.sudo sshd -T | grep -Ei '^(port|listenaddress)' - See what sshd is actually listening on.
sudo ss -plnt - If sshd appears only on 127.0.0.1, edit the file that sets
ListenAddress(the mainsshd_configor a drop-in found in the previous step). Comment the line out, or set it to0.0.0.0to listen on all IPv4 addresses. Avoid hard-coding the server's public IP: on cloud VMs that address is often NATed and not bound to the network interface, so sshd would fail to bind. - Check the file for errors, and only if that prints nothing, restart sshd (use
sshinstead ofsshdon Debian and Ubuntu).sudo sshd -t sudo systemctl restart sshd - If it uses a non-default port, connect with
ssh -p <port> user@host.
5.Check the host firewall
A firewall on the server can reject the SSH port even though sshd is healthy.
Before you start: Do not remove rules you do not understand. Only add an allow rule for your SSH port, and keep your console session open until you have tested a new connection.
- Inspect the firewall you use. For UFW, confirm the SSH port is allowed.
sudo ufw status - For firewalld, list allowed services and ports.
sudo firewall-cmd --list-services sudo firewall-cmd --list-ports - For iptables, look for REJECT or DROP rules on the INPUT chain.
sudo iptables -nL - If the SSH port is not allowed, add an allow rule using your firewall's own documentation for the exact syntax.
6.Check cloud and network firewalls
The block may sit outside the server, in a security group, provider firewall, ISP or office network.
- Open your provider's firewall or security group settings and confirm the SSH port is allowed inbound.
- If a rule only allows your own IP and your ISP changed it, update the rule.
- Try connecting from a different network, such as a phone hotspot, to see whether your current network is the cause.
Check that it worked
sudo ss -plntshows sshd listening on the expected port and on a non-loopback addressssh -v -p <port> user@hostgets past "Connecting to…" and reaches the key exchange and login prompt
If it still fails
- Run
ssh -vagain and compare the result with whatss -plntshows on the server: if sshd is listening correctly there, the block is almost certainly in a firewall between you and it. - Test from a second network to separate a local network block from a server problem.
- If sshd will not start, run
sudo sshd -tfrom the console to find the reason before changing more settings. - Contact your hosting provider's support with the exact error text and the time of the failure.
Questions
Why does ping work if SSH is refused?
Ping uses ICMP, while SSH needs a TCP connection to a specific port. The host can answer ICMP while nothing accepts connections on port 22.
Sources
- How to Troubleshoot SSH Connectivity Issues · DigitalOcean
- sshd_config(5)Official · OpenBSD (OpenSSH project)
- SSH command · ssh.com