When an SSH connection fails, the error message tells you a lot about where the problem lies. “Connection refused” means your client reached the target server, but nothing is listening on the specified port (usually 22). “Connection timed out” means your client sent packets but never got any response, indicating a network-level blockage or the server being unreachable. Your first checks should be: for refused, verify the SSH service is running and listening on the expected port; for timed out, check firewalls, routing, and IP reachability.
Background: Why SSH Connections Fail
SSH (Secure Shell) is the standard protocol for remote administration of Unix-like systems. When you run ssh user@host, your client goes through several steps: DNS resolution, TCP handshake, and then the SSH protocol handshake. Failures can occur at any of these stages, but the two most common error messages—”connection refused” and “connection timed out”—point to different stages. Understanding the TCP/IP model helps: refused is a Layer 4 (transport) response from the destination, while timed out usually indicates a Layer 3 (network) problem or a firewall silently dropping packets.
How “Connection Refused” Works
When your SSH client sends a TCP SYN packet to the server’s IP and port, the server’s operating system responds. If no process is listening on that port, the OS sends back a TCP RST (reset) packet. Your client then displays “connection refused.” This is a fast, definitive answer—it means the server is alive and reachable, but the SSH daemon (sshd) is not accepting connections.
Common causes include:
- SSH service (sshd) is not running.
- SSH is listening on a different port than the one you’re connecting to.
- A local firewall on the server (like iptables or firewalld) is rejecting connections with a RST rather than dropping them.
- The server’s IP address is correct, but you’re connecting to the wrong port.
How “Connection Timed Out” Works
A timeout means your client sent the TCP SYN packet but never received any response—neither a SYN-ACK (success) nor a RST (refused). The packet is being silently discarded somewhere between your client and the server. This could be due to:
- A firewall (on your client, on the server, or in between) dropping packets without responding.
- The server IP is unreachable (wrong IP, server down, or network route missing).
- Network congestion or packet loss causing the SYN to never arrive.
- Your client’s outbound firewall blocking SSH traffic.
Timeouts are inherently slower—your client waits for the TCP timeout period (often 30–120 seconds) before giving up.
First Checks for Each Error
For connection refused, do these in order:
- Verify the SSH service is running:
sudo systemctl status ssh(orsshd). - Check what port sshd is listening on:
sudo netstat -tlnp | grep sshorss -tlnp | grep ssh. - Try connecting to the correct port:
ssh -p 2222 user@hostif you changed it. - Test locally on the server:
ssh localhost—if that fails, the service is down.
For connection timed out, do these:
- Ping the server:
ping host—if no reply, the host is unreachable or ICMP is blocked. - Check your own firewall:
sudo iptables -Lorsudo ufw status. - Check if the server’s firewall is dropping packets: try a port scan with
nc -zv host 22(if allowed). - Verify routing:
traceroute hostto see where packets stop.
Common Misconception: Firewall Behavior
A frequent source of confusion is that firewalls can be configured to either reject or drop packets. A reject rule sends back a RST, producing “connection refused.” A drop rule silently discards the packet, producing “connection timed out.” So if you see “refused” from a remote server, it’s not necessarily that sshd is down—a firewall might be actively rejecting. Conversely, a “timeout” doesn’t always mean the server is offline; a firewall could be dropping all incoming SSH traffic.
Diagnostic Tools and Commands
Beyond the basic checks, several tools help pinpoint the issue:
nc -zv host port(netcat) — tests TCP connectivity and shows “succeeded” or “refused”.telnet host port— similar, but may hang on timeout.tcpdump -i any port 22— captures packets to see if SYN packets arrive and what responses come back.ssh -v user@host— verbose mode shows exactly where the connection stalls.
For example, if ssh -v shows “Connecting to host [IP] port 22” and then hangs, that’s a timeout. If it shows “Connection refused” immediately, the server responded with RST.
Frequently Asked Questions
Why does SSH say “connection refused” when the server is up?
Most likely sshd is not running, or it’s listening on a non-default port. Check with systemctl status ssh and ss -tlnp. Also, some cloud providers use a custom port for security—check your server documentation.
Can a firewall cause “connection refused”?
Yes, if the firewall is configured to reject (send RST) rather than drop. Many default firewall rules on Linux use REJECT, which results in “refused.” To see if that’s the case, temporarily disable the firewall (if you have console access) and retry.
Why does SSH time out only from outside my network?
This usually indicates a network firewall or router blocking port 22. Check your router’s port forwarding and any cloud security groups (if using a cloud provider). Also, some ISPs block inbound port 22 on residential connections.
How long should I wait before assuming a timeout?
Default TCP timeout in Linux is about 2 minutes, but SSH clients often have their own ConnectTimeout option. You can set it to 10 seconds: ssh -o ConnectTimeout=10 user@host. If it still times out, the network path is blocked.
Is “connection refused” faster than “timed out”?
Yes, typically. Refused is immediate because the server sends a RST as soon as the SYN arrives. Timeout requires waiting for the full TCP retransmission timeout, which can be 30 seconds or more.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Skip Ryobi and Milwaukee? Harbor Freight Tools to Buy
- Funny Subreddits That Make You Laugh
- Why Retro Gaming Is Still Worth Playing Today
- Neptune Facts That Will Blow Your Mind
- Survival Crafting Game Fatigue: Why & How to Fix It
- Sell Your EV Now Before Prices Plummet?