SSH "Connection refused" on Ubuntu: how to fix it

"Connection refused" means your computer reached the server, but nothing accepted the connection on that port, or a firewall rejected it. On Ubuntu the usual fixes are installing and starting openssh-server, connecting on the right port, and allowing the port in UFW. On Ubuntu 22.10 and later, a changed port can also be ignored because of ssh.socket.

Applies to:UbuntuReleases running openssh-server; socket activation (ssh.socket) applies to 22.10 and later.
On this page

What it means

The message looks like this: ssh: connect to host 192.168.1.50 port 22: Connection refused. Either no program is listening on that address and port, or a firewall is actively rejecting the attempt.

This is not a password or key problem. It happens before the SSH handshake starts, so changing keys or passwords will not help.

Run the checks below on the server itself (a monitor and keyboard, a VM console or your cloud provider's web console), since you cannot log in over SSH yet. Raspberry Pi owners have a dedicated page: SSH connection refused on a Raspberry Pi.

Symptoms and causes

You are likely dealing with this problem if:

  • ssh: connect to host <ip> port 22: Connection refused
  • The same refusal from PuTTY, scp, rsync or git over SSH
  • It began after a fresh install, a port change, enabling UFW, or a reboot or upgrade
  • ssh -vvv user@host shows the connection failing at the TCP stage

The usual causes, most common first:

openssh-server is not installed
Ubuntu Desktop and some images do not ship the SSH server, so nothing listens on port 22.
The ssh service is stopped, disabled or crashed
After a reboot or a failed start, sshd may simply not be running.
sshd listens on a different port
If the server uses for example 2222 and you connect to 22, you get a refusal.
Invalid sshd_config or snippet
A syntax error stops sshd from starting at all.
Port change ignored because of socket activation
On Ubuntu 22.10 and later, systemd's ssh.socket accepts the connections, so editing sshd_config and restarting only ssh.service may not change the port.
UFW or iptables rejecting the port
A firewall rule that rejects port 22 produces exactly this error.

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.Install and start the SSH server

Easy

If openssh-server is missing or the service is stopped, nothing answers on port 22.

  1. Install the server package if it is not there yet.
    Terminalneeds sudo / root
    sudo apt update && sudo apt install openssh-server
  2. Check the service. The service name is ssh. On Ubuntu 22.10 and later also look at ssh.socket.
    Terminalneeds sudo / root
    sudo systemctl status ssh
    sudo systemctl status ssh.socket
  3. Enable it and start it now, then read recent log lines for errors.
    Terminalneeds sudo / root
    sudo systemctl enable --now ssh
    sudo journalctl -u ssh --since "30 minutes ago"

2.Check what is listening and on which port

Easy

Shows whether something listens on the SSH port, and whether it uses a port other than 22.

  1. List what is listening on port 22. When ssh.socket is active (Ubuntu 22.10 and later), the process shown may be systemd rather than sshd, because the socket starts sshd on demand. That is normal.
    Terminalneeds sudo / root
    sudo ss -tlnp 'sport = :22'
  2. Look for Port and ListenAddress lines in the config and its snippets.
    Terminalneeds sudo / root
    sudo grep -iE "^Port|^ListenAddress" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*.conf
  3. If the server uses another port, connect with -p from your client.
    Terminal
    ssh -p 2222 user@host

3.Validate the config and restart

Easy

A broken sshd_config prevents the service from starting.

  1. Test the configuration. No output means it is valid; otherwise fix the line it names.
    Terminalneeds sudo / root
    sudo sshd -t
  2. Restart the service and follow its log.
    Terminalneeds sudo / root
    sudo systemctl restart ssh.service
    sudo journalctl -fu ssh.service
  3. Remember that files in /etc/ssh/sshd_config.d/*.conf are included at the top of the main file, and OpenSSH uses the first value it finds. A snippet therefore overrides sshd_config.

4.Allow SSH in the firewall

EasyChanges settings

A UFW rule rejecting the port gives this exact error.

Before you start: Allow SSH before running ufw enable, otherwise you can lock yourself out of a remote server.

  1. Look at the current rules.
    Terminalneeds sudo / root
    sudo ufw status verbose
  2. Allow SSH on the default port, or on your custom port.
    Terminalneeds sudo / root
    sudo ufw allow ssh
    sudo ufw allow 2222/tcp

5.Fix a port change that is ignored (ssh.socket)

AdvancedChanges settings

On Ubuntu 22.10 and later, systemd listens for SSH through ssh.socket, so editing sshd_config and restarting only ssh.service may not change the port.

Before you start: Keep a console session open while changing the SSH port or listener. A mistake can lock you out of a remote server. The steps below follow the Ubuntu Discourse post and user reports; they are not an official guarantee.

  1. On Ubuntu 24.04, per the Discourse post, nothing is migrated on upgrade: a systemd generator reads the port and address from the sshd config. Set Port in sshd_config or a snippet, then reload systemd and restart the socket.
    Terminalneeds sudo / root
    sudo systemctl daemon-reload
    sudo systemctl restart ssh.socket
  2. Or go back to the classic always-running service, after which Port and ListenAddress in sshd_config work again. This is the revert recipe from the Discourse post. The 00-socket.conf and addresses.conf files only exist on systems upgraded from 22.10 to 23.10; on other systems the rm -f simply does nothing.
    Terminalneeds sudo / root
    sudo systemctl disable --now ssh.socket
    sudo rm -f /etc/systemd/system/ssh.service.d/00-socket.conf /etc/systemd/system/ssh.socket.d/addresses.conf
    sudo systemctl daemon-reload
    sudo systemctl enable --now ssh.service

Check that it worked

  • ssh user@host (or with -p) now asks for a password or key instead of refusing
  • sudo ss -tlnp 'sport = :22' shows a listener on the expected port (the process may be listed as systemd when ssh.socket is active)
  • sudo systemctl status ssh.socket or sudo systemctl status ssh shows it active
  • After a reboot the connection still works

If it still fails

  • Run ssh -vvv user@host from the client and see whether it fails at the TCP stage or later.
  • Read sudo journalctl -u ssh for the exact reason sshd will not start.
  • If a custom port keeps reverting, re-check Port in /etc/ssh/sshd_config.d/ snippets and the ssh.socket state.
  • Ask your hosting provider's support if you cannot reach a console.

Questions

Why does editing the Port in sshd_config do nothing on newer Ubuntu?

On 22.10 and later, ssh.socket accepts connections, so its own settings decide the port. Restart ssh.socket after reloading systemd, or switch back to the classic service.

Is this the same as ERR_CONNECTION_REFUSED in Chrome?

The idea is the same (nothing accepts the connection on that port), but the fix is different. See ERR_CONNECTION_REFUSED in Chrome.

Sources

  1. OpenSSH server | Ubuntu Server documentationOfficial · Canonical
  2. Firewalls (UFW) | Ubuntu Server documentationOfficial · Canonical
  3. SSHd now uses socket-based activation (Ubuntu 22.10 and later)Official · Ubuntu Discourse (Canonical)
  4. How to Troubleshoot SSH 'Connection Refused' Errors on Ubuntu · OneUptime