Understanding Why SSH Connections to Host Port 22 Time Out
An SSH connection timeout to port 22 generally occurs because the client machine cannot establish a TCP handshake with the target server. This failure is typically caused by network-level obstructions, such as active firewalls, incorrectly configured security...
An SSH connection timeout to port 22 generally occurs because the client machine cannot establish a TCP handshake with the target server. This failure is typically caused by network-level obstructions, such as active firewalls, incorrectly configured security groups, routing issues, or the SSH service being unresponsive on the target host.
Contents
When you attempt to connect to a remote server using Secure Shell (SSH) and receive a timeout error, it indicates that the packets sent from your local machine to the server are not receiving a response within the allotted time. Unlike a "Connection Refused" error—which suggests the server received your request but actively rejected it—a timeout means your request effectively vanished into the network or was silently dropped by a security mechanism.
The Mechanism of an SSH Timeout
To understand why the connection fails, it is necessary to examine the TCP three-way handshake. When you execute an ssh command, your client sends a SYN (synchronize) packet to the server on port 22. The server should respond with a SYN-ACK (synchronize-acknowledgment) packet. If your client waits for a response and the server never sends one, or if intermediate network equipment drops the packet, the client eventually ceases waiting and reports a timeout.
This behavior is distinct from application-layer errors. Because the issue occurs at the transport layer (TCP), you are not yet communicating with the SSH daemon (sshd). Therefore, incorrect credentials or disabled user accounts are rarely the cause of a timeout; the problem is almost always located in the path between your device and the destination server.
Network and Firewall Configurations
The most common cause of a timeout is a firewall blocking incoming traffic on port 22. This can occur at several layers of the network infrastructure.
Cloud Security Groups and Network ACLs
If you are managing servers in a cloud environment, such as AWS, Google Cloud, or Azure, the primary suspects are Security Groups or Network Access Control Lists (NACLs). These act as virtual firewalls. If the inbound rule for TCP port 22 does not explicitly allow traffic from your specific IP address (or from 0.0.0.0/0 for global access), the cloud provider’s infrastructure will drop your packets before they ever reach your virtual machine instance.
Host-Based Firewalls
Even if the cloud security group permits the traffic, a local firewall running on the operating system—such as iptables, nftables, or ufw—might be configured to reject or drop incoming connections on port 22. In many default server hardening configurations, administrators disable all incoming ports by default, requiring specific rules to allow SSH access.
Routing and Connectivity Issues
If firewalls are confirmed to be permissive, the problem may reside in the routing path or the internet connection itself.
- IP Address Changes: Your server might have had its public IP address reassigned, or you might be connecting from a new network that is behind a strict NAT (Network Address Translation) gateway or a corporate VPN that blocks non-standard outbound traffic.
- ISP Restrictions: Some Internet Service Providers, particularly those offering residential connections, may block port 22 to mitigate brute-force attacks or abuse originating from home networks.
- Server Unresponsiveness: It is possible that the server itself is experiencing a kernel panic, a frozen operating system, or a full resource exhaustion (CPU/RAM), preventing it from responding to any network requests at the transport level.
Troubleshooting and Verification
To diagnose the specific point of failure, you should use diagnostic tools to isolate whether the issue is local, intermediary, or host-based.
| Tool | Purpose | What to Look For |
|---|---|---|
ping |
Basic connectivity | Packet loss or failure to resolve the host. |
traceroute |
Path analysis | Where the packets stop along the network route. |
nc -zv |
Port scanning | Whether port 22 is open, closed, or filtered. |
ssh -vvv |
Verbose debug | Detailed logs of the handshake process. |
Using the ssh -vvv flag is particularly effective. It provides a step-by-step breakdown of the connection attempt. If you see the client hanging at "Connecting to host...", it confirms that no data is returning from the server, reinforcing the diagnosis of a blocked or dropped connection.
Addressing Common Causes
If you have narrowed the issue down to a specific layer, follow these methodical steps to resolve it.
- Verify the IP Address: Ensure that you are attempting to connect to the correct, current public IP address of the server. Cloud instances often receive new public IPs upon restart unless an Elastic or Reserved IP is assigned.
- Inspect Cloud Security Rules: Log into your hosting provider’s dashboard. Navigate to the networking section and verify that an inbound rule exists for TCP port 22. Ensure the source IP matches your current network’s public IP.
- Check the Physical/Virtual Console: If the server is unreachable via SSH, use the out-of-band management console provided by your hosting service (e.g., AWS EC2 Instance Connect or a VNC/Serial console). This allows you to log in as if you were sitting at the machine, bypassing the network SSH requirements. Once logged in, check the status of
sshdusingsystemctl status sshand verify local firewall rules. - Analyze Local Network: Test the connection from a different network (e.g., using a mobile hotspot) to rule out your local ISP or office firewall as the source of the blockage.
Frequently Asked Questions
Why does the connection time out only intermittently?
Intermittent timeouts often suggest network congestion, unstable ISP routing, or a stateful firewall that closes connections due to inactivity. If you are on a poor-quality network, your packets might simply be dropped during bursts of traffic.
Could my SSH key be the reason for the timeout?
No. An SSH key mismatch or an incorrect password will result in a "Permission Denied" or "Authentication Failed" error. Because these errors occur after a successful TCP handshake, they prove that the network path is open. A timeout occurs before this stage.
What is the difference between Connection Refused and Connection Timed Out?
"Connection Refused" means your packet reached the server, and the server explicitly replied saying nothing is listening on port 22. "Timed Out" means your packet never got a response, implying it was dropped by a firewall or the server is completely offline.
Can I change the SSH port to avoid timeouts?
You can change the SSH port in /etc/ssh/sshd_config, but this will not fix a underlying network timeout. If port 22 is being blocked by a firewall, simply moving the service to another port may bypass the filter temporarily, but it does not address the root cause of the network obstruction.
Recommended Next Actions
If you remain unable to connect, prioritize gaining access via your hosting provider’s out-of-band console. This is the only way to confirm if the sshd process is actually running and to inspect local firewall logs without relying on the network interface that is currently failing. Once you have access to the internal terminal, check the logs located at /var/log/auth.log or /var/log/secure to see if the server is rejecting your requests or if the traffic is failing to arrive entirely.