Skip to content

Linux Foundations / Follow traffic and find the fault ยท Step 25 of 46

Lesson: routeSwitch
โ† Linux Foundations

Network operations

route

Read and change the Linux routing table with route and its modern replacement ip route.

Every packet your machine sends needs a path. The routing table tells the kernel which interface to use and which router, if any, should get the packet next.

Viewing the table

The classic command is route. The -n option shows numbers instead of resolving names:

$ route -n
Kernel IP routing table
Destination  Gateway      Genmask        Flags Metric Ref Use Iface
0.0.0.0      192.168.1.1  0.0.0.0        UG    100    0   0   enp3s0
192.168.1.0  0.0.0.0      255.255.255.0  U     100    0   0   enp3s0

route is part of net-tools, which may not be installed. The modern equivalent is ip route:

$ ip route
default via 192.168.1.1 dev enp3s0 metric 100
192.168.1.0/24 dev enp3s0 proto kernel scope link src 192.168.1.25 metric 100

Reading the entries

  • Flag U means the route is up.
  • Flag G means it goes through a gateway.
  • Flag H means the destination is a single host.

The 0.0.0.0 destination with mask 0.0.0.0 is the default route. It is used when nothing more specific matches. When several routes match, the one with the longest prefix wins.

Adding and removing routes

These commands need root. To reach 10.20.0.0/24 through a second router:

$ sudo route add -net 10.20.0.0 netmask 255.255.255.0 gw 192.168.1.254
$ sudo route del -net 10.20.0.0 netmask 255.255.255.0

With ip:

$ sudo ip route add 10.20.0.0/24 via 192.168.1.254
$ sudo ip route del 10.20.0.0/24

A default gateway is set with sudo route add default gw 192.168.1.1 or sudo ip route add default via 192.168.1.1. The gateway must be reachable on a directly connected network.

These changes last until reboot, or until your network manager rewrites the table. Put permanent routes in its configuration. Be careful on remote machines, since removing the default route can lock you out.

Which route will be used?

$ ip route get 8.8.8.8
8.8.8.8 via 192.168.1.1 dev enp3s0 src 192.168.1.25

This asks the kernel what it would do, without sending your data.

Practise fault finding in the broken default route lab.

Try it

  1. Run ip route and identify your default gateway.
  2. Run ip route get for an address on your own subnet and one outside it.
  3. Compare the output. Which one lists via?

Check your understanding

route

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.