Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

ssh to an EC2 instance hangs and ends in Connection timed out

Errorssh: connect to host 203.0.113.25 port 22: Connection timed out

Short 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.

  • Applies to Amazon EC2 Linux instances in a VPC, AWS CLI 2.x, OpenSSH 9.x client
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 22
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2Security 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. 3Security group and route are right; the subnet uses a custom network ACLThe network ACL blocks SSH or the return trafficFix
    4. 4The 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

What does the error look like?

  • ssh: connect to host 203.0.113.25 port 22: Connection timed out
  • Network error: Connection timed out
  • Error 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/32

3Verify

Port 22 now answers.

nc -vz -w 5 203.0.113.25 22

Confirmed by docs.aws.amazon.com

Did this fix it?

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-0123456789abcdef0

3Verify

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.25

Confirmed by docs.aws.amazon.com

Did this fix it?

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 allow

3Verify

Port 22 answers from your machine.

nc -vz -w 5 203.0.113.25 22

Confirmed by docs.aws.amazon.com

Did this fix it?

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-0123456789abcdef0

3Verify

The instance now reports a public address and port 22 answers on it.

nc -vz -w 5 <the-new-public-ip> 22

Confirmed by docs.aws.amazon.com

Did this fix it?

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:

Related fixes

Servers

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 fix

Servers

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 fix

Servers

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.