December 5, 2026

The Linux Routing Table and the Default Gateway, Explained With a Three-Machine Lab

A home router with cables leading to a laptop and a small server

A machine on a test network sent a ping to a server one hop away and got back a one-word diagnosis: ping: connect: Network is unreachable. The cable was fine. The router was up. The machine simply had no idea where to send a packet for that address, because its routing table had no entry that matched. Adding one line, a default gateway, fixed it. Removing a different line, on the server this time, broke it again in a way that looked identical from the client but had a completely different cause. Every output below came from three Linux network namespaces wired together as a host, a router and a server, built for this post.

Video: NetSecProf on YouTube

The lab, and a route that isn’t there

Three machines, two networks

The host lived on 192.168.50.0/24 at address 192.168.50.10. The router had a leg on each network: 192.168.50.1 on the host’s side and 10.20.0.1 on the server’s. The server sat at 10.20.0.5 on 10.20.0.0/24. That’s the shape of a home network in miniature: a laptop, a router, and something on the other side of it.

What the host knew at the start

Right after its address was assigned, the host’s routing table had exactly one line:

$ ip route show
192.168.50.0/24 dev h0 proto kernel scope link src 192.168.50.10

The kernel added this route automatically when the address went on. It says: anything in 192.168.50.0/24 is directly reachable on interface h0, no router needed. proto kernel means the kernel created it; scope link means the destinations are on the local link.

Network is unreachable

The server’s address, 10.20.0.5, doesn’t fall inside 192.168.50.0/24. With nothing else in the table, the host had no instructions for it:

$ ping -c1 10.20.0.5
ping: connect: Network is unreachable

$ ip route get 10.20.0.5
RTNETLINK answers: Network is unreachable

The packet never left the machine. That error comes from the local kernel, not from the network. It’s the clearest possible sign that the routing table itself is the problem.

ip route get

That second command is the most useful routing tool most people never use. The ip-route manual page says it “gets a single route to a destination and prints its contents exactly as the kernel sees it.” Instead of reading the table and working out which line applies, it asks the kernel to do the lookup and show the answer.

Aerial view of a multi-level highway interchange with light trails

Adding the default gateway

One line

$ ip route add default via 192.168.50.1
$ ip route show
default via 192.168.50.1 dev h0
192.168.50.0/24 dev h0 proto kernel scope link src 192.168.50.10

The manual describes default as a special prefix that “is equivalent to IP 0/0.” A /0 prefix matches every address. The route says: for anything not matched more specifically, hand the packet to 192.168.50.1 and let it figure out the rest.

What the kernel decided

$ ip route get 10.20.0.5
10.20.0.5 via 192.168.50.1 dev h0 src 192.168.50.10

$ ip route get 192.168.50.77
192.168.50.77 dev h0 src 192.168.50.10

The first goes “via” the gateway. The second has no “via”: it’s on the local network, so the host sends it straight to that machine. The ping now worked, and the reply’s ttl=63 showed it had passed through exactly one router, which decremented the starting value of 64 by one.

Longest prefix wins

Overlapping routes

Real routing tables often have several routes that match the same address. To see how the kernel chooses, two more routes were added to the host: a broad one for all of 10.20.0.0/16 pointing to a nonexistent 192.168.50.99, and a narrow one for the single address 10.20.0.5/32 pointing to the real router.

default via 192.168.50.1 dev h0
10.20.0.0/16 via 192.168.50.99 dev h0
10.20.0.5 via 192.168.50.1 dev h0
192.168.50.0/24 dev h0 proto kernel scope link src 192.168.50.10

The lookups

$ ip route get 10.20.0.5   ->  via 192.168.50.1
$ ip route get 10.20.0.6   ->  via 192.168.50.99
$ ip route get 8.8.8.8     ->  via 192.168.50.1

10.20.0.5 matched three routes: the /32, the /16 and the default. The kernel chose the /32, the most specific. 10.20.0.6 didn’t match the /32, so the /16 won over the default. 8.8.8.8 matched only the default. The order lines appear in, or were added, doesn’t matter. The longest matching prefix wins.

Metrics break ties

When two routes have the same prefix, the metric decides. A second default route was added with metric 200:

$ ip route show default
default via 192.168.50.1 dev h0
default via 192.168.50.2 dev h0 metric 200

