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.
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@hostshows 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
If openssh-server is missing or the service is stopped, nothing answers on port 22.
- Install the server package if it is not there yet.
sudo apt update && sudo apt install openssh-server - Check the service. The service name is
ssh. On Ubuntu 22.10 and later also look atssh.socket.sudo systemctl status ssh sudo systemctl status ssh.socket - Enable it and start it now, then read recent log lines for errors.
sudo systemctl enable --now ssh sudo journalctl -u ssh --since "30 minutes ago"
2.Check what is listening and on which port
Shows whether something listens on the SSH port, and whether it uses a port other than 22.
- List what is listening on port 22. When
ssh.socketis active (Ubuntu 22.10 and later), the process shown may besystemdrather thansshd, because the socket starts sshd on demand. That is normal.sudo ss -tlnp 'sport = :22' - Look for
PortandListenAddresslines in the config and its snippets.sudo grep -iE "^Port|^ListenAddress" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*.conf - If the server uses another port, connect with
-pfrom your client.ssh -p 2222 user@host
3.Validate the config and restart
A broken sshd_config prevents the service from starting.
- Test the configuration. No output means it is valid; otherwise fix the line it names.
sudo sshd -t - Restart the service and follow its log.
sudo systemctl restart ssh.service sudo journalctl -fu ssh.service - Remember that files in
/etc/ssh/sshd_config.d/*.confare included at the top of the main file, and OpenSSH uses the first value it finds. A snippet therefore overridessshd_config.
4.Allow SSH in the firewall
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.
- Look at the current rules.
sudo ufw status verbose - Allow SSH on the default port, or on your custom port.
sudo ufw allow ssh sudo ufw allow 2222/tcp
5.Fix a port change that is ignored (ssh.socket)
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.
- 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
Portinsshd_configor a snippet, then reload systemd and restart the socket.sudo systemctl daemon-reload sudo systemctl restart ssh.socket - Or go back to the classic always-running service, after which
PortandListenAddressinsshd_configwork again. This is the revert recipe from the Discourse post. The00-socket.confandaddresses.conffiles only exist on systems upgraded from 22.10 to 23.10; on other systems therm -fsimply does nothing.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 refusingsudo ss -tlnp 'sport = :22'shows a listener on the expected port (the process may be listed assystemdwhen ssh.socket is active)sudo systemctl status ssh.socketorsudo systemctl status sshshows it active- After a reboot the connection still works
If it still fails
- Run
ssh -vvv user@hostfrom the client and see whether it fails at the TCP stage or later. - Read
sudo journalctl -u sshfor the exact reason sshd will not start. - If a custom port keeps reverting, re-check
Portin/etc/ssh/sshd_config.d/snippets and thessh.socketstate. - 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
- OpenSSH server | Ubuntu Server documentationOfficial · Canonical
- Firewalls (UFW) | Ubuntu Server documentationOfficial · Canonical
- SSHd now uses socket-based activation (Ubuntu 22.10 and later)Official · Ubuntu Discourse (Canonical)
- How to Troubleshoot SSH 'Connection Refused' Errors on Ubuntu · OneUptime