I spent time recently getting my head around Linux networking fundamentals and how they map to AWS. Not in AWS itself, but in my homelab with k3s, trying to understand the layers underneath rather than memorizing console clicks.
Right now I have a solid mental model loaded up. But I don’t work with this daily and the details will get hazy. So this is a reference. Future me, here’s where you left off.
The Routing Table
Every device that does networking has one. It answers one question: where does this packet go next?
Each entry has a destination CIDR and a next hop. The kernel checks the destination IP against the table, finds the most specific match, and sends it that direction.
On a Linux box, ip route shows yours:
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 src 192.168.1.50
10.0.0.0/8 via 10.0.0.1 dev tun0
Reading that: anything for 192.168.1.0/24 is local, deliver directly. Anything for 10.0.0.0/8 goes through the VPN tunnel. Everything else hits the default gateway.
Every packet goes through this lookup. Keep that in mind because iptables is organized entirely around this moment.
Reading the Syntax
Route table syntax looks cryptic but follows a pattern. Every rule is basically a sentence:
“Traffic going to [destination], send it [directly / via gateway] out [interface], using [source IP] as the return address.”
The template:
[destination] via [gateway] dev [interface] src [source IP]
- destination is always present. Where is the packet going? A CIDR like
10.42.0.0/16, ordefault(shorthand for0.0.0.0/0, the catch-all). - via is optional. If present, hand the packet to this gateway to forward. If absent, deliver directly because the destination is on the local network.
- dev is which network interface to use. Physical (
eth0) or virtual (cni0,tailscale0). - src is optional. Which of this machine’s IPs to use as the return address.
The key distinction: via means hand it to a middleman. No via means deliver it yourself.
A more realistic example from a k3s node with Tailscale:
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 src 192.168.1.100
10.42.0.0/16 dev cni0 src 10.42.0.1
100.64.0.0/10 dev tailscale0
defaultis the catch-all, goes to home router192.168.1.0/24is the local home network, deliver directly10.42.0.0/16is the k3s pod CIDR, routed through the CNI bridge100.64.0.0/10is Tailscale’s address range, routed through the WireGuard tunnel
You don’t need to write these from scratch. You need to read them. When you see route table output on a node or in AWS, you want to know what it’s telling you.
iptables
The kernel’s packet inspection and manipulation tool. It has tables (what kind of operation) and chains (when in the packet’s lifecycle the rules fire).
Tables: filter is your firewall (accept, drop, reject). nat rewrites source or destination IPs. mangle is advanced header stuff, ignore it for now.
The chains are the important part. They’re hooks that fire at specific moments relative to the routing decision. That relationship is the mental model.
The Chains
There are two paths through the system. Which one a packet takes depends on whether it’s arriving from the network or being generated locally.
Inbound path (a packet arrives from the network):
PREROUTING -> Routing Decision -> INPUT (if for this machine) or FORWARD (if passing through)
- PREROUTING fires before routing. DNAT happens here so you can rewrite the destination before the kernel decides where to send it.
- INPUT is your inbound firewall. The packet is for this machine.
- FORWARD is for transit traffic. The packet is just passing through.
Outbound path (this machine generates a packet, like curl or nginx):
OUTPUT -> Routing Decision -> POSTROUTING
Both paths converge at POSTROUTING, which is the last stop before the packet leaves the interface. SNAT/MASQUERADE happens here because you need to know which interface the packet is leaving through before rewriting the source IP.
NAT and MASQUERADE
NAT rewrites IP addresses. Two flavors:
SNAT / MASQUERADE rewrites the source IP. Your home router does this. It rewrites your local IP to its public IP, tracks the mapping, and rewrites responses back.
DNAT rewrites the destination IP. Load balancers do this.
MASQUERADE is dynamic SNAT. Instead of hardcoding the rewrite IP, it grabs whatever’s currently on the outgoing interface. Useful when IPs can change.
The rule:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
For any packet leaving eth0, rewrite the source IP to this machine’s IP and track the connection.
How This Maps to AWS
AWS wraps these primitives in managed services.
| Linux Primitive | AWS Equivalent |
|---|---|
ip route | VPC Route Tables |
iptables -t filter | Security Groups + NACLs |
iptables -t nat MASQUERADE | NAT Gateway |
iptables -t nat DNAT | ALB / NLB |
conntrack (connection tracking) | Security Groups (stateful) |
| Kernel packet forwarding | AWS network fabric |
Route tables: every VPC subnet has one. Same concept, same “most specific match wins” behavior. Targets are internet gateways, NAT gateways, Transit Gateways, etc.
Security groups are stateful firewalls with connection tracking built in. NACLs are stateless, so you explicitly allow both directions.
NAT gateways are managed MASQUERADE. Before they existed, people ran EC2 instances with that one-line iptables rule. AWS productized it.
Packet Flow Through a NAT Gateway
EC2 instance at 10.0.1.50 in a private subnet wants to reach 8.8.8.8:
- Sends packet. Source:
10.0.1.50, destination:8.8.8.8. - Route table says
0.0.0.0/0 -> NAT Gateway. - NAT gateway rewrites source to its Elastic IP (
54.1.2.3), records the mapping. - Packet goes through the internet gateway to the internet.
- Response comes back to
54.1.2.3. - NAT gateway looks up the tracking table, rewrites destination back to
10.0.1.50, delivers it.
Same thing your home router does.
Public vs Private NAT Gateways
Public: Elastic IP, public subnet, routes through internet gateway. The standard setup for letting private instances reach the internet.
Private: no Elastic IP, no internet. For routing between private networks. The main use case is overlapping CIDRs. Two VPCs both using 10.0.1.0/24? A private NAT gateway translates source addresses to avoid collisions.
Same mechanics. Public translates private to public. Private translates private to a different private range.
Zonal vs Regional
Not networking, availability.
Zonal (default): one AZ. AZ goes down, NAT goes with it. The HA pattern is to deploy one per AZ.
Regional: AWS manages multi-AZ redundancy. Simpler, costs more.
Poke at This in the Homelab
If you’re running k3s, all of this is live on your nodes right now. SSH in:
sudo iptables -t nat -L -n # NAT rules kube-proxy set up
ip route # pod network routes
sudo conntrack -L # connection tracking in real time
MASQUERADE rules, DNAT rules for services, routes for the pod CIDR. Same primitives, managed by kube-proxy.
It’s the same stuff all the way down. AWS, Kubernetes, your home router. Route tables decide where packets go. iptables decides if they’re allowed and rewrites headers. NAT tracks connections and translates addresses. Everything else is management on top.
You’ll probably need to re-read this in a month.
