Over a million developers have joined DZone.

4 Reasons Why SSH Connections Fail

DZone's Guide to

4 Reasons Why SSH Connections Fail

Want to ease the pain and burden of figuring out why people can't SSH to servers? Let’s examine common SSH failures together.

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

As DevOps or IT professionals, people may ask us why they can’t SSH to servers. It happens from time to time. Not much fun. Just routine work.

Want to ease the pain and burden? Let’s examine common SSH failures together.

Share this link with others who might find it useful. They may be able to identify the root cause all by themselves or be efficient in collecting all necessary information before turning to us.

Image title

It’s not something fancy or difficult. Just not everyone possesses enough information or experience about this. As DevOpsers, we shouldn’t stand in the way for any process. Let’s empower people with a simple and easy guide.

Here are four common SSH failures sorted by frequency.

1. Our SSH Public Key Is Not Injected to Servers

SSH by password is very dangerous. Nowadays, almost all serious servers will only accept SSH by a key file. Here is the process:

  • We generate an SSH key pair (even better, protect the private key with a passphrase).
  • Send our SSH public key to the person who manages the servers.
  • This person will inject our SSH public key there (usually, it’s ~/.ssh/authorized_keys).
  • Then, we should be able to SSH.

Here comes the most frequent SSH failure!

denny@laptop:/# ssh root@www.dennyzhang.com
Permission denied (publickey).

This error message may have two possible clauses:

  1. The private key doesn’t have the privilege to log in. Either the public key is not injected correctly or simply it’s missing.

    Note: If your Ops/DevOps are not available, you can try alternatives. Think about who else in the team can SSH. In fact, anyone who can SSH is able to perform the change.
  2. The local SSH public key and private key are not correctly paired.

Before connecting, the SSH will check whether our public key and private key are correctly paired. If not, it will reject to use the private key silently. Yes, silently!

You may wonder how this could happen. As humans, we don’t let it happen, but we may have some automation scripts that create the mess. (BTW, if we only have a valid private key without a public key, it’s fine.)

2. Firewall Prevents Us From Connecting

For security concerns, people may enforce a strict firewall policy. It means only certain IPs can SSH.

denny@laptop:/# ssh root@www.dennyzhang.com
ssh: connect to host www.dennyzhang.com port 22: Connection refused

# Confirm with telnet. Usually it shall connect in seconds
denny@laptop:/# telnet www.dennyzhang.com

You may want to fetch help immediately. Just wait a second.

People may have reconfigured SSHD to listen on other ports. Are you sure it’s port 22? Even better, double check the server IP and DNS name.

I know they might be stupid questions, but people make these mistakes sometimes.

Once it’s confirmed, talk to your DevOps. There is another possible reason for this failure: SHHD is not up and running. Very rare, I would say, but this could be it. In that case, DevOps and Ops need to take actions immediately.

3. Host Key Check Fails

When you see the below warning for the first time, you may get confused. To be simple, it helps us to avoid the attack of the man in the middle.

denny@laptop:/# ssh root@www.dennyzhang.com
The ECDSA host key for [www.dennyzhang.com]:22 has changed,
and the key for the corresponding IP address []:22
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /root/.ssh/known_hosts:2
  remove with: ssh-keygen -f "/root/.ssh/known_hosts" -R [www.dennyzhang.com]:22
ECDSA host key for [www.dennyzhang.com]:22 has changed and you have requested strict checking.
Host key verification failed.

Each server can have a fingerprint. If the server is re-provisioned or it's simply a different server, the fingerprint would be different. Once we have successfully logged in, our laptop will save the server’s fingerprint locally. Next time we log in, it will do a comparison first. If the fingerprint doesn’t match, we will see the warning.

If we’re confident it has been re-provisioned recently, we can ignore this warning. Remove the entry from ~/.ssh/known_hosts, or you can empty the file. You can even turn off the SSH host key checking for all hosts (certainly, I would not recommend this).

4. Your SSH Key File Mode Issues

As a self-protection, the file access of your ssh key file can’t be widely open. The file mode should be either 0600 or 0400.

denny@laptop:/# ssh -i id_rsa root@www.dennyzhang.com
Permissions 0644 for 'id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: id_rsa
Permission denied (publickey).

Use -v for verbose output: ssh -v $user@$server_ip.

Original article here.

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

performance ,ssh ,ssh connections ,connection failure

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}