DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

NetworkingLesson 7 of 12
Your progress · 0%
On this page
  1. Default gateway
  2. Route tables
  3. NAT and port forwarding
  4. traceroute and mtr
↖ Course roadmap
Lesson 7 · Routing and NAT

Routing and NAT: finding the next hop

Read route tables, explain source and destination NAT, and trace a path without assuming every router answers.

22 min read · beginner · hands-on

By the end of this lesson you'll be able to

0 of 3 complete
Lab status

Verified 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.

⌘
Mini mission

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.

Input: DockerOutput: A route lookup and the NAT table in a disposable container
Reveal 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 done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 3

A packet's destination is outside every connected subnet. Which route is normally used?

Go deeper

Official reading for this lesson

  • Docsip-route(8)Linux man-pages ↗
  • GuidePerforming NAT with nftablesNetfilter Wiki ↗
  • Docstraceroute(8)Linux man-pages ↗

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.

← PreviousTLS and HTTPS: certificates without the mysteryNext lesson →Firewalls: allow only the traffic you need