linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Simulator
  • Knowledge base
  • Interview prep
Index
Categories
All entries
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
home/linux/kb/Networking: L2 / L3/routing-table

kb/network-l2-l3 ── Networking: L2 / L3 ── intermediate

Routing table

The routing table lists where to send packets for each destination. The longest matching prefix wins.

view as markdownaka: routing, ip-route, routes

What the table contains

Each entry holds:

  • destination - the target subnet (10.0.0.0/24, or 0.0.0.0/0 for the default route)
  • next hop - the IP of the next router (via X.X.X.X), or - when the subnet is directly connected
  • dev - the interface to send the packet out of
  • metric - priority (lower is better) when multiple entries match
  • scope - link (same subnet) or global
  • proto - who installed the route: kernel, static, boot, ra (router advertisement), bgp, ospf, dhcp

Longest prefix match

When a packet is destined for 1.2.3.4, the kernel finds the longest matching subnet in the table. For example:

default via 192.168.1.1 dev eth0
10.0.0.0/8 via 192.168.1.254 dev eth0
10.42.0.0/16 via 10.42.0.1 dev wg0
  • 8.8.8.8 - does not match 10.x.x.x, falls through to default, exits via 192.168.1.1
  • 10.99.5.5 - matches /8 but not /16, exits via 192.168.1.254
  • 10.42.7.7 - matches both /8 and /16, but /16 is longer, so it exits via wg0

This rule is universal. BGP and OSPF do not change it: they only INSERT entries into the table. The selection itself is done by the kernel FIB.

Default route

0.0.0.0/0 covers everything not matched by a more specific entry. On a regular host this entry is required for internet access.

Without a default route:

$ ping 8.8.8.8
ping: connect: Network is unreachable

How to inspect the table

bash
ip route                  # full main table
ip -4 route show           # IPv4 only
ip route get 1.2.3.4       # which route the kernel would pick for 1.2.3.4 right now

Besides main, there are also the local table (addresses of the machine itself) and the default table (separate tables selected via ip rule for policy routing).

Where the kernel stores this

  • RIB (Routing Information Base) - what routing protocols and the admin write in
  • FIB (Forwarding Information Base) - a lookup-optimized structure (trie) that the kernel actually uses to forward each packet

For most tasks the difference does not matter.

§ команды

bash
ip route

Print the current main routing table

bash
ip route get 8.8.8.8

Show which route the kernel would pick for 8.8.8.8, including the auto-selected source IP

bash
sudo ip route add 10.5.0.0/16 via 10.0.0.1

Add a static route (requires NET_ADMIN capability)

bash
sudo ip route add default via 192.168.1.1

Set the default gateway

bash
ip rule

Policy routing: rules that select which routing table to use (used for multi-WAN and VPN split-routing)

§ см. также

  • ipv4-addressingIPv4: Addressing and CIDRAn IPv4 address is 32 bits written as `a.b.c.d`. The **/N** suffix is the prefix length: `/24` fixes the first 24 bits for the network and leaves 8 bits for hosts (256 addresses).
  • subnetting-cidrSubnetting and CIDRCIDR /N specifies how many of the 32 bits (or 128 for IPv6) belong to the network. /24 gives 256 addresses, /30 gives 4 (p2p), /16 gives 65536. A host in a subnet can communicate directly only with hosts that share the same prefix.
  • default-gatewayDefault gateway: leaving your own networkThe router IP in your subnet where the stack sends packets for every address that **is not local**. One gateway per host, but in multi-homed setups there can be several.
  • policy-routingPolicy Routing: Rule-Based RoutingPolicy routing selects a routing table based on src-IP, fwmark, iif, or tos. ip rule + ip route table N. Multi-uplink, source-based routing, VRF, split-tunnel VPN. RPDB is the Routing Policy Database.
  • bgpBGP: Border Gateway Protocol**BGP** is the routing protocol between autonomous systems (AS). It is the language ISPs and large networks use to exchange reachability information: which AS owns which prefixes and how to reach them.
  • ospfOSPF: Open Shortest Path First**OSPF** is a link-state IGP inside one autonomous system. Each router knows the full topology (LSDB) and independently computes shortest paths with Dijkstra. Convergence takes seconds.

§ упоминается в уроках

  • ›advanced-08-bgp-minimal
  • ›intermediate-01-network-101
  • ›intermediate-03-three-node-routing
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies