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.

Applies to:OpenSSH server on Debian/Ubuntu-based LinuxCommands below use Debian/Ubuntu names and paths
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 refused right 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 Port and ListenAddress in sshd_config can change that, and several values are allowed. A server with ListenAddress 127.0.0.1 only 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/*.conf at 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

Easy

A wrong host or port is the cheapest thing to rule out, and it produces the same refusal.

  1. 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).
  2. Ask whoever runs the server which port SSH uses. If it is not 22, pass it on the command line.
    Terminal
    ssh -p 2222 user@HOST
  3. To avoid typing the port every time, add a Port line for that host in ~/.ssh/config.

2.Check that sshd is installed and running

Easy

The most common cause is simply that the SSH server is missing or stopped.

  1. Get onto the server through its console, a cloud serial console or another way in, since SSH itself is down.
  2. On Ubuntu, install the server package if it is missing.
    Terminalneeds sudo / root
    sudo apt install openssh-server
  3. Restart the service. On Debian and Ubuntu the unit is called ssh.
    Terminalneeds sudo / root
    sudo systemctl restart ssh.service
  4. If the service will not start or stays down, read its log.
    Terminalneeds sudo / root
    sudo journalctl -fu ssh.service

3.Validate the sshd configuration

ModerateChanges settings

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.

  1. Test the configuration. It prints any errors it finds.
    Terminalneeds sudo / root
    sudo sshd -t
  2. Open /etc/ssh/sshd_config and the files in /etc/ssh/sshd_config.d/ and look at Port, ListenAddress and AllowUsers. On Ubuntu the first value found wins, so a snippet in the .d folder can override the main file.

Check that it worked

  • ssh user@HOST gets past the connection stage and shows a login prompt or key exchange instead of Connection 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.service while 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

  1. sshd_config(5) manual pageOfficial · OpenBSD / OpenSSH project
  2. OpenSSH server how-toOfficial · Ubuntu / Canonical
  3. Error: Permission denied (publickey)Official · GitHub Docs