You launched an EC2 instance, ran ssh, and after a long wait you got this:
ssh: connect to host 3.110.12.34 port 22: Connection timed out
The good news: a timeout always means a network path problem, so you can find it by checking each layer between your laptop and the instance, in order.
What the error means
Different SSH errors point to different layers. Identify yours first:
| Error | What it means | Look at |
|---|---|---|
Connection timed out | Packets are dropped silently before reaching SSH | Security group, public IP, route table, NACL, your network |
Connection refused | The instance is reachable but nothing listens on port 22 | sshd service, OS firewall, wrong port |
Permission denied (publickey) | Network is fine; authentication failed | Key pair, username, key file permissions |
Run SSH in verbose mode to see exactly where it stops:
ssh -v -i mykey.pem ec2-user@3.110.12.34
How to fix it, step by step
1. Confirm the instance is running and passing status checks
In the EC2 console, the instance state must be Running and both status checks 2/2 passed. From the CLI:
aws ec2 describe-instances --instance-ids i-0123456789abcdef0 \
--query "Reservations[].Instances[].{State:State.Name,PublicIP:PublicIpAddress,Subnet:SubnetId,SGs:SecurityGroups[].GroupId}"
2. Allow port 22 in the security group, from your current IP
The security group must have an inbound rule: Type SSH, Protocol TCP, Port 22, Source = your public IP/32. Your IP changes on many home and mobile connections, so re-check it at a site such as checkip.amazonaws.com and update the rule.
aws ec2 authorize-security-group-ingress --group-id sg-0abc1234 \
--protocol tcp --port 22 --cidr 203.0.113.25/32
Avoid 0.0.0.0/0 on port 22. It works, but it exposes SSH to the whole internet.
3. Make sure the instance has a public IP
If Public IPv4 address is empty, you can't reach the instance from the internet. Either launch it in a subnet with "auto-assign public IP" enabled, or attach an Elastic IP. Note that a stopped and started instance gets a new public IP unless it uses an Elastic IP.
4. Check the subnet's route table
A "public" subnet is simply one whose route table has 0.0.0.0/0 → igw-xxxx (an internet gateway). Without that route, replies never leave the VPC and SSH times out.
aws ec2 describe-route-tables --filters Name=association.subnet-id,Values=subnet-0abc1234 \
--query "RouteTables[].Routes[]"
5. Check the network ACL (it's stateless)
Unlike security groups, network ACLs don't remember connections. The subnet's NACL must allow inbound TCP 22 from your IP and outbound TCP 1024-65535 (the ephemeral ports SSH replies use). The default NACL allows everything; custom NACLs often miss the outbound rule.
6. Test from your side of the network
Corporate and some public Wi-Fi networks block outbound port 22. Test the port directly:
# Linux / macOS
nc -vz 3.110.12.34 22
# Windows PowerShell
Test-NetConnection 3.110.12.34 -Port 22
If it fails only on one network, try a mobile hotspot or use Session Manager (step 8).
7. If you get "Permission denied (publickey)": fix the user and key
- Use the right username for the AMI:
ec2-user(Amazon Linux, RHEL),ubuntu(Ubuntu),admin(Debian),centos(CentOS). - Use the private key from the key pair chosen at launch.
- Restrict the key file, or SSH refuses to use it:
chmod 400 mykey.pem.
8. Still stuck? Get in without SSH
If sshd crashed or the OS firewall blocks port 22, connect another way and repair it:
- Session Manager (Systems Manager): needs the SSM agent and an instance role with
AmazonSSMManagedInstanceCore. - EC2 Instance Connect or EC2 Instance Connect Endpoint for instances in private subnets.
- EC2 Serial Console for boot or network configuration problems.
aws ssm start-session --target i-0123456789abcdef0
sudo systemctl status sshd
How to prevent it
- Prefer Session Manager over open SSH ports. No inbound rule, full audit trail.
- Define security groups, routes and NACLs in code with Terraform so every environment is identical.
- Use an Elastic IP or a DNS name for instances you connect to regularly.
- Keep a "bastion" or Instance Connect Endpoint for private subnets instead of giving every instance a public IP.
Networking issues like this are covered hands-on in our AWS training, where you build public and private subnets from scratch.
Frequently asked questions
Why does SSH to my EC2 instance time out?
Most often the security group does not allow port 22 from your current public IP, the instance has no public IP, or the subnet has no route to an internet gateway. A network ACL that blocks return traffic on ports 1024-65535 also causes timeouts.
What is the default username for EC2 instances?
ec2-user for Amazon Linux and RHEL, ubuntu for Ubuntu, admin for Debian, and centos for CentOS AMIs. Using the wrong username gives 'Permission denied (publickey)'.
Can I connect to EC2 without opening port 22?
Yes. AWS Systems Manager Session Manager and EC2 Instance Connect Endpoint let you open a shell without exposing port 22 to the internet.
Why did SSH work yesterday but not today?
Your home or office public IP probably changed, so the security group rule limited to 'My IP' no longer matches. Update the rule with your new IP.
Learn AWS & AI on AWS hands-on with expert trainers
Fixing problems like this one is part of our AWS & AI on AWS training: live online classes for individuals and on-site corporate training for teams, with real projects and labs.
Training available in Hyderabad, Bangalore, Pune, Mumbai, Delhi, Chennai and online across India.