TechBuzzPro

SSH Connection Failed: How to Find the Fault Stage

· 6 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SSH connection failures usually happen because of a network timeout, a blocked port, or incorrect authentication credentials. To find the fault stage, you must use verbose mode (adding -v, -vv, or -vvv to your command) to see exactly where the handshake stops: at the network layer (Connection timed out), the protocol layer (No supported authentication methods), or the application layer (Permission denied).

What You'll Need

  • A device with an SSH client installed (Terminal on macOS/Linux, PowerShell or Command Prompt on Windows, or an app like PuTTY or Termius).
  • The IP address or hostname of the remote server.
  • The username of the account you are trying to access.
  • Administrative or sudo access to the local machine to check firewall settings.
  • (Optional) Access to the server's cloud console or a physical monitor/keyboard if you are locked out entirely.

Step-by-Step

  1. Enable Verbose Logging
    The most critical step in finding the fault stage is seeing the "conversation" between your client and the server. If you are using a command-line interface, add the verbose flag to your connection command. Use ssh -v username@hostname for basic details, -vv for more, and -vvv for the maximum level of debug information. If you are using a GUI app like PuTTY, look for the "Event Log" or "Session Log" settings to save the output to a file. This allows you to see exactly which packet was sent and where the server stopped responding.
  2. Analyze the Network Layer (The "Ping" Stage)
    If the verbose log hangs at "Connecting to..." or returns "Connection timed out," the fault is at the network layer. The client cannot even reach the server. First, try to ping the server using ping [IP address]. If the ping fails, the server is either offline, the IP address is wrong, or a firewall is dropping all ICMP packets. If the ping works but SSH fails, the specific port (usually 22) is likely blocked by a hardware firewall, a cloud security group, or a local software firewall on the server.
  3. Verify Port Accessibility
    Once you know the server is online, check if the SSH port is actually open. Use a tool like telnet [IP address] 22 or nc -zv [IP address] 22. If the screen goes blank or says "Connected," the port is open, and the fault is further along in the process. If it says "Connection refused," the server is reachable, but the SSH service (sshd) is either not running on the server or is configured to listen on a different port. If it hangs on "Trying...", a firewall is silently dropping the packets.
  4. Check the SSH Handshake (The Protocol Stage)
    If the connection is established but fails shortly after, look at the verbose logs for the "Kex" (Key Exchange) phase. You will see lines like "Remote protocol version..." and "Local protocol version...". If the connection drops here, there may be a mismatch in supported encryption algorithms between an old SSH client and a new server (or vice versa). Ensure your client software is updated to the latest version to support modern ciphers.
  5. Inspect Authentication Methods
    If the logs show "Next authentication method: publickey" followed by "Permission denied (publickey)", the fault is in the authentication stage. This means the server is reachable and the SSH service is working, but it does not recognize your identity. Check if you are pointing to the correct private key file using the -i flag (e.g., ssh -i ~/.ssh/id_rsa username@hostname). If you are using a password, ensure the server is configured to allow password authentication in its sshd_config file.
  6. Validate User Permissions and Shells
    If the authentication is successful but the connection closes immediately after you log in, the fault is at the session stage. This often happens if the user account does not have a valid shell assigned (e.g., it is set to /sbin/nologin or /bin/false) or if there is an error in the user's .bashrc or .zshrc file that crashes the session upon startup. You can test this by trying to run a single command without starting a full shell, such as ssh username@hostname "ls -l".

Common Mistakes

  • Wrong Private Key Permissions: On Linux and macOS, SSH will ignore a private key if its permissions are too open. If you see an error about "unprotected private key file," run chmod 600 ~/.ssh/id_rsa to restrict access to only your user.
  • Using the Wrong Username: Many cloud providers use specific default usernames (like ubuntu, ec2-user, or debian) rather than root. Attempting to connect as root when root login is disabled will result in a "Permission denied" error.
  • Ignoring Firewall Rules: A common mistake is opening the port on the server's internal firewall (like ufw or firewalld) but forgetting to open the port in the cloud provider's external security group or network ACL.
  • Typing the IP Wrong: It sounds simple, but a single digit error in the IP address will lead to a "Connection timed out" error that looks like a complex network failure.

Tips

  • Use a Config File: Instead of typing long commands with keys and ports, create a ~/.ssh/config file. You can define hosts with Host myserver\n HostName 1.2.3.4\n User ubuntu\n IdentityFile ~/.ssh/my_key. Then you can simply type ssh myserver.
  • Check Server Logs: If you have alternative access to the server, check the authentication logs. On most Linux systems, these are located at /var/log/auth.log or /var/log/secure. These logs tell you exactly why the server rejected the connection (e.g., "Invalid user" or "Authentication failed").
  • Test with a Different Network: If you suspect your local network or ISP is blocking port 22, try connecting via a mobile hotspot. If it works on the hotspot but not on your Wi-Fi, the fault is with your local router or network administrator.

Troubleshooting

If you have followed the steps above and still cannot connect, consider these scenarios based on the fault stage you identified:

If it is a Network Fault: Restart the server via your hosting provider's dashboard. Ensure the server has a public IP address and that the IP has not changed. If you are using a VPN, try disconnecting it, as some VPNs block outbound SSH traffic to prevent tunneling.

If it is a Service Fault: If you can access the server via a web-based console, check if the SSH service is running by typing systemctl status ssh or service ssh status. If it is stopped, start it with sudo systemctl start ssh. If it fails to start, check the configuration file for syntax errors using sshd -t.

If it is an Authentication Fault: Verify that your public key is correctly added to the ~/.ssh/authorized_keys file on the server. Ensure the .ssh directory has 700 permissions and the authorized_keys file has 600 permissions. If you are using a password and it is being rejected, check if PasswordAuthentication yes is set in the server's /etc/ssh/sshd_config file.

Frequently Asked Questions

What does "Connection refused" actually mean?

It means the server is reachable and responded, but it actively rejected the connection. This usually happens because the SSH service is not running or the server is not listening on the port you are trying to use.

Why does it say "Connection timed out"?

This means the client sent a request but received no response at all. This is almost always caused by a firewall (local, network, or server-side) dropping the packets or the server being completely powered off.

Can I change the SSH port to fix connection issues?

Changing the port (e.g., to 2222) can help avoid automated bot attacks, but it won't fix a broken configuration. If you change it, you must update your firewall rules and connect using ssh -p 2222 username@hostname.

What is the difference between -v, -vv, and -vvv?

The more "v"s you add, the more detail you get. -v shows basic connection steps; -vv shows the authentication process and key exchange; -vvv provides deep debugging information about the internal state of the SSH client.

Why does my key work on one computer but not another?

This is usually because the private key file was not copied correctly, the file permissions are too open (needs chmod 600), or the SSH agent is not running on the second computer to manage the key.