Every host and router keeps a routing table: a list of destinations and how to reach them. When a packet needs to leave, the system looks it up in this table.
Reading the table
On Linux, ip route (short for ip route show) prints the main IPv4 table:
$ ip route
default via 192.168.1.1 dev eth0 proto dhcp metric 100
10.8.0.0/24 dev wg0 proto kernel scope link src 10.8.0.2
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.50 metric 100Each line starts with a destination. The other fields you will meet most:
via: the next router (the next hop)dev: the interface to send throughsrc: the preferred source addressproto: who added the route (kernel,dhcp,staticand so on)metric: the cost, where lower is preferred
The exact fields differ between distributions and network managers. Older systems may use route -n from the net-tools package, which is often not installed today.
Connected routes
A line with no via and with scope link is a directly connected network. The kernel adds it when you give an interface an address. Hosts there are reached without any router.
Longest prefix match
Several routes can match one destination. The most specific one, with the longest prefix, wins. default is 0.0.0.0/0, which matches everything but is the least specific. Ask the kernel what it would do:
$ ip route get 10.8.0.7
10.8.0.7 dev wg0 src 10.8.0.2 uid 1000
cacheBoth default and 10.8.0.0/24 match, and the /24 wins. If two routes have the same prefix, the lower metric is chosen.
Adding and removing routes
A route you type in yourself is a static route. This sends one network through another router on your LAN:
$ sudo ip route add 203.0.113.0/24 via 192.168.1.254
$ ip route show 203.0.113.0/24
203.0.113.0/24 via 192.168.1.254 dev eth0
$ sudo ip route del 203.0.113.0/24The next hop must be reachable, normally on a connected network. Routes added with ip disappear at reboot. To keep them, use your distribution's network configuration, such as Netplan, NetworkManager or systemd-networkd. Take care on remote machines: changing the default route can cut your own session.
Practise finding a broken default route in this troubleshooting lab.
Try it
Run ip route and label each line as connected, default or other. Then run ip route get with an address on your LAN and with 198.51.100.1. Compare which interface and next hop each one uses.
Check your understanding
The Routing Table
All 5 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.