Linux SSH connection refused: diagnose and fix it
SSH refuses to connect to your Linux server? Test the port, check sshd, open the firewall, and validate the config — in that order.
What this covers
"Connection refused" means your client reached the machine but nothing answered on the SSH port. Work through the port test, the daemon status, the firewall, and the config in order — most cases are resolved by one of these four.
Symptom
Test whether port 22 is reachable
"Connection refused" usually means nothing is listening on the SSH port or a firewall is blocking it. Use netcat against the server's port 22: a succeeded connection means sshd is up and the port is open, so look elsewhere; a refused or timed-out result points at the daemon or firewall. Swap in your server's hostname or IP for the target.
The fastest way to answer 'is the firewall blocking me?' — netcat in scan mode attempts one TCP connection and prints whether it succeeded.
$ nc -zv example.com 443Check whether sshd is actually running
If the port test failed, the most likely cause is that the SSH daemon isn't running. On the server, inspect the sshd unit: the status output shows whether it's active, when it last restarted, and the most recent log lines — a config error or crash will usually be visible right there. If it shows inactive or failed, you've found your answer.
Inspect any systemd unit's loaded/enabled state, active state, main PID, and its most recent log lines in one command.
$ systemctl status nginxOpen port 22 in the firewall
If the daemon is running but the port test still fails, a host firewall like UFW may be blocking inbound SSH. Allow TCP port 22 through UFW, then re-run the port test from your client — it should now succeed. If you need to undo the rule later, the rule can be deleted explicitly. This changes your firewall exposure, so only open ports you intend to serve.
Add a UFW rule to permit inbound traffic on a TCP or UDP port, such as SSH on port 22.
$ sudo ufw allow 22/tcp▾ Undo
$ sudo ufw delete allow 22/tcpValidate sshd_config before restarting
A typo in /etc/ssh/sshd_config can make sshd fail to start — a classic hidden cause of connection refused. Dry-run the configuration with sshd -t before any restart: no output means the config is valid; errors are printed with line numbers so you can fix them. Once it passes, restarting the daemon should bring SSH back, and the port test should connect.
Dry-run the SSH daemon configuration so a typo can't kill your remote access — the standard habit before every systemctl reload sshd.
$ sudo sshd -t
sudo sshd -T | grep -i permitrootloginHow to confirm it worked
Re-run the netcat port test against port 22 — it should report success — and then connect with ssh; you should get a login prompt or shell.
Undo everything
Every command above can be reverted. Run these in order to undo all of it.
$ sudo ufw delete allow 22/tcpReview before running — some steps may not be independent.
Sources
- nc(1) — arbitrary TCP and UDP connections and listens (OpenBSD netcat)Evidence: Documents -z (zero-I/O scan mode) and -v (verbose) for testing if a port is open without sending data
- OpenSSL — ArchWikiEvidence: ArchWiki network troubleshooting pages commonly recommend nc -zv for quick port reachability checks
- Ubuntu Manpage: systemctl - Control the systemd system and service managerEvidence: "status [PATTERN...|URL...] Show terse runtime status information about one or more units ... This function is intended to generate human-readable output ... along with the most recent log data."
- systemctl(1) — Arch Linux manualEvidence: "status [PATTERN...|URL...] — Show terse runtime status information about one or more units, followed by most recent log data from the journal."
- UncomplicatedFirewall - Ubuntu WikiEvidence: The Ubuntu UFW wiki describes enabling the firewall with ufw enable and adding allow rules by port and protocol.
- ufw(8) — program for managing a netfilter firewallEvidence: The ufw manual documents 'ufw allow' rules with PORT/PROTO syntax such as 22/tcp and matching 'ufw delete allow' to remove them.
- sshd(8) — Linux manual pageEvidence: sshd man page: '-t Test mode. Only check the validity of the configuration file and sanity of the keys' and '-T extended test mode printing effective config'.
Last verified: 2026-09-21
Related guides
A step-by-step systemd troubleshooting guide: check status, read the journal, fix the unit file, reload, and restart.
A service starts fine and works on localhost, but remote clients hang or get refused. Work from the client inward: probe the port, check the listener, read the firewalld or ufw ruleset, open the port permanently, then verify with an external scan.
A problem-fix walkthrough for a full Linux disk: find the culprit filesystem, remove space hogs, sweep caches, and verify the space came back.