Sub topic 2
What must be true on the network before SSH works
This chapter assumes you can launch an instance in a VPC subnet and run AWS CLI commands. It also assumes you know what a security group is. You do not need to know how routing works inside a VPC. The chapter covers that part.
An SSH connection fails before it starts if the network path is wrong. A key pair that is correct in every way will not help when packets never reach the instance. This chapter lists the conditions on the network path and shows how to check each one from the CLI. The next chapters cover key pairs and the SSH client.
The path a packet takes
Your SSH client sends a TCP packet to port 22 on the instance. For that packet to arrive, five things must be true, and they apply in this order:
- The instance has an address you can reach.
- The subnet's route table sends internet traffic to an internet gateway.
- The network ACL on the subnet allows the traffic.
- A security group on the instance allows inbound TCP 22 from your address.
- The SSH daemon (
sshd) is running and listening on the instance.
Each hop can drop the packet without telling you why. That is why SSH problems often look the same from the client side, as a hang. The sections below take the hops one at a time.
A reachable address
An instance needs an address that your client can route to. For a connection from the internet, that means a public IPv4 address or an IPv6 address.
- A public IPv4 address is assigned at launch if the subnet or the launch request enables it. It belongs to the instance only until the instance stops. A stop and start gives the instance a new one.
- An Elastic IP is a public IPv4 address you allocate to your account and attach to an instance. It stays the same across stops and starts.
- An instance that has only a private address is not reachable from the internet at all. You reach it from inside the VPC, for example through a bastion host (a public instance you connect to first), a VPN, or AWS Direct Connect. Session Manager, covered later, is another option.
To see what an instance has, run:
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query "Reservations[].Instances[].[PublicIpAddress,PrivateIpAddress,SubnetId,SecurityGroups[].GroupId]" \
--output jsonIf PublicIpAddress is null, the instance has no public IPv4 address, and an SSH connection to a public address cannot work.
A related gotcha is that SSH config entries and scripts often hold an old public IP. After a stop and start, the entry points to an address that now belongs to someone else, or to nothing. If a connection that worked yesterday now times out, check the current address first.
A route to the internet
A public IP is not enough. The subnet must also have a route that sends internet-bound traffic to an internet gateway. A subnet with such a route is called a public subnet.
Every subnet uses a route table. If you did not associate one explicitly, the subnet uses the VPC's main route table. Look up the explicit association first:
aws ec2 describe-route-tables \
--filters Name=association.subnet-id,Values=subnet-0123456789abcdef0 \
--query "RouteTables[].Routes[].[DestinationCidrBlock,GatewayId,State]" \
--output tableIf that returns nothing, the subnet uses the main route table. Query it by VPC:
aws ec2 describe-route-tables \
--filters Name=vpc-id,Values=vpc-0123456789abcdef0 Name=association.main,Values=true \
--query "RouteTables[].Routes[].[DestinationCidrBlock,GatewayId,State]" \
--output tableYou are looking for a row with destination 0.0.0.0/0 and a target that starts with igw-, in state active. If the default route points to a nat- gateway or is missing, the subnet is private. For IPv6 the equivalent row has destination ::/0.
The internet gateway itself must be attached to the VPC. That is true in any VPC where the route shows active. A route to a detached or deleted gateway shows as blackhole.
A security group that allows port 22
A security group is a set of allow rules attached to the instance's network interface. It has no deny rules. Anything not allowed is dropped.
Two properties matter here:
- Security groups are stateful. If inbound SSH is allowed, the reply packets leave automatically. You do not need an outbound rule for the response.
- Rules match on source address. A rule for port 22 from
203.0.113.25/32accepts only that one address.
This command adds an inbound rule for SSH from a single address:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 22 \
--cidr 203.0.113.25/32If you do not know your public address, ask a service that echoes it back. This avoids typing it by hand:
MY_IP=$(curl -s https://checkip.amazonaws.com)
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp --port 22 \
--cidr "${MY_IP}/32"Home and office addresses change, so a /32 rule that worked last week may no longer match. If a connection starts timing out from a location where it used to work, compare your current address with the rule's source.
To inspect the rules on a group:
aws ec2 describe-security-groups \
--group-ids sg-0123456789abcdef0 \
--query "SecurityGroups[].IpPermissions[]"An instance can have several security groups. The effective rule set is the union of all of them. A rule in any one group is enough to allow the traffic.
Network ACLs: the stateless layer
A network ACL (access control list) is a second filter, applied at the subnet boundary. The default network ACL of a VPC allows all traffic, so most people never touch it. A custom ACL changes that, and it behaves differently from a security group:
- It is stateless. Allowing inbound traffic does not allow the reply. You need a rule in each direction.
- It has numbered rules, evaluated from the lowest number up. The first match wins, and an explicit deny is possible.
For SSH from the internet, a custom ACL needs two entries. The inbound rule allows TCP 22 from your address. The outbound rule allows TCP on the ephemeral port range, usually 1024-65535, to your address. The ephemeral range is needed because your client picks a random high source port, and the instance's reply goes back to that port.
If you add the inbound rule and forget the outbound one, the connection hangs as if the security group were wrong. Check the ACL for the subnet with:
aws ec2 describe-network-acls \
--filters Name=association.subnet-id,Values=subnet-0123456789abcdef0 \
--query "NetworkAcls[].Entries[]"The daemon and the host firewall
Even with a clean network path, the instance must be listening. On standard Amazon Linux and Ubuntu AMIs, sshd starts at boot on port 22. Problems here are usually self-inflicted, for example a changed Port setting, a stopped service, or a host firewall such as iptables or firewalld that blocks the port. These are hard to fix over the network because you cannot log in. The usual recovery is to attach the volume to another instance, or to use an access method that does not depend on SSH.
Testing each layer from your machine
Before you start the SSH client, test the TCP connection alone. This separates network problems from key and user problems:
nc -vz 203.0.113.25 22The result tells you which layer to look at:
| Result of the TCP test | What it usually means | Where to look |
|---|---|---|
| Succeeds quickly | The network path is open | Key pair and login user (next chapter) |
| Hangs, then times out | A packet is dropped silently | Public IP, route table, network ACL, security group source |
| `Connection refused` immediately | The packet reached the host, but nothing listens on the port | `sshd` stopped, wrong port, host firewall |
The difference between a timeout and a refusal is the most useful clue in this chapter. A refusal means the instance sent back a reset, so the route, ACL, and security group are all working. A timeout means something dropped the packet before sshd could answer.
Private subnets and other paths
When the instance has no public address, the same conditions apply along a different path. The route must lead from your machine to the instance's private address, for example through a VPN or Direct Connect connection or a peered network. The security group must allow port 22 from the source address as seen by the instance. If you hop through a bastion host, the source is the bastion's private address or its security group, not your own address. Referencing the bastion's security group as the source is better than a CIDR, because it keeps working when the bastion's address changes.
Exercise
You launch an instance in a subnet whose route table has 0.0.0.0/0 pointing to an internet gateway. The instance has a public IP, and the security group allows TCP 22 from your /32 address. nc -vz <public-ip> 22 hangs and times out. The subnet uses a custom network ACL. Name two ACL rules to check, and explain why a refusal would point somewhere else.
Show answer
Check that the inbound rule allows TCP 22 from your address and that the outbound rule allows TCP on the ephemeral ports (1024-65535) back to your address, because ACLs are stateless and need a rule in each direction. Also check that no lower-numbered deny rule matches first. A refusal would mean the packet reached the instance and got a reset, so the route, ACL, and security group are fine and the problem is sshd or the host firewall.