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 enp3s0route 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 100Reading the entries
- Flag
Umeans the route is up. - Flag
Gmeans it goes through a gateway. - Flag
Hmeans 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.0With ip:
$ sudo ip route add 10.20.0.0/24 via 192.168.1.254
$ sudo ip route del 10.20.0.0/24A 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.25This asks the kernel what it would do, without sending your data.
Practise fault finding in the broken default route lab.
Try it
- Run
ip routeand identify your default gateway. - Run
ip route getfor an address on your own subnet and one outside it. - 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.