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.
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]orssh user@<ip address>ends withConnection refused.pingto 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
Portsetting insshd_configdefaults to 22 but can be changed, so connecting to 22 gets refused. - Wrong device or address
- The IP or the
.localname 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
SSH is off by default on Raspberry Pi OS, so this fixes the most common case.
- 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.
- Or, in a terminal on the Pi, run the configuration tool and choose SSH, then Yes, Ok and Finish.
sudo raspi-config - Headless, with the SD card in another computer: create an empty file named
sshin 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. - 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.
sudo touch /boot/firmware/ssh sudo reboot - 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
A wrong IP or a .local name resolving to another machine gives the same symptom.
- Ping the default hostname, or find the Pi's IP address in your router's device list.
ping raspberrypi.local - Connect with your username and the IP address. Type
yesat the first-connection prompt.ssh <username>@<ip address>
3.Restart the ssh service
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.
- Restart the service on the Pi.
sudo systemctl restart ssh
4.Connect on the right port
If someone changed Port in sshd_config, port 22 will refuse you.
- Look for
Portlines in/etc/ssh/sshd_configon the Pi, then connect with that number.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 ofConnection 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
- Remote access – Raspberry Pi DocumentationOfficial · Raspberry Pi Ltd
- sshd_config(5) manual pageOfficial · OpenBSD