Sub topic 2
Diagnosing EC2 Access Failures
Assume you know basic Linux, EC2, VPC, IAM, and AWS CLI concepts. The symptom of an SSH failure usually identifies which layer is broken.
Reading SSH failures
| Error | Likely cause | First check |
|---|---|---|
| Timeout | No network path, blocked security group, or wrong address | Public IP, route, and port 22 rule |
| Connection refused | The instance is reachable, but `sshd` is not accepting connections | SSH service and operating-system firewall |
| Permission denied | Wrong key, user, or file permissions | AMI user, private key, and `chmod 400` |
A timeout usually happens before authentication. Confirm that the instance has a public IPv4 address, the subnet route table leads to an internet gateway, and the security group allows TCP port 22 from your current IP. A refused connection means packets arrived, but no process accepted them. A permission error means the connection reached SSH, but authentication failed.
chmod 400 ~/.ssh/my-key.pem
ssh -i ~/.ssh/my-key.pem [email protected]The login name depends on the AMI. Amazon Linux commonly uses ec2-user; Ubuntu commonly uses ubuntu.
Save connection details
Put host-specific settings in ~/.ssh/config:
Host web1
HostName 203.0.113.25
User ec2-user
IdentityFile ~/.ssh/my-key.pemYou can then run ssh web1. Protect the private key and keep port 22 restricted to trusted source addresses.
EC2 Instance Connect
EC2 Instance Connect temporarily places your public key in instance metadata. The key is available for about 60 seconds, so the matching private key must be used promptly.
aws ec2-instance-connect send-ssh-public-key \
--instance-id i-0123456789abcdef0 \
--instance-os-user ec2-user \
--ssh-public-key file://$HOME/.ssh/id_ed25519.pubSession Manager
Session Manager uses the Systems Manager Agent (SSM Agent) on the instance to make an outbound connection to AWS Systems Manager. Because the connection starts outbound, the instance needs no inbound port 22.
Prepare the instance and users
The instance needs a running SSM Agent, an instance profile with AmazonSSMManagedInstanceCore, and network access to Systems Manager endpoints. Public internet access or suitable VPC interface endpoints can provide that path.
The human user also needs permission to start a session. Scope ssm:StartSession to approved instance ARNs where possible, and allow ssm:TerminateSession and ssm:ResumeSession only for that user's sessions.
Exercise
An SSH attempt times out, but the instance is running. Which layer should you check first?
Show answer
Check reachability: the address, subnet route, internet gateway or VPN path, and the security group rule for TCP port 22.