Sub topic 5
Reading SSH failures: timeouts, refused connections, permission errors
This chapter assumes you can run ssh from a terminal. It also assumes you know what a security group, a key pair, and a public subnet are. Each SSH failure message points to a different layer, so reading it correctly tells you where to look first.
Start with the symptom
Run the connection with -v to see how far it gets:
ssh -v -i ~/.ssh/my-key.pem [email protected]The last lines of output show whether the TCP connection opened, whether the server key was accepted, and which keys were offered.
Connection timed out
A timeout means your packets never got an answer. Something dropped them silently. Check these in order:
- The instance has a public IP address, or you are connecting from inside the VPC.
- The subnet route table has a route to an internet gateway.
- The security group allows inbound TCP 22 from your current address.
- The network ACL allows port 22 inbound and the ephemeral ports outbound.
The security group is the most common cause, especially after your home or office IP address changes. Add a rule for your own address only:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 22 \
--cidr 203.0.113.25/32Connection refused
"Connection refused" is a different failure. Your packets reached the instance, and the operating system replied that nothing is listening on that port. The network path works. Look at the instance itself:
sshdis stopped or failed to start. Check the instance's system log in the console.sshdlistens on a non-default port set by a hardened AMI or user data script.- A bad change to
sshd_configstopped the service from restarting.
If you cannot log in to repair it, use Session Manager or EC2 Instance Connect, both covered later.
Permission errors
These errors mean the network and sshd are fine, and authentication failed.
| Message | Cause | Fix |
|---|---|---|
| `WARNING: UNPROTECTED PRIVATE KEY FILE!` | Other users can read the key file | `chmod 400` the key |
| `Permission denied (publickey)` | Wrong login user, wrong key, or a key that does not match the instance's key pair | Use the right user and key |
| `Host key verification failed` or `REMOTE HOST IDENTIFICATION HAS CHANGED` | A new instance now has an address you used before | Confirm the change is expected, then remove the old entry |
Fix the file mode and retry with the correct default user for the AMI:
chmod 400 ~/.ssh/my-key.pem
ssh -i ~/.ssh/my-key.pem [email protected]Amazon Linux uses ec2-user. Ubuntu uses ubuntu, and Debian uses admin. A wrong user name is the most frequent cause of Permission denied (publickey). In the -v output, look for lines like Offering public key followed by Authentications that can continue. They show which keys your client tried.
For the host key warning, remove the stale entry only after you are sure the instance was replaced:
ssh-keygen -R 203.0.113.25Quick reference
- A timeout points to the network. Check the security group first.
- A refusal points to
sshdon the instance. - A permission error points to the user name, the key, or the key file mode.
Exercise
You run ssh and it prints "Connection refused" immediately. A colleague suggests adding your IP to the security group. Why is that unlikely to help?
Show answer
A refusal means your packets reached the instance and the operating system answered that nothing is listening on the port. A missing security group rule would drop the packets and cause a timeout instead. Check that sshd is running and which port it listens on.
Exercise
You get "Permission denied (publickey)" on an Ubuntu instance while using ec2-user and the correct key. What do you change?
Show answer
Change the login user to ubuntu. The key is valid, but Ubuntu AMIs have no ec2-user account.