Routing and NAT: finding the next hop
Read route tables, explain source and destination NAT, and trace a path without assuming every router answers.
By the end of this lesson you'll be able to
0 of 3 completeVerified lab: route lookup and a disposable iptables MASQUERADE rule ran inside alpine:3.22. The output
below comes from that run. The host's Docker NAT is outside the container and is not shown by its ruleset.
Cloud lab: none. No AWS resources were created.
Before cloud networks, people connected machines with static routes and a gateway someone configured by hand. A missing route could strand a server even when its interface was up. Routers solve this one hop at a time: each reads its route table, forwards toward the next hop, and leaves the destination IP alone unless NAT is configured. In this lesson, you will read a route decision, follow address translation, and interpret a traceroute.
Default gateway
A gateway is a router reachable on the local link. Your machine sends off-subnet traffic to it. It does not send the packet directly to the remote server's MAC address.
ip route get asks the kernel to perform a lookup for one destination:
$ ip route get 1.1.1.1 1.1.1.1 via 172.17.0.1 dev eth0 src 172.17.0.2 uid 0 cache
This real lookup selected gateway 172.17.0.1, interface eth0, and source address 172.17.0.2. A route table
does not guarantee the gateway is healthy; it only describes the next step.
Route tables
The kernel compares the destination with route prefixes. A more specific prefix wins over a less specific one. A connected route usually handles the local subnet; the default route handles the rest. The selected route can name a next-hop gateway, an output interface, or both.
ip route ip route get 1.1.1.1
Use the first command to inspect routes and the second to see the actual decision for one address. A common mistake is to read the default route and assume it applies to every destination. A more specific route can override it.
NAT and port forwarding
Network address translation (NAT) rewrites packet addresses. Source NAT changes the sender on outbound traffic. Destination NAT changes the recipient, often to forward an inbound public port to a private server. Stateful NAT records a mapping for the flow, so the reply can be translated in reverse. NAT is not a substitute for a firewall: the firewall still decides which traffic is allowed.
This temporary rule shows the shape of outbound source NAT:
$ iptables -t nat -S POSTROUTING -P POSTROUTING ACCEPT -A POSTROUTING -o eth0 -j MASQUERADE
The output is from an isolated container. Docker performs its own host-side translation separately. Do not add a port-forwarding rule until you have decided which sources may reach the service and confirmed the service is safe to expose.
traceroute and mtr
traceroute sends probes with a small time to live (TTL). Each router that decrements TTL to zero may return an
ICMP time-exceeded message. mtr repeats probes over time and summarizes loss and delay at each hop.
traceroute -n example.com mtr -n example.com
Use these tools to find where replies stop, not to prove a router is dropping application traffic. A * means a
probe did not get a response in time. The router may still forward ordinary packets and simply suppress probes.
Read a route and a NAT rule
Inspect the route to a public address, then add one temporary MASQUERADE rule inside an Alpine network namespace. The container exits and removes the rule.
A route lookup and the NAT table in a disposable containerReveal one safe solution
docker run --rm --cap-add NET_ADMIN alpine:3.22 sh -ec ' apk add --no-cache iproute2 iptables ip route ip route get 1.1.1.1 iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE iptables -t nat -S POSTROUTING '
The output should show a default route and the rule you added. That container rule does not expose a port on your host.
Try it yourself
0 of 4 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
A packet's destination is outside every connected subnet. Which route is normally used?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.