Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Telling routers where to send traffic (static routes)

After this lesson: I can read a routing table and pick the route a packet takes.

55 min lesson5 commandsStatic routing lab: build routes on a small network in your browser

The sorting belt that never plans

A parcel sorter reads a postcode and drops the parcel on one belt. It never plans the whole journey.

A router does the same with a packet. It reads the destination address, picks one line in its table and hands the packet on.

Every router on the way does the same job again.

By the end of this lesson

Note

I can read a routing table and pick the route a packet takes.

Key words

WordWhat it meansExample
Routing tablea router's list of networks and where to send packets for each
Next hopthe neighbouring router that gets the packet nextvia 10.0.1.1
Connected routea network the router is plugged into directly
Static routea route that a person typed in by hand
Default routethe line used when no other line matches0.0.0.0/0
Longest prefixthe matching line with the most network bits

The cells students fill in are left blank in this preview.

A host sends direct or to its gateway

A host asks one question first. Is the destination on my own network?

If yes, it sends the packet straight there. If no, it sends it to its default gateway, a router on its own network.

The host never plans the rest of the journey.

Read a host's routes

Command
sudo ip netns exec h1 ip route

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask which way h1 sends a packet

Command
sudo ip netns exec h1 ip route get 10.0.3.2

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Reading the answers

The first Run prints two lines. One is the network h1 is on. The other starts with default via 10.0.1.1: everything else goes to r1.

The second Run starts with 10.0.3.2 via 10.0.1.1 dev to-r1. The word via names the next hop.

A router follows its table

A router has several interfaces and a routing table. Each line says: for this network, use this way.

A router starts with only its connected routes. It learns every other network because a person adds a static route, or because a protocol shares routes.

Without a line for a network, the router cannot forward packets to it.

Worked example: a router's first table

NetworkNext hopKind
10.0.1.0/24direct, on to-h1Connected
10.0.12.0/24direct, on to-r2Connected
10.0.3.0/24noneNo line, so no way

The cells students fill in are left blank in this preview.

Read r1's table in its console

Command
sudo vtysh -N r1 -c "show ip route"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask r1 about an unknown network

Command
sudo ip netns exec r1 ip route get 10.0.3.2

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Send a real packet from h1

Command
sudo ip netns exec h1 ping -c 2 -W 1 10.0.3.2

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

No line means no way

The console lists four lines for r1. Two start with C for connected and two with L for the router's own addresses.

The ip route get Run prints RTNETLINK answers: Network is unreachable. The table has no line for 10.0.3.2, so r1 has nowhere to send it.

The ping shows the same gap from the host's side. Each reply reads From 10.0.1.1 and ends Destination Net Unreachable.

The longest prefix wins

Several lines can match one address. The router picks the line with the longest prefix, the one that names the network most exactly.

The default route, 0.0.0.0/0, matches everything with a prefix of zero bits. So it loses to any other match and is used last.

A common mistake

Warning

Many people think the first matching line wins. A router does not read from the top. It looks at every line and picks the longest match. The order in the table does not change the answer.

Worked example: which line wins

DestinationLines that matchWinner
10.0.3.2000.0.0.0/0, 10.0.0.0/8, 10.0.3.0/24, 10.0.3.128/2510.0.3.128/25
10.0.3.50.0.0.0/0, 10.0.0.0/8, 10.0.3.0/2410.0.3.0/24
10.9.9.90.0.0.0/0, 10.0.0.0/810.0.0.0/8
8.8.8.80.0.0.0/00.0.0.0/0

The cells students fill in are left blank in this preview.

Going further: say why a line won

Copy your pick_route into a new function, explain_route. It returns the whole winning line, not only the next hop.

Use it on the TABLE from the tests for 10.0.3.129, 10.0.3.127, 10.200.0.1 and 1.1.1.1. For each one, write a sentence naming every line that matched and why the winner beat the others.

About this lab and its source: Static routing lab: build routes on a small network in your browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.