# ssh: connect to host ec2 port 22: Connection timed out

AWS error fixes · Updated 17 November 2026

**Short answer:** A timeout means your SSH packets never got an answer, so the problem is the network path, not your key. The usual cause is a security group that does not allow port 22 from your current public IP; after that, check the instance has a public IP and an internet gateway route, the network ACL, and whether the instance is actually running.

## Timed out vs refused

```
ssh: connect to host 203.0.113.25 port 22: Connection timed out
```

A **timeout** means packets were silently dropped somewhere: a security group, a network ACL, a missing route, a firewall on your side, or a dead host. **Connection refused** is different: the packets reached the instance but nothing was listening on port 22, so sshd is stopped or on another port. Fix the right one.

## Causes, most common first

1. **Security group** lacks an inbound rule for TCP 22 from your address, or allows an old address. Home and mobile IPs change often.
2. **No public IPv4 address**, or you are using the old one: an auto-assigned public IP changes on every stop/start.
3. **Subnet has no route to an internet gateway** (a private subnet), so replies never leave the VPC.
4. **Network ACL** denies port 22 inbound, or denies the ephemeral port range (1024–65535) outbound. NACLs are stateless, so both directions need rules.
5. **Your network blocks outbound 22**: corporate, hotel and some mobile networks do.
6. **Instance not healthy**: stopped, still booting, or failing its status checks.
7. **OS firewall** (firewalld, ufw, iptables) dropping SSH.

## Check the path step by step

Find your current public IP and test the port from your machine:

```
curl -s https://checkip.amazonaws.com
nc -vz -w 5 203.0.113.25 22
```

Get the instance's state, public IP, subnet and security groups:

```
aws ec2 describe-instances --instance-ids i-0123456789abcdef0 \
  --query 'Reservations[].Instances[].{state:State.Name,ip:PublicIpAddress,subnet:SubnetId,sgs:SecurityGroups[].GroupId}'
```

Read the inbound rules of each group:

```
aws ec2 describe-security-groups --group-ids sg-0123456789abcdef0 \
  --query 'SecurityGroups[].IpPermissions[?ToPort==`22`]'
```

Check the subnet's route table has `0.0.0.0/0` pointing at an `igw-`:

```
aws ec2 describe-route-tables \
  --filters Name=association.subnet-id,Values=subnet-0bb1c79de3EXAMPLE \
  --query 'RouteTables[].Routes[]'
```

An empty result means the subnet has no explicit association and uses the VPC's main route table. VPC Reachability Analyzer can also trace the path from an internet gateway to the instance's network interface and name the blocking component.

## Fix it

Allow SSH from your current address only, never `0.0.0.0/0`:

```
aws ec2 authorize-security-group-ingress --group-id sg-0123456789abcdef0 \
  --protocol tcp --port 22 --cidr 198.51.100.7/32
```

Then remove the stale rule for your old address with `revoke-security-group-ingress`. If the instance is in a private subnet, do not move it to a public one just for SSH: use a bastion, an EC2 Instance Connect Endpoint, or Session Manager. If the public IP keeps changing, attach an Elastic IP or connect by DNS name from your inventory.

## Skip port 22 entirely

Session Manager opens a shell over an outbound HTTPS connection from the SSM agent, so it needs no inbound rule, no public IP and no key pair:

```
aws ssm start-session --target i-0123456789abcdef0
```

The instance needs the SSM agent, an instance profile with the `AmazonSSMManagedInstanceCore` managed policy, and a route to the SSM endpoints (NAT or VPC endpoints). For many teams this removes SSH timeouts as a category of problem.

## Questions

### Why did SSH to my EC2 instance stop working after a restart?

An auto-assigned public IPv4 address changes when the instance stops and starts. Use the new address from describe-instances, or attach an Elastic IP so it stays fixed.

### Is it safe to open port 22 to 0.0.0.0/0 to test?

Avoid it: internet-wide scanners find open SSH ports within minutes. Allow your own /32, test, and remove the rule, or use Session Manager instead.

### How do I connect to EC2 with Session Manager from the CLI?

Install the Session Manager plugin and run aws ssm start-session --target with the instance ID. The instance must be registered as a managed node.

## When this happens away from your desk

When SSH times out from a café or a phone hotspot, the cause is often your own changing IP. CloudConsole, a phone app for AWS, is built to let you check instance state and alarms from your phone, so you can rule out the instance before blaming the network.

[About the CloudConsole app](https://cloudconsole.dev/aws-console-app) · [Download on the App Store](https://apps.apple.com/app/cloudconsole-for-aws/id6835419710)

## Sources

- [Troubleshoot issues connecting to your EC2 Linux instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html)
- [VPC Reachability Analyzer](https://docs.aws.amazon.com/vpc/latest/reachability/)

## Related

- [Permission denied (publickey) when connecting to EC2 over SSH](https://cloudconsole.dev/errors/ec2-permission-denied-publickey)
- [How to connect to EC2 with Session Manager](https://cloudconsole.dev/how-to/connect-to-ec2-with-session-manager)
- [aws ec2 authorize-security-group-ingress](https://cloudconsole.dev/cli/ec2-authorize-security-group-ingress)
- [aws ec2 describe-security-groups](https://cloudconsole.dev/cli/ec2-describe-security-groups)
- [EC2 Instance Connect: Failed to connect to your instance](https://cloudconsole.dev/errors/ec2-instance-connect-failed)
- [SSM Session Manager: TargetNotConnected](https://cloudconsole.dev/errors/ssm-start-session-targetnotconnected)
- [How to add my IP to an AWS security group](https://cloudconsole.dev/how-to/add-my-ip-to-security-group)

---

Source: https://cloudconsole.dev/errors/ec2-ssh-connection-timed-out

CloudConsole is an independent iPhone and Android app for Amazon Web Services by Cloudconcur Ltd. It is not affiliated with Amazon Web Services.
