beginner

Routing Basics: Gateways, Routing Tables and NAT

How a packet gets from your laptop to a server on another network: the default gateway, routing tables and longest-prefix match, TTL, and how NAT lets a whole network share one public address.

30 min

Switches connect devices. Routers connect networks.

A switch moves frames inside one network. To get from one network to another (your office to a web server, or the staff subnet to the server subnet) you need a router: a device with an interface in each network that forwards packets based on their destination IP address.

Every host makes a simple decision

When a host has something to send, it asks one question: is the destination in my own subnet? It answers by applying its subnet mask to its own address and to the destination.

  • Yes (same subnet): deliver directly. The host finds the destination’s MAC address with ARP (next lesson) and sends the frame straight to it.
  • No: send the packet to the default gateway, the router interface on the local network that leads elsewhere. The host puts the gateway’s MAC address in the frame but keeps the final destination’s IP address in the packet.

That last point is important: the IP address stays the same end to end, while the MAC address is replaced at every hop.

graph LR PC[PC 192.168.1.20] --- SW[Switch] SW --- R1[Router LAN 192.168.1.1 / WAN 203.0.113.2] R1 --- ISP[ISP router] ISP --- WEB[Web server 198.51.100.10]

The routing table

A router decides where to send each packet using its routing table. Each entry has:

  • a destination prefix (for example 198.51.100.0/24),
  • a next hop address or an exit interface,
  • often a metric or administrative distance used to choose between routes learned in different ways.

Routes come from three sources:

  1. Connected: networks directly attached to one of the router’s interfaces. Added automatically when the interface is up and has an address.
  2. Static: added by an administrator (198.51.100.0/24 via 203.0.113.1). Simple and predictable, but they do not adapt to failures.
  3. Dynamic: learned from routing protocols such as OSPF and BGP, covered in later courses.

Longest prefix match

If several routes match a destination, the router picks the one with the longest prefix (the most specific). For example:

Destination prefix Next hop
10.0.0.0/8 A
10.1.0.0/16 B
0.0.0.0/0 C

A packet to 10.1.5.9 matches all three, but /16 is the longest, so it goes to B. A packet to 10.200.1.1 matches /8 and /0, so it goes to A. Anything else goes to C.

The default route

0.0.0.0/0 matches every address, so it is chosen only when nothing more specific exists. It is the default route (“gateway of last resort”) and typically points toward the ISP. Hosts have the same idea in simpler form: the default gateway.

If no route matches and there is no default route, the router discards the packet and normally sends back an ICMP “destination unreachable”.

TTL: preventing endless loops

Misconfigured routes can make packets circle forever. The IPv4 header therefore contains a TTL (time to live) that each router decreases by one. When it reaches zero the packet is dropped and the router sends the sender an ICMP “time exceeded” message. (IPv6 calls the same field the Hop Limit.) You will use this in traceroute, which deliberately sends packets with TTL 1, 2, 3 and so on to reveal each router along the path.

NAT: sharing a public address

Home and office networks use private addresses that cannot be routed on the internet. To let those devices reach the internet, the router performs Network Address Translation (NAT). The most common form, NAT overload (also called PAT, port address translation), works like this:

  1. A PC at 192.168.1.20 opens a connection from source port 50234 to a web server’s port 443.
  2. The router replaces the source address with its own public address and picks a unique source port, for example 203.0.113.2:61001, and records the mapping in its translation table.
  3. The server replies to 203.0.113.2:61001.
  4. The router looks up the mapping, rewrites the destination back to 192.168.1.20:50234, and forwards the reply.

Thousands of internal devices can share one public address, because the (address, port) pair keeps their connections distinct.

Many mobile and broadband providers go one step further with carrier-grade NAT (CGNAT): your router’s “public” address is itself a shared address (often in 100.64.0.0/10), so you cannot receive inbound connections at all without help from the provider.

Look at it yourself

# Linux: your routing table (look for "default via ...")
ip route

# Windows
route print
Get-NetRoute -DestinationPrefix 0.0.0.0/0

# The path your packets take (Windows uses tracert)
traceroute 8.8.8.8

Find your default gateway, then compare your local address with your public address (for example, curl https://api.ipify.org). If they differ, NAT is happening somewhere between you and the internet.

Host 10.0.5.30/24 sends a packet to 10.0.6.40. Which MAC address is in the destination field of the frame leaving the host, and which IP address is in the packet's destination field?

The destination IP address is 10.0.6.40, because the final destination never changes. The destination MAC address is that of the default gateway, because 10.0.6.40 is not in 10.0.5.0/24 and so must be sent via the router.

Can You Explain It?

Close the lesson and answer these without looking back. The goal isn't the wording — it's whether you've formed a usable mental model.

  • Explain how a host decides whether to send a packet directly or to the default gateway.
  • A router has routes for 172.16.0.0/16, 172.16.4.0/24 and 0.0.0.0/0. Which does a packet for 172.16.4.7 use, and why?
  • Describe in your own words what NAT overload does and why a device behind it cannot normally be reached from the internet.

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for Routing Basics: Gateways, Routing Tables and NAT. What would you like to know?
📝

Knowledge Check

1 of 4

A router has these routes: 10.0.0.0/8 via A, 10.1.0.0/16 via B, 0.0.0.0/0 via C. A packet arrives for 10.1.5.9. Which next hop is used?

Report a problem

About: /lessons/routing-basics-gateways-and-nat