ssh to an EC2 instance hangs and ends in Connection timed out
ssh: connect to host 203.0.113.25 port 22: Connection timed outShort answer
A timeout means your packets never got an answer, so this is a network path problem, not a key problem. Check in order: the security group allows port 22 from your current public IP, the subnet route table sends 0.0.0.0/0 to an internet gateway, the network ACL allows port 22 and the return ports, and the instance has a public IPv4 address.
Fix it fast
1Find out which cause you have
Confirm your current public IP and test whether port 22 answers at all. A timeout (not 'refused') points to a filter on the path.
curl -s https://checkip.amazonaws.com nc -vz -w 5 203.0.113.25 222Match what you see, then jump to its fix
- 1
The timeout started after you changed networks, restarted a router or switched to a new security groupThe security group has no inbound rule for port 22 from your IPFix - 2
Security group rule is correct, new VPC or subnet, instance has a public address, still times outThe subnet has no route to an internet gatewayFix - 3
Security group and route are right; the subnet uses a custom network ACLThe network ACL blocks SSH or the return trafficFix - 4
The console shows an empty Public IPv4 address, or you are using a private 10.x or 172.x address from the internetThe instance has no public IPv4 addressFix
- 1
What does the error look like?
ssh: connect to host 203.0.113.25 port 22: Connection timed outNetwork error: Connection timed outError connecting to [instance], reason: -> Connection timed out: connect
Cause 1 of 4
The security group has no inbound rule for port 22 from your IP
1Check
List the inbound rules of the instance's security group. If the source is a single /32 address and your ISP or office changed your IP, the rule no longer matches you.
aws ec2 describe-security-groups --group-ids sg-0123456789abcdef0 --query 'SecurityGroups[0].IpPermissions'2Fix
Add an inbound TCP 22 rule for your current IP, or for the range your network uses if the address is not static. Avoid 0.0.0.0/0 for SSH.
aws ec2 authorize-security-group-ingress --group-id sg-0123456789abcdef0 \
--protocol tcp --port 22 --cidr 198.51.100.7/323Verify
Port 22 now answers.
nc -vz -w 5 203.0.113.25 22Confirmed by docs.aws.amazon.com
Cause 2 of 4
The subnet has no route to an internet gateway
1Check
AWS requires a route sending all traffic destined outside the VPC (0.0.0.0/0, and ::/0 for IPv6) to an internet gateway attached to the VPC. Read the route table of the instance's subnet.
aws ec2 describe-route-tables \
--filters Name=association.subnet-id,Values=subnet-0123456789abcdef0 \
--query 'RouteTables[].Routes'2Fix
Attach an internet gateway to the VPC if there is none, and add a default route to it in the subnet's route table.
aws ec2 create-route --route-table-id rtb-0123456789abcdef0 \
--destination-cidr-block 0.0.0.0/0 --gateway-id igw-0123456789abcdef03Verify
The route table now lists 0.0.0.0/0 with the igw target, and ssh connects.
ssh -i ~/.ssh/my-key.pem ec2-user@203.0.113.25Confirmed by docs.aws.amazon.com
Cause 3 of 4
The network ACL blocks SSH or the return traffic
1Check
A network ACL is stateless. It must allow inbound TCP 22 from your IP and outbound traffic to the ephemeral ports 1024-65535.
aws ec2 describe-network-acls \
--filters Name=association.subnet-id,Values=subnet-0123456789abcdef0 \
--query 'NetworkAcls[].Entries'2Fix
Edit or remove the deny entry, and add an inbound allow for port 22 and an outbound allow for 1024-65535 with rule numbers lower than any matching deny.
aws ec2 create-network-acl-entry --network-acl-id acl-0123456789abcdef0 \
--ingress --rule-number 100 --protocol tcp --port-range From=22,To=22 \
--cidr-block 198.51.100.7/32 --rule-action allow
aws ec2 create-network-acl-entry --network-acl-id acl-0123456789abcdef0 \
--egress --rule-number 100 --protocol tcp --port-range From=1024,To=65535 \
--cidr-block 198.51.100.7/32 --rule-action allow3Verify
Port 22 answers from your machine.
nc -vz -w 5 203.0.113.25 22Confirmed by docs.aws.amazon.com
Cause 4 of 4
The instance has no public IPv4 address
1Check
AWS says to check that the instance has a public IPv4 address.
aws ec2 describe-instances --instance-ids i-0123456789abcdef0 --query 'Reservations[0].Instances[0].[PublicIpAddress,PrivateIpAddress]'2Fix
Associate an Elastic IP address with the instance, or connect from inside the VPC or through Session Manager.
aws ec2 allocate-address
aws ec2 associate-address --instance-id i-0123456789abcdef0 --allocation-id eipalloc-0123456789abcdef03Verify
The instance now reports a public address and port 22 answers on it.
nc -vz -w 5 <the-new-public-ip> 22Confirmed by docs.aws.amazon.com
How do I stop it coming back?
- Open port 22 to a known range, or skip SSH and use Session Manager.
- Allocate an Elastic IP for servers that people connect to, so the address does not change after a stop and start.
- Keep network ACLs at their defaults unless you have a reason; they are stateless and easy to get wrong.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- RaheebA consumer app shipped to both stores from one Flutter codebase on a .NET and Azure back end.
- Loade DriverThe driver side of a delivery platform: live jobs, routing and earnings, past 5K Google Play downloads.
Related fixes
Security
SSH to an EC2 instance fails with Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com: Permission denied (publickey).
On EC2 this error almost always means the instance did not accept your key for that user.
3 causes, each with a fixServers
Apache has a certificate, but https:// does not open
No line with :443 in the output of ss -ltnp
HTTPS needs four things at once: mod_ssl loaded, Apache listening on 443, a virtual host on 443 with SSLEngine on, and a firewall that lets 443 in.
4 causes, each with a fixServers
AWS CLI says you must specify a region
You must specify a region. You can also configure your region by running "aws configure".
The AWS CLI sends every request to one region and has none to use.
4 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fixServers
git clone from CodeCommit over HTTPS returns 403
aws codecommit credential-helper $@ get: aws: command not found
A 403 from CodeCommit over HTTPS usually means Git sent the wrong credentials, not none.
5 causes, each with a fixServers
EC2 Instance Connect from the browser will not open an SSH session
Host key validation failed for EC2 Instance Connect
Browser-based EC2 Instance Connect needs five things at once: a fresh host key in the AWS database, a current agent package, port 22 open to the EC2 Instance Connect prefix list, a public address with a route (or an Instance Connect Endpoint), and a browser that can reach the proxy on port 443.
5 causes, each with a fix