Sub topic 2
What must be true on the network before SSH works
This chapter assumes you know the basics of EC2 instances, VPC subnets, and the AWS CLI. SSH is a TCP connection to port 22 on the instance. Before any key or login user matters, a packet from your machine has to reach that port and a reply has to come back. Four conditions on the network make that possible.
The four conditions
- The instance has an address you can reach. For access from the internet, that means a public IPv4 address, an Elastic IP, or a public IPv6 address.
- The subnet's route table sends internet traffic to an internet gateway. A route for
0.0.0.0/0(and::/0for IPv6) must point at the gateway. - The security group allows inbound TCP port 22 from your source address.
- The network ACL (NACL) of the subnet allows port 22 inbound and the ephemeral ports outbound for the replies.
If any one of these fails, the connection fails. Each failure looks different, which helps when you troubleshoot. The next chapters cover that in detail.
Security groups
A security group is a stateful firewall attached to the instance's network interface. Stateful means that if an inbound connection is allowed, the reply traffic is allowed automatically. Security groups only contain allow rules, and anything not allowed is dropped.
Do not open port 22 to 0.0.0.0/0. Scanners on the internet probe that port constantly. Allow only your own address, written as a /32 CIDR block (a single IPv4 address):
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 22 \
--cidr 203.0.113.25/32If your home or office address changes, the rule stops matching and you must update it. Using a corporate VPN with a fixed egress range avoids this.
Network ACLs
A network ACL is a stateless filter on the subnet. Stateless means it checks each packet separately, so it must allow both directions. The default NACL allows all traffic, so it rarely causes trouble. A custom NACL needs an inbound allow rule for port 22 and an outbound allow rule for ephemeral ports 1024-65535, which the client uses as its source port. A NACL can also contain deny rules, and rules are evaluated in number order.
Checking the route and the address
To check the instance's public address and subnet, run:
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query "Reservations[].Instances[].[PublicIpAddress,SubnetId]" \
--output textIf the public address shows None, the instance has no public IPv4 address, and you cannot reach it from the internet. Then look up the subnet's route table:
aws ec2 describe-route-tables \
--filters Name=association.subnet-id,Values=subnet-0123456789abcdef0 \
--query "RouteTables[].Routes[].[DestinationCidrBlock,GatewayId]" \
--output textYou should see 0.0.0.0/0 pointing at a gateway ID that starts with igw-. If it points at nat- or has no default route, the subnet is private.
Note that a subnet with no explicit association uses the VPC's main route table, so an empty result does not mean the subnet has no routes.
Common mistakes
- Editing the wrong security group. An instance can have several, and the rules from all of them combine.
- Forgetting that a public IPv4 address that is not an Elastic IP changes when you stop and start the instance.
- Testing from a network that blocks outbound port 22, such as some guest Wi-Fi networks.
Exercise
Your instance has a public IP and the security group allows port 22 from your address, but SSH still times out. The subnet uses a custom network ACL. Name two things to check in the network setup.
Show answer
Check that the subnet's route table has a 0.0.0.0/0 route to an internet gateway (igw-...). Check that the custom network ACL allows inbound TCP 22 and outbound ephemeral ports 1024-65535, because NACLs are stateless.