SSH "Connection refused" by the remote host: what it means and how to fix it
"Connection refused" means the connection was rejected before SSH even started, so it is not a password or key problem. Most often sshd is not installed or not running, or it listens on a different port.
On this page
What it means
The full message usually looks like ssh: connect to host 203.0.113.5 port 22: Connection refused. The connection itself was rejected. The SSH conversation, including your password or key, never started, so this is not a login problem. If you see Permission denied (publickey) instead, that is a different error: the server was reached and then rejected your credentials.
The same message shows up in scp, sftp, rsync over ssh and git over ssh, because they all use the same connection.
The server-side steps below are written for Debian and Ubuntu, where the service is called ssh. On other Linux distributions the service name, paths and defaults can differ, so check that distribution's documentation. macOS (Remote Login) and the Windows OpenSSH Server service use different steps that this article does not cover.
If you are on a specific setup, there are shorter guides for Ubuntu and for a Raspberry Pi.
Symptoms and causes
You are likely dealing with this problem if:
ssh: connect to host HOST port 22: Connection refusedright after you run the command- The same refusal from scp, sftp, rsync or git over ssh
The usual causes, most common first:
- sshd is not installed or not running
- The SSH server is a separate package from the client. On Ubuntu it is
openssh-server, and a fresh Ubuntu Desktop install does not include it. The service can also have stopped or failed at boot. - sshd listens on another port or address
- sshd defaults to port 22, but
PortandListenAddressinsshd_configcan change that, and several values are allowed. A server withListenAddress 127.0.0.1only accepts connections from itself. - sshd failed to start because of a config error
- An invalid directive can stop sshd from starting. On Ubuntu the main file also includes
/etc/ssh/sshd_config.d/*.confat the top, and the first value found wins, so a snippet there can override what you edited in the main file. - Wrong host, address or port
- The name may point to an old IP, you may be talking to a different machine, or a router port-forward or container port mapping may be missing.
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 address and port you are using
A wrong host or port is the cheapest thing to rule out, and it produces the same refusal.
- Confirm that HOST is the right address or name, and that it still points to the server you mean (old IPs and stale DNS records are common).
- Ask whoever runs the server which port SSH uses. If it is not 22, pass it on the command line.
ssh -p 2222 user@HOST - To avoid typing the port every time, add a
Portline for that host in~/.ssh/config.
2.Check that sshd is installed and running
The most common cause is simply that the SSH server is missing or stopped.
- Get onto the server through its console, a cloud serial console or another way in, since SSH itself is down.
- On Ubuntu, install the server package if it is missing.
sudo apt install openssh-server - Restart the service. On Debian and Ubuntu the unit is called
ssh.sudo systemctl restart ssh.service - If the service will not start or stays down, read its log.
sudo journalctl -fu ssh.service
3.Validate the sshd configuration
A broken directive stops sshd from starting, which looks exactly like a refusal from outside.
Before you start: Back up the file before editing it: sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak. If you are connected over SSH while editing, make sure you have a second way into the machine in case the change goes wrong.
- Test the configuration. It prints any errors it finds.
sudo sshd -t - Open
/etc/ssh/sshd_configand the files in/etc/ssh/sshd_config.d/and look atPort,ListenAddressandAllowUsers. On Ubuntu the first value found wins, so a snippet in the.dfolder can override the main file.
Check that it worked
ssh user@HOSTgets past the connection stage and shows a login prompt or key exchange instead ofConnection refused.- scp, sftp or git over ssh connects without the refusal message.
If it still fails
- Read the exact last line of the error. If it now says
Permission denied, the connection works and you have a credentials problem instead. - Read the server log with
sudo journalctl -fu ssh.servicewhile you try to connect. No log lines at all can mean the traffic never reaches sshd, so look at port forwarding and the address again. - Service names, defaults and logs differ between systems. For Ubuntu, see the OpenSSH server how-to.
- If the server is a hosted VM, use the provider's console or support to get a shell and confirm the network rules on their side.
Questions
Is "Connection refused" a password or key problem?
No. The refusal happens before authentication starts. Wrong passwords and keys give Permission denied.
Sources
- sshd_config(5) manual pageOfficial · OpenBSD / OpenSSH project
- OpenSSH server how-toOfficial · Ubuntu / Canonical
- Error: Permission denied (publickey)Official · GitHub Docs