How to fix "ssh: connect to host port 22: Connection refused"
Tested: we reproduced this error and confirmed the fix on Ubuntu 24.04 with OpenSSH 9.6p1, client and server, November 29, 2026.
Connection refused means your computer reached the server, and the server said nothing is listening on that port. Check these three things on the server, in order:
- sshd is running:
sudo systemctl status ssh(orsshdon Fedora, RHEL and Arch). - It listens on the port you’re using:
sudo ss -tlnp | grep ssh(orsudo netstat -tlnpon older systems). If it shows:2222, connect withssh -p 2222 user@host. - A firewall isn’t rejecting the port:
sudo ufw statuson Ubuntu, or your cloud provider’s security group or firewall rules.
We reproduced the error both ways it usually happens, with OpenSSH 9.6: connecting to a port where nothing listens, and connecting after stopping sshd. Both printed the same line:
ssh: connect to host 127.0.0.1 port 2222: Connection refused
Test the port from your own machine
Before changing anything on the server, check whether the port answers at all:
nc -zv example.com 22
In our test against a port with nothing listening, this printed nc: connect to 127.0.0.1 port 2299 (tcp) failed: Connection refused, the same answer SSH gets. A working port prints succeeded or open. If nc hangs and then gives up, you have a timeout instead, which points to a firewall, a wrong IP or a server that’s off.
Find your cause
| Cause | How to tell | Fix |
|---|---|---|
| SSH server not installed | systemctl status ssh says the unit doesn’t exist |
sudo apt install openssh-server |
| sshd stopped or crashed | Status shows inactive or failed |
sudo systemctl enable --now ssh |
| Wrong port | sshd listens on another port | ssh -p PORT user@host or Port in ~/.ssh/config |
| Firewall rejects the port | sshd is running but you’re still refused | sudo ufw allow OpenSSH, or allow TCP 22 in the cloud firewall |
| Server still booting or rebooting | It worked a minute ago | Wait 30 to 60 seconds and try again |
| Wrong machine | The IP now belongs to something else | Check the IP in your cloud console |
| Raspberry Pi with SSH never turned on | Fresh Raspberry Pi OS install | Turn on SSH in sudo raspi-config (Interface Options) or in Raspberry Pi Imager |
Ubuntu: you changed Port but SSH still listens on 22 |
ss -tlnp shows :22, not your port |
sudo systemctl daemon-reload then sudo systemctl restart ssh.socket |
Fix: start the SSH server
On Ubuntu and Debian:
sudo apt install openssh-server
sudo systemctl enable --now ssh
sudo systemctl status ssh
On Fedora and RHEL, the package is also openssh-server and the service is sshd. If the service fails to start, sudo journalctl -u ssh -n 50 shows why; a typo in /etc/ssh/sshd_config is common, and sudo sshd -t checks the file.
If you can’t log in to start it, use your provider’s web console or recovery mode.
Fix: use the right port
sudo ss -tlnp | grep ssh
Connect to the port it shows:
ssh -p 2222 user@example.com
To stop typing it, add a block to ~/.ssh/config:
Host myserver
HostName example.com
Port 2222
User ubuntu
Ubuntu 22.10 and later: the port didn’t change
Ubuntu starts SSH through ssh.socket rather than the ssh service. If you edit Port in /etc/ssh/sshd_config and only restart ssh, the server keeps listening on 22, so connecting to the new port is refused. Reload systemd and restart the socket:
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
sudo ss -tlnp | grep ssh
This is documented in Ubuntu bug 2069041. We didn’t reproduce it, because our test server ran sshd without systemd.
Fix: SSH is turned off on a Raspberry Pi
Raspberry Pi OS ships with SSH turned off, a default the Raspberry Pi team introduced in 2016. With a screen attached, run sudo raspi-config, open Interface Options and turn on SSH. For a headless setup, turn on SSH in Raspberry Pi Imager’s settings before writing the card.
Fix: the firewall
On the server, with ufw:
sudo ufw allow OpenSSH # or: sudo ufw allow 2222/tcp
sudo ufw status
Without ufw, list the raw rules with sudo iptables -L -n (or sudo nft list ruleset) and look for a REJECT on your SSH port. In our test, with a server listening on the port, an iptables REJECT rule made SSH print Connection refused and a DROP rule made it print Connection timed out.
For a server at home, the router also has to forward the SSH port to the server’s local IP. If SSH works from inside your network but is refused from outside, check the port forwarding rule.
On AWS, Azure, Google Cloud or DigitalOcean, also check the instance’s security group or firewall allows inbound TCP on your SSH port from your IP. A cloud firewall that drops traffic usually gives a timeout rather than a refusal, so if you see Connection refused, look at the server first.
Confirm it worked
From your machine, ssh -v user@host should get past Connecting to host [address] port 22 and show Connection established. If you now see Permission denied (publickey), the connection works and the problem is your key: see Permission denied (publickey).
FAQ
What is the difference between connection refused and timed out?
Refused means the machine answered and said nothing is listening on that port. Timed out means no answer came back at all, which usually points to a firewall dropping packets, a wrong IP or a server that's down. Our SSH errors guide groups the timeout and refused cases.
How do I check if sshd is running?
On most Linux servers: sudo systemctl status ssh (Ubuntu and Debian) or sudo systemctl status sshd (Fedora, RHEL and Arch). sudo ss -tlnp | grep ssh shows the port it listens on.
Why does SSH work on one network and not another?
A firewall or router between you and the server is rejecting port 22 on one network. Some office and public networks block outgoing SSH. Try another network or a VPN, or run SSH on port 443 if you control the server.
I'm connecting to my own Mac. Why is it refused?
macOS doesn't run an SSH server until you turn on Remote Login in System Settings, General, Sharing.
