Routing basics

Topic: Networking basics

Summary

Routing is how a host or router decides where to send a packet: it consults a routing table (destination prefix and next-hop or interface) and forwards the packet. Learn how default route and longest prefix match work so you can fix 'no route to host' and configure gateways. Use this when a host cannot reach certain networks.

Intent: How-to

Quick answer

  • Each host and router has a routing table: entries are (destination prefix, next-hop or interface). The host looks up the destination IP and sends the packet to the matching next-hop or out the matching interface.
  • Longest prefix match: if 10.0.0.0/8 and 10.0.1.0/24 both match, the /24 is used. Default route (0.0.0.0/0) matches everything not matched by a more specific route.
  • A host needs a default route (or a route to the target network) to reach off-link destinations; 'no route to host' usually means no matching route or the next-hop is unreachable.

Prerequisites

Steps

  1. Understand the routing table

    Entries have destination (prefix), gateway (next-hop IP or 'on-link'), and optionally interface. The host picks the entry with the longest matching prefix for the packet's destination IP.

  2. Use default route

    0.0.0.0/0 (or ::/0 for IPv6) is the default; traffic that matches no more specific route goes to the default gateway. A typical host has one default route pointing to the router.

  3. Relate to connectivity

    To reach 8.8.8.8, the host needs a route (usually default). The packet goes to the gateway; the gateway forwards according to its table. If the host has no default or the gateway is down, off-link destinations fail.

  4. Inspect and fix on Linux

    ip route or route -n shows the table; add default with ip route add default via GATEWAY_IP; ensure the gateway is on-link (same subnet as an interface) or add a route to the gateway first.

Summary

Routing is the process of choosing where to send a packet using a routing table. Longest prefix match selects the route; the default route (0.0.0.0/0) is used when no more specific route matches. Use this when fixing “no route to host” or configuring gateways.

Prerequisites

Steps

Step 1: Understand the routing table

Each entry has a destination prefix (e.g. 10.0.0.0/8 or 0.0.0.0/0), a next-hop (gateway IP or “on-link”), and optionally an interface. The host finds the entry whose prefix contains the packet’s destination IP and uses the longest such prefix (longest prefix match).

Step 2: Use default route

The default route has destination 0.0.0.0/0 (IPv4) or ::/0 (IPv6). Any destination that does not match a more specific route uses this entry. The next-hop is typically the LAN router so all “internet” traffic goes there first.

Step 3: Relate to connectivity

If the host has no default route, it can only reach destinations that match other routes (e.g. on-link subnets). If the default gateway is wrong or unreachable, off-link traffic fails. “No route to host” (ICMP or kernel) usually means no route or unreachable next-hop.

Step 4: Inspect and fix on Linux

  • ip route or route -n: show table.
  • ip route add default via 192.168.1.1: add default (replace with your gateway).
  • The gateway must be reachable (typically on the same subnet as one of the host’s interfaces).

Verification

  • You can read a routing table and identify the default route; you know that missing or wrong default causes failure to reach off-link hosts.

Troubleshooting

No default route — Add one: ip route add default via GATEWAY; make it persistent in netplan or NetworkManager. Ensure the gateway IP is correct and on-link.

Gateway unreachable — Check that the interface has an IP in the same subnet as the gateway; check cable and switch; ping the gateway.

Next steps

Continue to