The manual describes the value as the route’s preference, and says “routes with lower values are preferred.” The first route has the implicit metric of 0, so it wins. This is how a laptop with both Wi-Fi and Ethernet connected decides which one carries traffic: each interface’s default route gets a different metric.

A close up of a white marble surface

The router’s half of the job

Forwarding must be turned on

A Linux machine with two network interfaces doesn’t route between them by default. The kernel’s ip-sysctl documentation describes ip_forward as “Forward Packets between interfaces,” with “Default: 0 (disabled).” In the lab, setting it back to 0 on the router made the host’s ping fail with 100% packet loss, even though every routing table was correct. Setting it to 1 brought the replies back.

The route back

The trickier failure came next. The server’s default route was deleted, while the host’s table stayed correct. The host’s ping failed again. A tcpdump on the server’s interface showed what was really happening:

IP 192.168.50.10 > 10.20.0.5: ICMP echo request, id 6343, seq 1, length 64

The request arrived. The server received it, built a reply addressed to 192.168.50.10, and then found it had no route for that address. The reply never left. From the host, the two failures looked identical: a ping with no answer. Only by watching both ends was it clear that one was broken going out and the other coming back. Routing is decided independently at every machine, in each direction.

Reading each field

proto, scope and src

Every route line carries labels that explain where it came from and how it behaves. The manual defines proto kernel as a route “installed by the kernel during autoconfiguration,” which is exactly the 192.168.50.0/24 line that appeared when the address was assigned. proto static, by contrast, marks a route “installed by the administrator to override dynamic routing.” Scope describes distance: the manual reserves scope link “for direct unicast and broadcast routes” and scope global “for all gatewayed unicast routes.” The src field names “the source address to prefer” when sending to that prefix, which is why every ip route get result in the lab ended with src 192.168.50.10.

Routes that refuse traffic

Not every route forwards packets. A separate test namespace was given three routes that discard traffic on purpose:

prohibit 192.0.2.0/24
unreachable 198.51.100.0/24
blackhole 203.0.113.0/24

Each failed differently when probed. A ping into the blackhole range returned ping: connect: Invalid argument, matching the manual’s note that packets “are discarded silently” while local senders get “an EINVAL error.” The unreachable range returned No route to host. For the prohibit range, ip route get 192.0.2.5 answered RTNETLINK answers: Permission denied. Three routes, three error messages, and none of them means the network is down.

These are handy for blocking a range without a firewall, or for stopping traffic from leaking out the default gateway when a VPN drops. Removing them in bulk is one command: ip route flush type blackhole deleted the blackhole line and left the prohibit route untouched.

A one way sign on a black wall

Tables behind the table

More than one table

ip route show displays the “main” table, but Linux keeps several. The ip-rule manual page describes the three rules every system starts with, and the lab host showed them exactly:

0:      from all lookup local
32766:  from all lookup main
32767:  from all lookup default

The local table, per the manual, holds “high priority control routes for local and broadcast addresses”; ip route show table local on the host listed entries for 127.0.0.1 and the loopback network. “The main table is the normal routing table containing all non-policy routes.” The default table “is empty” unless something puts routes there.

Why it matters

Extra rules let traffic be routed by source address, by firewall mark or by user, sending a VPN’s traffic one way and everything else another. Most home machines never need them, but when ip route show looks correct and traffic still goes somewhere unexpected, ip rule show and ip route get are the two commands that reveal what’s really deciding.

A troubleshooting order

From the inside out

  1. ip route get <destination>: does the kernel have a route at all, and through which gateway?
  2. Ping the gateway itself. If that fails, the problem is on the local link.
  3. On the router, check that forwarding is enabled.
  4. On the far machine, check that it has a route back, ideally with tcpdump running to see whether requests arrive.

Making it stick

Routes added with ip route add vanish at reboot. Permanent routes and gateways belong in the system’s network configuration, whether that’s NetworkManager, systemd-networkd, netplan or a router’s DHCP settings, which normally hand out the default gateway along with the address.

Sources

  • ip-route(8) manual page, man7.org: https://man7.org/linux/man-pages/man8/ip-route.8.html
  • ip-rule(8) manual page, man7.org: https://man7.org/linux/man-pages/man8/ip-rule.8.html
  • Linux kernel documentation, “IP Sysctl”: https://docs.kernel.org/networking/ip-sysctl.html

820 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *