Linux Tips guides, explanations and reader checklists on whilenetworking.com

How to Debug ‘Connection Timed Out’ Errors in SSH Sessions Using Network Tools

The error message ssh: connect to host port 22: Connection timed out is one of the most frustrating signals for any Linux administrator or…

The error message ssh: connect to host <hostname> port 22: Connection timed out is one of the most frustrating signals for any Linux administrator or developer. It indicates that a TCP handshake was never completed, meaning your packet requests were either dropped by an intermediate device or never reached their destination. Unlike a “Connection refused” error, which tells you the service exists but rejected the connection, a timeout suggests the network path itself is broken or blocked.

This guide walks IT students and junior engineers through a systematic approach to diagnosing these issues using standard Linux network tools. We will explore firewall rules, routing tables, and service status to resolve connectivity issues safely in production-like environments.

Understanding the Nature of a Timeout

Before running commands, it is crucial to understand what “Connection timed out” actually means in the context of TCP/IP. When you initiate an SSH session, your client sends a SYN (synchronize) packet to the target server’s port 22. If the server receives this packet and has its firewall open, it replies with a SYN-ACK. Your client then responds with an ACK, completing the handshake.

A timeout occurs when no response is received within a specific window of time. This usually happens because:
1. Firewall Blocking: An intermediate firewall (on the server or a perimeter device) is dropping packets without sending a reset signal.
2. Routing Failure: The network path does not know how to reach the destination IP address.
3. Service Unreachable: The SSH daemon is down, and the OS is configured to drop incoming packets rather than acknowledge them (less common on modern Linux distros but possible).

Step 1: Verify Local Network Connectivity

The first step in any troubleshooting workflow is to confirm that your own machine can reach the target. If you cannot ping the server, the issue might be a complete routing failure or a local firewall configuration on your client machine.

Run the following command to test ICMP echo requests (ping):

ping -c 4 <target_ip_address>
  • Expected Result: You should see four lines of replies indicating packet transmission and reception times.
  • Illustrative Output (Success):
    text
    PING <target_ip> (<target_hostname>) 56(84) bytes of data.
    64 bytes from <target_ip>: icmp_seq=1 ttl=64 time=0.2 ms
    ...
    --- <target_ip> ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss

  • Illustrative Output (Failure):
    text
    PING <target_ip> (<target_hostname>) 56(84) bytes of data.
    From <your_machine_ip> icmp_seq=1 Time to live exceeded
    ...
    100% packet loss, time 4000ms

Note: Some security-conscious servers block ICMP (ping) traffic but allow TCP port 22. If ping fails but the issue is specifically an SSH timeout, proceed to step two. Do not assume ping failure equals SSH failure.

Step 2: Check Intermediate Firewall Rules

If you can ping the host but cannot connect via SSH, or if you suspect a perimeter firewall is dropping packets, use tcpdump to inspect traffic at the packet level. This tool captures raw data flowing through your network interface.

First, ensure tcpdump is installed (often part of the net-tools or available via package managers like apt or yum).

sudo tcpdump -i any -nn port 22
  • Explanation: This command listens on all interfaces (any) for packets destined to port 22 (port 22) without resolving hostnames (-nn).
  • Action: On a separate terminal, try to initiate the SSH connection from your client machine.
  • Verification: Watch the output of tcpdump.
    • Scenario A (Success): You will see a SYN packet followed quickly by a SYN-ACK packet.
    • Scenario B (Firewall Drop): You will see the SYN packet enter, but no corresponding SYN-ACK or RST (reset) packet returns within a few seconds. This confirms the packets are being silently dropped.

Step 3: Analyze Server-Side Firewall Configuration

If traffic is dropping, the culprit is often the server’s local firewall. In Linux environments using iptables or nftables, rules can inadvertently block new connections.

To inspect current rules on a server running Debian/Ubuntu:

sudo iptables -L -n -v | grep ESTABLISHED
  • Explanation: This lists all rules and filters for established connections.
  • Common Mistake: Administrators sometimes add a rule that allows only established connections, forgetting to explicitly allow new incoming SSH traffic from external IPs.
  • Fix: Ensure there is an explicit ACCEPT rule for port 22 in the INPUT chain:
    bash
    sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT

For systems using firewalld (common in RHEL/CentOS/Fedora), check the active zones:

sudo firewall-cmd --list-all

Look for services named ssh or ports listed as 22/tcp. If they are missing, add them temporarily to test:

sudo firewall-cmd --add-service=ssh --permanent
sudo firewall-cmd --reload

Step 4: Verify Routing and Interface Status

Sometimes the network interface itself is down, or the routing table has no entry for the destination subnet. Check the server’s primary network interface status:

ip addr show

Ensure the interface associated with your external connection (e.g., eth0 or ens192) has an IP address assigned and is in the UP state. Next, verify the routing table to ensure there is a default gateway or specific route for the destination network:

ip route show

If the default gateway is missing, packets destined for external networks will be dropped. Add a default route if necessary:

sudo ip route add default via <gateway_ip> dev <interface_name>

Step 5: Review SSH Daemon Logs

Finally, if network paths are clear but the connection still times out, the issue may lie with the SSH daemon (sshd) itself. While a timeout usually implies a network issue, sshd might be restarting unexpectedly or bound to a specific interface that is unreachable.

Check the logs for errors preceding the connection attempt:

sudo journalctl -u sshd -n 50 --no-pager
# Or for older systems:
tail -f /var/log/auth.log

Look for messages indicating the service failed to start or is listening only on 127.0.0.1 instead of 0.0.0.0. Ensure the configuration file /etc/ssh/sshd_config allows remote logins and listens on the correct interface.

Student Practice Task

To reinforce these skills, set up a virtual lab using two Virtual Machines (e.g., via VirtualBox or VMware).
1. VM A (Client): Install Ubuntu/Debian.
2. VM B (Server): Install Ubuntu/Debian and disable its firewall (ufw disable) initially to confirm connectivity works.
3. Challenge: Re-enable the firewall on VM B using ufw. Configure it to allow SSH but block all other traffic. Attempt to connect from VM A.
4. Resolution: Use the steps above to identify that while ICMP is blocked, TCP port 22 is open. Then, simulate a routing failure by removing the default gateway in VM B and observe the timeout behavior.

Conclusion

Debugging “Connection timed out” errors requires a methodical elimination of possibilities: local connectivity, intermediate firewalls, server-side firewall rules, routing tables, and daemon status. By using tools like ping, tcpdump, iptables, and journalctl, you can isolate the exact point of failure. Remember that in real-world scenarios, packet loss often occurs silently without notification; monitoring tools are essential for detecting these drops before they impact critical business operations. Always verify your changes in a non-production environment first to avoid locking yourself out of a remote server.

Educational-use notice: This tutorial is for learning and controlled lab use only. Test carefully, use backups, and obtain authorization before making changes. Do not run unfamiliar commands on production systems. We accept no responsibility for misuse, damage, data loss, or production issues.