Skip to content

Routing

image.png

image.png

image.png

image.png

Linux Workstations typically have routing disabled by default, while Servers often have it enabled.

  • Check Forwarding Status:

    Terminal window
    sysctl net.ipv4.conf.all.forwarding
    # Or check the proc file directly
    cat /proc/sys/net/ipv4/ip_forward

    (A result of 1 means it is ON; 0 means it is OFF) .

  • Enable Forwarding (Permanent): Edit /etc/sysctl.conf and uncomment or add net.ipv4.ip_forward = 1.

image.png

image.png

  • Modern: ip route show.
  • Legacy: route -n.

Temporary Configuration (Lost after Reboot)

Section titled “Temporary Configuration (Lost after Reboot)”

Route utility is not built-in to install use sudo yum/dnf/apt-get install net-tools

  • Add a Route: sudo **ip route** **add** <network_ip>/<cidr> **via** <gateway_ip>.
  • Delete a Route: sudo **ip route** **del** <network_ip>/<cidr> **via** <gateway_ip>.
  • Ubuntu (Netplan): Edit YAML files in /etc/netplan/ to define routes under the specific interface.
  • RHEL/CentOS (nmcli): nmcli connection modify <conn-name> ipv4.routes "<network>/<prefix> <gateway>".
  • RHEL/CentOS (Manual): Create a file named route-<interface> in /etc/sysconfig/network-scripts/.
    • restart network.. sudo systemctl restart network

image.png

When a routing table contains multiple paths to the same destination, Linux follows these priorities:

  1. Longest Match: The route with the most specific (biggest) prefix/mask wins.

    image.png

    all three routes “match” the destination, but the router must pick only one:

    • Route 1 (/12): Only the first 12 bits match. This is a very broad, general route.
    • Route 2 (/18): The first 18 bits match. This is a more specific match than Route 1.
    • Route 3 (/26): The first 26 bits match. This is the most specific match possible in this list.

    Since /26 is greater than 18 and 12, the router will use Route 3 to forward your packet

    image.png

    The Decision Process:

    1. Check Prefix Length (Longest Match): Route A has a prefix of /25, and Route B has a prefix of /24.
    2. Evaluate: Because 25 bits is longer than 24 bits, Route A is considered more specific.
    3. Result: Linux chooses Route A immediately. It never even looks at the metrics because the “Longest Match” rule already found a clear winner .

    What happened when you deleted the path?

    When you ran sudo ip route del 10.0.3.0/25:

    • The /25 route (the “Longest Match”) was removed from the table.
    • Only the /24 route remained.
    • Since there was no longer a more specific match, the system was forced to use the only available path via 10.0.5.2.

  2. Lowest Metric: If prefixes are identical, the route with the lower cost (metric) is chosen.

    When does “Lowest Metric” actually matter?

    The “Lowest Metric” rule is only used as a tie-breaker when two routes have the exact same prefix length.

    Example Scenario:

    • 10.0.3.0/24 via 10.0.1.1 (Metric 10)
    • 10.0.3.0/24 via 10.0.5.2 (Metric 0) In this case, since both are /24, the system would finally check the metrics and choose the Metric 0 path because it is lower.

  1. Route Summarization: You can combine multiple smaller static routes into one single route address (e.g., summarizing 172.16.1.0/24, .2.0/24, and .3.0/24 into 172.16.0.0/22) to reduce the size of the routing table.

For large networks, routers use dynamic protocols to automatically update tables. Quagga is a popular software suite for this on Linux.

  • Zebra: The core daemon that talks to the Linux kernel.
  • Protocol Daemons: ospfd (OSPF), ripd (RIP), and bgpd (BGP) communicate with Zebra to update routes

image.png