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.

Applies to:OpenSSH client and server (Linux, BSD, macOS)Cloud VMs running OpenSSH
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 refused
  • ping to the same address replies normally
  • ssh -v user@host fails 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 Port setting defaults to 22, but may have been changed, while your client still tries 22.
ListenAddress limits sshd to one address
If ListenAddress is 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

Easy

A wrong port or address is the cheapest thing to rule out, and verbose mode shows where the attempt fails.

  1. Run the client in verbose mode with the port you believe sshd uses.
    Terminal
    ssh -v -p 22 user@host
  2. Confirm the IP address really belongs to the server you want. If it came from a provider panel, compare it there.
  3. 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

Easy

The next fixes run on the server itself, so you need a way in that does not depend on SSH.

  1. Use your provider's recovery or web console, or sit at the machine physically.

3.Check that sshd is running

Easy

If the service is stopped, nothing listens on the port and every connection is refused.

  1. Check the service status. The unit is sshd on RHEL-family systems and ssh on Debian and Ubuntu.
    Terminal
    systemctl status sshd
    systemctl status ssh
  2. On older systems without systemd, use the service command.
    Terminal
    service ssh status
  3. If it is stopped, start it (use ssh instead of sshd on Debian and Ubuntu).
    Terminal
    sudo systemctl start sshd
  4. If it will not start, test the configuration file for errors.
    Terminalneeds sudo / root
    sudo sshd -t

4.Check the port and listen address

ModerateChanges settings

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.

  1. 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.
    Terminalneeds sudo / root
    sudo sshd -T | grep -Ei '^(port|listenaddress)'
  2. See what sshd is actually listening on.
    Terminalneeds sudo / root
    sudo ss -plnt
  3. If sshd appears only on 127.0.0.1, edit the file that sets ListenAddress (the main sshd_config or a drop-in found in the previous step). Comment the line out, or set it to 0.0.0.0 to 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.
  4. Check the file for errors, and only if that prints nothing, restart sshd (use ssh instead of sshd on Debian and Ubuntu).
    Terminalneeds sudo / root
    sudo sshd -t
    sudo systemctl restart sshd
  5. If it uses a non-default port, connect with ssh -p <port> user@host.

5.Check the host firewall

ModerateChanges settings

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.

  1. Inspect the firewall you use. For UFW, confirm the SSH port is allowed.
    Terminalneeds sudo / root
    sudo ufw status
  2. For firewalld, list allowed services and ports.
    Terminalneeds sudo / root
    sudo firewall-cmd --list-services
    sudo firewall-cmd --list-ports
  3. For iptables, look for REJECT or DROP rules on the INPUT chain.
    Terminalneeds sudo / root
    sudo iptables -nL
  4. 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

Moderate

The block may sit outside the server, in a security group, provider firewall, ISP or office network.

  1. Open your provider's firewall or security group settings and confirm the SSH port is allowed inbound.
  2. If a rule only allows your own IP and your ISP changed it, update the rule.
  3. 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 -plnt shows sshd listening on the expected port and on a non-loopback address
  • ssh -v -p <port> user@host gets past "Connecting to…" and reaches the key exchange and login prompt

If it still fails

  • Run ssh -v again and compare the result with what ss -plnt shows 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 -t from 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

  1. How to Troubleshoot SSH Connectivity Issues · DigitalOcean
  2. sshd_config(5)Official · OpenBSD (OpenSSH project)
  3. SSH command · ssh.com