Linux › Section I · Your network and its configuration › Topic I.3
Routes, the routing table, and the default gateway
An address tells the world where a Linux machine is; the routing table tells the machine where to send everything else. Reading that table, and knowing how the kernel picks one route from several, is the key to solving most cannot-reach-the-network problems.
Two kinds of destination
When a Linux host sends a datagram, the kernel must decide where it goes first. If the destination is on a subnet the host is attached to, the datagram can go straight to it across the local link. If not, it must go to a router that knows the way onwards. The kernel makes this choice for every outgoing datagram by consulting its routing table, a list of rules held in kernel memory. Each rule pairs a destination prefix with a way to reach it: a directly attached interface, or a next-hop router reached through one.
Reading ip route show
ip route show, or just ip route, prints the main routing table. A typical laptop on a home LAN shows two lines (illustrative): default via 192.168.1.1 dev enp3s0 proto dhcp src 192.168.1.20 metric 100, and 192.168.1.0/24 dev enp3s0 proto kernel scope link src 192.168.1.20 metric 100. The first field is the destination. via names the next-hop router, and dev the interface to send on. proto records who added the route, scope link means the destination is directly reachable on that link, src is the source address the kernel prefers for these datagrams, and metric ranks competing routes.
Where routes come from
You rarely type routes yourself. When an address such as 192.168.1.20/24 is put on an interface, a route for its whole subnet normally appears automatically, marked proto kernel. A DHCP client usually adds the default route, often marked proto dhcp. Routes an administrator sets in configuration may show proto static, and IPv6 routes learned from router advertisements show proto ra. A route added by hand with ip route add and no proto option is recorded as proto boot. These labels are bookkeeping: the kernel forwards the same way whatever added the route, but they tell you which program to blame when a route is wrong.
The default route and gateway
The destination default matches every address; in CIDR notation it is 0.0.0.0/0 for IPv4. The router named by its via field is the default gateway, the place a host sends anything no other rule covers. The gateway must sit on a subnet the host is directly attached to, because the host has to reach it across one link. On many /24 LANs the router is the .1 address, such as 192.168.1.1, but that is only a convention. A host with no default route can still reach its own subnet; for anything else, programs fail with an error such as Network is unreachable.
How the kernel chooses a route
Often several routes match. A datagram for 192.168.1.77 matches both default and 192.168.1.0/24. The order of the lines in the table does not decide. The kernel chooses the most specific match, the route with the longest matching prefix, which is the same longest-prefix rule that the Networking topic Inside a router describes for internet routers. A /24 beats a /0, so local traffic goes direct. Only when two routes have the same prefix, such as two default routes, does the metric decide: the lower metric wins, and the other waits as a backup.
Worked example: picking routes
A host's table holds: default via 192.168.1.1; 192.168.1.0/24 dev enp3s0; and 10.8.0.0/16 via 192.168.1.254, a lab router. Where does each datagram go? 1. 192.168.1.77 matches /24 and /0; the /24 is longer, so it is sent directly on enp3s0. 2. 10.8.3.4 matches /16 and /0; the /16 wins, so it goes to 192.168.1.254. 3. 203.0.113.9 matches only the default route, so it goes to 192.168.1.1. 4. 10.9.0.1 is outside 10.8.0.0/16, whose addresses run only to 10.8.255.255, so it also takes the default route. Check: every destination matches default, so nothing is left without a route.
Asking the kernel with ip route get
Working out matches by hand is good practice, but Linux will answer the question directly. ip route get 203.0.113.9 prints the single route the kernel would use for that destination, in a line such as 203.0.113.9 via 192.168.1.1 dev enp3s0 src 192.168.1.20 (illustrative). No packet is sent; the kernel simply performs the lookup and reports it. This is the quickest way to test a new route, or to find out why traffic leaves by the wrong interface on a machine with several networks, a VPN or containers. Adding or deleting routes yourself is covered in Configuring interfaces and network managers.
Worked example: two default routes
A laptop is connected by cable and by Wi-Fi at the same time, and ip route shows two default routes: default via 192.168.1.1 dev enp3s0 metric 100, and default via 192.168.1.1 dev wlp2s0 metric 600 (illustrative values, matching NetworkManager's usual defaults of 100 for wired and 600 for Wi-Fi). 1. Both have prefix length 0, so the longest-prefix rule cannot separate them. 2. The kernel compares metrics, and 100 is lower, so internet traffic leaves by the wired interface. 3. When the cable is unplugged, the network manager removes the wired route. 4. The Wi-Fi route is now the only default, so traffic moves to wlp2s0. Check: ip route get 203.0.113.9 would name enp3s0 before step 3 and wlp2s0 after it.
Legacy tools and other tables
Older guides show routes with route -n or netstat -rn, both from the legacy net-tools package. The -n option prints addresses as numbers, avoiding slow name lookups, and the destination 0.0.0.0 in their output is the default route. Linux in fact keeps several routing tables. ip route shows the main table; ip route show table local lists routes for the machine's own addresses, which the kernel manages itself. Rules that choose between tables, called policy routing and managed with ip rule, are used by VPN clients and multi-homed servers, but an ordinary host needs only the main table.
Common mistakes to avoid
First, thinking the first matching line in ip route wins; the longest prefix wins, and the metric only breaks ties. Second, choosing a gateway that is not on a directly attached subnet; the host cannot reach it. Third, assuming the gateway is always the .1 address. Fourth, reading a smaller metric as worse; lower is preferred. Fifth, deleting the default route on a remote server; you will lose every connection from outside your subnet, possibly including your own. Sixth, forgetting that ip route get only reports the kernel's choice and sends nothing.
Key takeaways
The kernel's routing table pairs destination prefixes with an interface (dev) and, for remote networks, a next-hop router (via). ip route shows it; proto says who added each route, and src the preferred source address. The default route, 0.0.0.0/0, sends everything else to the default gateway, which must be on a directly attached subnet. The kernel picks the longest matching prefix, and the lowest metric among equals. ip route get ADDRESS reports the exact route the kernel would use. route -n and netstat -rn are legacy views of the same table.
Flashcards
Flashcard
Kernel list of destination prefixes and how to reach each
Test your understanding
Question 1 of 5
A table holds default via 192.168.1.1 and 192.168.1.0/24 dev enp3s0. Which route does the kernel use for 192.168.1.40, and why?
Question 2 of 5
Two default routes exist: one with metric 100 on enp3s0 and one with metric 600 on wlp2s0. Which carries internet traffic?
Question 3 of 5
What does ip route get 203.0.113.9 do?
Question 4 of 5
A host is 192.168.1.20/24. Which address can it use as its default gateway?
Question 5 of 5
In the route 192.168.1.0/24 dev enp3s0 proto kernel scope link, what does proto kernel indicate?
