SSH "Connection refused" on port 22 with a Raspberry Pi: how to fix it

"Connection refused" on port 22 means the Pi answered, but nothing is accepting SSH connections. On a fresh Raspberry Pi OS install the cause is usually that SSH is disabled by default, so enabling it fixes the problem.

Applies to:Raspberry Pi OSAll Raspberry Pi models
On this page

What it means

The message ssh: connect to host … port 22: Connection refused tells you something useful: your computer did reach the Pi, and the Pi's network stack replied. What failed is the last step. Either no program is listening on port 22, or a firewall is actively turning the connection away.

That separates it from "Connection timed out" (wrong address, Pi switched off, or packets dropped) and from "No route to host". If you see one of those instead, start by checking the address and the network. For the similar browser-side error, see ERR_CONNECTION_REFUSED in Chrome.

There is no Pi-specific bug behind this. The Raspberry Pi documentation states that Raspberry Pi OS disables the SSH server by default, which is why it so often shows up on a new image.

Symptoms and causes

You are likely dealing with this problem if:

  • ssh [email protected] or ssh user@<ip address> ends with Connection refused.
  • ping to the Pi works.
  • It started after flashing a fresh SD card, reflashing, or changing a configuration file.

The usual causes, most common first:

SSH is not enabled
Raspberry Pi OS ships with the SSH server turned off. This is the best documented cause and the most likely one on a new install.
SSH listens on a different port
The Port setting in sshd_config defaults to 22 but can be changed, so connecting to 22 gets refused.
Wrong device or address
The IP or the .local name may point at another 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.Enable SSH on the Pi

Easy

SSH is off by default on Raspberry Pi OS, so this fixes the most common case.

  1. If you have a screen on the Pi desktop, open Preferences, then Control Centre, then the Interfaces tab. Set SSH to Enabled and click OK.
  2. Or, in a terminal on the Pi, run the configuration tool and choose SSH, then Yes, Ok and Finish.
    Terminal
    sudo raspi-config
  3. Headless, with the SD card in another computer: create an empty file named ssh in the boot volume that appears when you insert the card. Then eject the card safely, put it in the Pi and boot it. No commands are needed on the Pi for this case.
  4. Headless, but you can already log in on the Pi (for example with a keyboard and screen): create the same empty file from the Pi's own boot partition and reboot. The documentation shows this command for current releases.
    Terminal
    sudo touch /boot/firmware/ssh
    sudo reboot
  5. Setting up a new card? In Raspberry Pi Imager, go to Customisation, then Remote Access, and turn on Enable SSH. Choose password or public-key authentication.

2.Confirm you are connecting to the right address

Easy

A wrong IP or a .local name resolving to another machine gives the same symptom.

  1. Ping the default hostname, or find the Pi's IP address in your router's device list.
    Terminal
    ping raspberrypi.local
  2. Connect with your username and the IP address. Type yes at the first-connection prompt.
    Terminal
    ssh <username>@<ip address>

3.Restart the ssh service

Moderate

If SSH is enabled but the service has stopped, a restart can bring it back. You need a keyboard and monitor, or another way to reach the Pi's console.

  1. Restart the service on the Pi.
    Terminal
    sudo systemctl restart ssh

4.Connect on the right port

Moderate

If someone changed Port in sshd_config, port 22 will refuse you.

  1. Look for Port lines in /etc/ssh/sshd_config on the Pi, then connect with that number.
    Terminal
    ssh -p <port> <username>@<ip address>

Check that it worked

  • Running ssh <username>@<ip address> from your computer shows the host key prompt or a password prompt instead of Connection refused.
  • After you accept the host key and sign in, you get the Pi's command prompt.

If it still fails

  • If you now get Permission denied, the connection works and the problem is the login. Check the username and password, or set up the user through Raspberry Pi Imager's customisation options.
  • For other remote-connection errors, see Remote Desktop error 0x204.

Sources

  1. Remote access – Raspberry Pi DocumentationOfficial · Raspberry Pi Ltd
  2. sshd_config(5) manual pageOfficial · OpenBSD