Home/Tutorials/AWS/Error fix
Error fixAWS · 3 min read · Updated 5 Dec 2026

EC2 SSH Connection Timed Out or Permission Denied: Fix

Cannot SSH into your EC2 instance? Fix "connection timed out" (network) and "Permission denied (publickey)" (key or user) with this checklist.

QUICK ANSWERIf SSH to EC2 says "Connection timed out", the network is blocking you: check the security group allows port 22 from your IP, the instance has a public IP, the subnet routes 0.0.0.0/0 to an Internet Gateway, and the NACL allows the traffic. If it says "Permission denied (publickey)", you reached the server but the key or user name is wrong. Session Manager avoids SSH keys and open ports entirely.
EC2 SSH connection timed out: causes and fixes
EC2 SSH connection timed out: causes and fixes

Two different problems

MessageMeaningLook at
Connection timed outPackets never reach the instance (network)Security group, public IP, route table, NACL, firewall
Connection refusedReached the host but nothing listens on 22sshd stopped, wrong port, instance still booting
Permission denied (publickey)Reached sshd, login rejectedKey file, user name, file permissions

Fix "Connection timed out"

  1. Security group inbound rule: SSH, TCP 22, source = your public IP (My IP in the console). Your IP changes on home and mobile networks.
  2. The instance needs a public IPv4 address or Elastic IP, and you must use that address, not the private 10.x/172.x one.
  3. The subnet route table needs 0.0.0.0/0 → igw-.... A private subnet (route to a NAT gateway) cannot accept inbound SSH from the internet.
  4. Network ACL: allow inbound 22 and outbound ephemeral ports 1024-65535 (NACLs are stateless).
  5. Office or college networks often block outgoing port 22: try from a mobile hotspot to test.
  6. Use EC2 Instance Connect or Reachability Analyzer in the console to test the path.

Fix "Permission denied (publickey)"

  1. Use the right user for the AMI: ec2-user (Amazon Linux), ubuntu (Ubuntu), admin (Debian), centos or rocky for those images.
  2. Use the key pair chosen at launch: ssh -i mykey.pem ec2-user@<public-ip>.
  3. Fix key permissions on Linux or Mac: chmod 400 mykey.pem (SSH refuses keys readable by others).
  4. Lost the key? Use Session Manager or EC2 Instance Connect to get in, then add a new public key to ~/.ssh/authorized_keys.

Better: no SSH at all

AWS Systems Manager Session Manager gives a shell in the browser or CLI (aws ssm start-session --target i-...) without opening port 22 or managing keys. The instance needs the SSM agent (included in Amazon Linux and Ubuntu AMIs), an instance profile with the AmazonSSMManagedInstanceCore policy, and a route to the SSM endpoints. Every session can be logged, which auditors like.

Questions

Is it safe to allow SSH from 0.0.0.0/0?

No. Bots scan port 22 constantly. Allow only your IP, or use Session Manager and close port 22 completely.

Why did SSH work yesterday and time out today?

Usually your public IP changed, so the security group rule no longer matches, or the instance was stopped and started and got a new public IP.