Routing

Contents

Routing#

The kernel consults the routing table for every outbound packet to pick an interface and next hop. The match is longest-prefix, so a more specific route always wins over a broader one; the default route is just the broadest possible match:

+---------+      +---------------+      +-----------+      +---------+ | process | ---> | routing table | ---> | interface | ---> | next    | |         |      | (longest      |      | (eth0,    |      | hop /   | |         |      |  prefix)      |      |  wlan0)   |      | gateway | +---------+      +---------------+      +-----------+      +---------+
        flowchart LR
    P[process] --> R["routing table<br/>(longest prefix)"]
    R --> I["interface<br/>(eth0, wlan0)"]
    I --> H["next hop /<br/>gateway"]
    

A worked example. Three rules in the table, an outbound packet to 8.8.8.8, and the longest-prefix decision picks the default gateway because nothing more specific matches:

routing table:
    10.0.0.0/24    dev eth0  scope link        ◄── /24
    192.168.1.0/24 dev wlan0 scope link        ◄── /24
    0.0.0.0/0      via 192.168.1.1 dev wlan0   ◄── /0   (default)

packet  dst = 8.8.8.8
           │
           ▼
compare against each route's prefix:
    8.8.8.8 ∈ 10.0.0.0/24       ?  no
    8.8.8.8 ∈ 192.168.1.0/24    ?  no
    8.8.8.8 ∈ 0.0.0.0/0         ?  yes  (length 0)
           │
           ▼
pick longest match → default → wlan0 via 192.168.1.1

ip route get 8.8.8.8     prints the same answer the kernel computed.
$ ip route
$ ip route add default via 192.168.1.1
$ ip route get 8.8.8.8

NetworkManager#

NetworkManager is the default connectivity daemon on most desktop distributions and many servers. nmcli is its CLI; it stores connection profiles in /etc/NetworkManager/system-connections/ and brings them up automatically based on hardware presence and policy.

$ nmcli device status
$ nmcli con show
$ nmcli con up wifi-home
$ nmcli dev wifi connect <SSID> password <pwd>