Routing and the default gateway
How Linux picks an outgoing interface and next hop for every packet, how to ask it with ip route get, and what policy routing and IPv6 add.
Interfaces, addresses and the ip command/proc, /sys and tuning with sysctl
Routing and the default gateway
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- For every outgoing packet the kernel looks up the destination in a routing table and picks the most specific matching route. That gives an interface, and for non-local destinations a next-hop gateway.
- Adding an address to an interface also adds a route for its subnet, marked `proto kernel scope link`. The default route, `default via <gateway>`, catches everything no other route matches.
- `ip route get <address>` prints the exact decision the kernel would make, including the source address it would use. It beats reading the table by eye.
- Linux has several tables. `local` (255) holds the machine's own addresses, `main` (254) is what `ip route` shows, and `ip rule` decides which table a packet consults. That is policy routing.
- IPv6 routing works the same way with `ip -6 route`. The default route usually arrives in router advertisements and points at a link-local `fe80::` address.
- A Linux host does not forward packets between interfaces unless `net.ipv4.ip_forward` is set to 1. The default is 0.
Every packet asks the same question
Your server just built a packet for 203.0.113.9. Before it can send a single byte, the kernel has to answer: which interface does this go out of, and to whom on that wire do I hand it? A machine with one network card and one router makes the answer look trivial. Add a VPN, a second network for backups, or a container bridge, and suddenly “why is this traffic going out the wrong way” is a real afternoon.
The answer comes from the routing table. Think of it as the sorting frame in a post office: each slot is labelled with a range of addresses, and the clerk drops a letter into the most specific slot that fits. A slot for “all of 10.x.x.x” and a slot for “10.2.x.x” can both match, and the letter goes into the narrower one. That rule, longest prefix match, is the whole lookup.
Reading the main table
ip route # IPv4 main table
ip -6 route # IPv6 main table
On a typical server you will see two kinds of line. One looks like 192.0.2.0/24 dev enp5s0 proto kernel scope link src 192.0.2.10. You did not add it. The kernel created it when the address went onto the interface, because hosts in your own subnet are reachable directly. The ip-route man page explains the fields: proto kernel means “installed by the kernel during autoconfiguration”, and direct routes default to scope link.
The other is the default route, default via 192.0.2.1 dev enp5s0. default is shorthand for 0.0.0.0/0, the least specific prefix there is, so it only wins when nothing else matches. via names the next hop, the gateway, which must itself be reachable through a connected route. If there are two defaults, the one with the lower metric wins.
Commands written from the iproute2 man pages, not run, since there is no Linux machine here.
Ask the kernel, don’t guess
Reading a table by eye goes wrong once it has more than a few lines. Ask instead:
ip route get 203.0.113.9
ip route get 203.0.113.9 from 192.0.2.10
The man page says this “gets a single route to a destination and prints its contents exactly as the kernel sees it”. The output names the gateway, the device and the src address the kernel would put on the packet. That last part settles a common puzzle: which of a machine’s several addresses the far side will see.
macOS has the same idea under a different command. This is a real run on the Mac I wrote this on, so the fields are BSD’s, not Linux’s:
$ route -n get 1.1.1.1
route to: 1.1.1.1
destination: default
mask: default
gateway: 192.168.1.1
interface: en0
The destination matched nothing more specific, so the default route won and the packet goes to the home router on en0.
Routes that refuse
Not every route forwards. The man page lists types that exist to stop traffic: blackhole discards silently, unreachable discards and sends back an ICMP host-unreachable, and prohibit sends an administratively-prohibited error. A blackhole route for an attacker’s prefix is a cheap way to drop their traffic before a firewall rule has to look at it.
More than one table: policy routing
ip route shows only the main table, ID 254. The ip-rule man page says the kernel starts with three rules: priority 0 looks up local (255), where the machine’s own and broadcast addresses live; priority 32766 looks up main; and priority 32767 looks up default (253), which starts empty. Rules are checked in priority order, and the first one that yields a route ends the search.
That is policy routing: choosing a table by something other than the destination. A server on two networks can send replies out the interface the request came in on:
sudo ip route add default via 198.51.100.1 dev enp6s0 table 100
sudo ip rule add from 198.51.100.20 table 100
Now traffic from the second address uses the second gateway. Without that rule, replies leave through the main default route and a strict upstream drops them as spoofed. VPN clients and Kubernetes network plugins use the same mechanism, often matching on a firewall mark instead of a source address.
IPv6 is the same lookup
ip -6 route works identically, longest prefix first. Two things look different. The default route usually shows proto ra, because it was learned from a router advertisement rather than configured. And its via is often a link-local fe80:: address, which only means something together with dev. Neighbour discovery replaces ARP for finding the gateway’s hardware address, and both are visible with ip neigh.
Hosts are not routers
A machine with two interfaces still drops a packet that arrives on one and is addressed to a network on the other. The kernel’s ip-sysctl document gives ip_forward a default of 0. Setting net.ipv4.ip_forward = 1, as covered in tuning with sysctl, is what turns a host into a router. Container hosts turn it on, and that is one reason their firewall rules matter.
About the video
The embed is all of LinuxCloudHacks’s “Linux IP Routing Basics: Routes, Tables, and Policy-Based Routing Explained”, August 2024, about 20 minutes. Its chapters run from destination classes, routes, route scopes and routing tables through ECMP, special routes, policy rules and routing marks, which follows this lesson closely and goes a bit further. I judged it from the title, chapters and description, not a full transcript. If you only want the commands, Learn Linux TV’s four-minute routing chapter is in deeper. English was judged from the channel and its English auto-caption track. The man-page quotes were read on man7.org on 2026-11-28. The route get output above is the only command I ran.
Where this fits
Routes take addresses. People type names. Next, how a name becomes an address on Linux: name resolution on Linux.
Check yourself
A server has `10.0.0.0/8 via 10.1.0.1` and `10.2.0.0/16 via 10.1.0.254`. Which gateway does a packet to 10.2.3.4 use, and why?
10.1.0.254, because the /16 is the longer, more specific prefix. The kernel prefers the most specific match, not the first line in the table.
`ip route` shows no line starting with `default`. What can the machine still reach?
Only networks it has explicit routes for, such as its directly connected subnets. Anything else fails with 'Network is unreachable'.
You add `ip rule add from 192.0.2.0/24 table 100`. What changes?
Packets with a source address in 192.0.2.0/24 now look up table 100 before the main table. Other traffic still uses the default rules, so it is a way to send one source's traffic out a different gateway.
Why does a route added with `ip route add` vanish after reboot?
Like `ip addr add`, it changes only the running kernel. A persistent route has to be written into NetworkManager, netplan or systemd-networkd configuration.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.