Notes
Private ranges
Three ranges are reserved for private use: ten slash eight, one seven two dot sixteen slash twelve, and one nine two dot one six eight slash sixteen. Routers on the internet drop them, so every home, office and cloud can reuse the same ranges without conflict. Python's ipaddress module confirms ten dot zero dot one dot seventy seven is private. Your servers stay off the public internet unless you deliberately expose them.
10.0.0.0/8 16.7 million
172.16.0.0/12 1 million
192.168.0.0/16 65,536
>>> ip_address("10.0.1.77")
... .is_private
True
the same ranges are reused
in every home and company Source NAT
Source NAT works like this. Our private server sends a request from ten dot zero dot sixty four dot twelve, port forty thousand one hundred and twelve. The NAT gateway replaces the source address with its own public address and picks a new source port. The remote server only ever sees the public address. When the reply comes back to that public address and port, the gateway rewrites it back and forwards it to the private server.
- leaves server: 10.0.64.12:40112 → api.example:443
- after NAT: PUBLIC_IP:61001 → api.example:443
- reply comes back to PUBLIC_IP:61001 and is rewritten back
The NAT table
To rewrite replies correctly, the gateway keeps a table of every connection: which private address and port maps to which public port, and to which remote server. Entries expire when the connection goes idle. On Linux this is connection tracking, listed with conntrack. A packet arriving from the internet with no matching entry is dropped, which is why outsiders cannot start connections to private machines.
- private ip:port ↔ public ip:port ↔ remote ip:port
- entries expire after idle timeouts
- Linux: conntrack -L
- replies with no entry are dropped → outsiders cannot start a connection inward
Port exhaustion
Here is the classic failure. Ports are a sixteen bit number, and our Linux machine used a range of twenty eight thousand two hundred and thirty two ports for outgoing connections. A NAT gateway has a similar limit per destination. Hundreds of servers opening short connections to one external API can exhaust it, and new connections fail. The fixes: reuse connections, add public addresses, and keep connections open longer.
$ sysctl net.ipv4.
ip_local_port_range
32768 60999
= 28,232 ports
per (public IP, remote IP,
remote port)
many connections to one API
→ "cannot assign address"
fix: reuse connections, more
NAT IPs, fewer TIME-WAITs Destination NAT
Destination NAT works the other way. Traffic to a public address and port is rewritten to a private machine and port. Your home router's port forwarding is destination NAT, and so is Docker's minus p flag, which writes iptables rules that forward a host port into a container. In the cloud you usually do not forward ports by hand; a load balancer with a public address receives traffic and sends it to private servers.
- public 203.0.113.5:443 → private 10.0.1.20:8443
- home router "port forward"
- Docker -p 8080:80 does this with iptables rules
- cloud: a load balancer is the usual inbound path
- documentation IPs: 203.0.113 and 192.0.2 are examples
Cloud gateways
In AWS, a public subnet has a route to an internet gateway, and its machines can have public addresses that work both ways. A private subnet routes to a NAT gateway that lives in a public subnet, so its machines can reach out, for updates or APIs, but nothing can reach in. NAT gateways are per availability zone and charge per gigabyte, so heavy traffic to storage should use VPC endpoints instead. Part eight builds this.
- public subnet: route to an internet gateway, machines have public IPs
- private subnet: route to a NAT gateway in a public subnet: out only NAT gateway is per AZ and billed per GB processed
- S3/DynamoDB: VPC endpoints
NAT is not security
One warning: NAT is not a firewall. It blocks unsolicited inbound connections only as a side effect, and it lets every outbound connection through, so a compromised server can still call home. IP version six usually has no NAT at all, every machine has a global address. Real control comes from firewalls, security groups and egress rules, which episode fourteen covers.
- NAT blocks unsolicited inbound only as a side effect
- outbound is wide open: malware can call home
- IPv6 often has no NAT at all real control: firewalls, security groups, egress rules (episode 14)
Common mistakes
Mistakes. Treating NAT as a firewall. One NAT gateway shared by all availability zones, which fails together with its zone. Opening a new connection for every request and exhausting ports. Sending large storage traffic through a NAT gateway and paying for every gigabyte. And giving a partner a NAT address to allowlist without making sure it is a fixed elastic address.
- treating NAT as a firewall
- one NAT gateway for all AZs (single point of failure)
- new connection per request → port exhaustion
- huge S3 traffic through NAT (cost)
- allowlisting a partner by a NAT IP that can change
Say it like this
In the interview: the private subnet's route table sends internet traffic to a NAT gateway in a public subnet. The gateway rewrites the source to its public address and a port, tracks the connection, and maps replies back, so the server can reach out but nothing can start a connection in. I deploy one NAT per availability zone, use endpoints for heavy storage traffic, and watch for port exhaustion.
- "Its subnet routes 0.0.0.0/0 to a NAT gateway in a public
- subnet. The gateway rewrites
- the source to its public IP and port and tracks the connection
- to send replies back. Nothing
- can connect in. One NAT per AZ, and watch port exhaustion."