IP Routing and Subnetting with CIDR on Linux
I subdivided a /24 into role-based CIDR subnets and configured a Linux host as a router to forward traffic between them. Correct subnet sizing and route tables produced verified end-to-end reachability with no overlap or black-hole routes.
Objective & Context
Subnetting controls broadcast domains and enforces segmentation; routing connects those segments. This lab plans address space with VLSM and turns a multi-homed Linux box into a forwarding router, the foundation for the VLAN and zero-trust segmentation labs.
Learning Objectives
- Perform VLSM subnet math with ipcalc to carve a /24 into role-based subnets.
- Enable IPv4 forwarding on Linux persistently via sysctl.
- Add, inspect, and remove static routes with the ip route commands.
- Verify bidirectional reachability across routed segments.
- Explain how subnetting shrinks broadcast domains and enforces segmentation.
Environment & Prerequisites
Prerequisites
- A Linux router VM with two interfaces; two client VMs.
ipcalcfor subnet math; iproute2 for routes.- A /24 to subdivide (for example 192.168.10.0/24).
- A Linux router VM with two network interfaces and two client VMs (or network namespaces).
- iproute2 installed (standard on modern distributions) and ipcalc (sudo apt install -y ipcalc).
- Privileged access (sudo) to change sysctl settings and the routing table.
- A /24 to subdivide, for example 192.168.10.0/24.
- Comfort reading CIDR notation and binary subnet math.
Step-by-Step Execution
The outputs on this page come from a re-run on 14 November 2026 in an Ubuntu 24.04 container (arm64), with a router and a host in network namespaces. Sample files, users, and hosts were created for the run, so names, numbers, and timestamps reflect that run.
1. Plan subnets with ipcalc
ipcalc 192.168.10.0/24 --split 60 60Output:
Address: 192.168.10.0 11000000.10101000.00001010. 00000000
Netmask: 255.255.255.0 = 24 11111111.11111111.11111111. 00000000
Wildcard: 0.0.0.255 00000000.00000000.00000000. 11111111
=>
Network: 192.168.10.0/24 11000000.10101000.00001010. 00000000
HostMin: 192.168.10.1 11000000.10101000.00001010. 00000001
HostMax: 192.168.10.254 11000000.10101000.00001010. 11111110
Broadcast: 192.168.10.255 11000000.10101000.00001010. 11111111
Hosts/Net: 254 Class C, Private Internet
1. Requested size: 60 hosts
Netmask: 255.255.255.192 = 26 11111111.11111111.11111111.11 000000
Network: 192.168.10.0/26 11000000.10101000.00001010.00 000000
HostMin: 192.168.10.1 11000000.10101000.00001010.00 000001
HostMax: 192.168.10.62 11000000.10101000.00001010.00 111110
Broadcast: 192.168.10.63 11000000.10101000.00001010.00 111111
Hosts/Net: 62 Class C, Private Internet
2. Requested size: 60 hosts
Netmask: 255.255.255.192 = 26 11111111.11111111.11111111.11 000000
Network: 192.168.10.64/26 11000000.10101000.00001010.01 000000
HostMin: 192.168.10.65 11000000.10101000.00001010.01 000001
HostMax: 192.168.10.126 11000000.10101000.00001010.01 111110
Broadcast: 192.168.10.127 11000000.10101000.00001010.01 111111
Hosts/Net: 62 Class C, Private Internet
Needed size: 128 addresses.
Used network: 192.168.10.0/25
Unused:
192.168.10.128/25
What this teaches: VLSM planning turns a host-count requirement into exact network, broadcast, and usable ranges before anything is configured.
The classic Debian/Ubuntu ipcalc package takes requested host counts with --split (or -s) and rounds each up to the next prefix boundary: 60 hosts rounds up to 64 addresses, a /26. Verify with: sudo apt install -y ipcalc.
2. Enable IP forwarding [PRIVILEGED]
sysctl -w net.ipv4.ip_forward=1 && echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.confOutput:
net.ipv4.ip_forward = 1
What this teaches: Linux does not forward packets between interfaces by default; ip_forward is the single switch that makes a host a router.
Repeating the echo appends duplicate lines. Prefer a drop-in file instead: echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-router.conf, then sudo sysctl --system.
3. Add and inspect routes
ip route add 192.168.10.64/26 via 192.168.10.1 && ip route showOutput:
192.168.10.0/26 dev eth0 proto kernel scope link src 192.168.10.62
192.168.10.64/26 via 192.168.10.1 dev eth0
What this teaches: the routing table is consulted for every packet, and longest-prefix match decides which route wins. The next hop must be a router on a connected subnet, here 192.168.10.1. A route via the host's own address (192.168.10.62) sends the packets straight back to this host, and traceroute then fails with !H (host unreachable).
How It Works
Subnetting borrows host bits to create multiple networks from one block. A /26 splits a /24 into four networks of 62 usable hosts each, and VLSM lets each subnet be sized to its actual need instead of forcing uniform splits. The network address, broadcast address, and usable range all derive mechanically from the prefix length.
Routing is the process of selecting an egress interface and next hop for each destination. Linux consults the routing table for every forwarded packet and applies longest-prefix match: among all matching routes, the one with the longest prefix (most specific network) wins. A default route (0.0.0.0/0) matches everything and therefore loses to any more specific entry.
Enabling net.ipv4.ip_forward lets the kernel move packets between interfaces, which is what turns a multi-homed host into a router. Static routes are explicit entries added by the administrator; they are simple and predictable but do not adapt to failures, which is the problem dynamic routing protocols solve.
Validation & Testing
From a client in subnet A, ping a client in subnet B and trace the path through the router. Pass criteria: successful cross-subnet ping, the router appears as the first hop, and no overlapping routes exist.
traceroute 192.168.10.70Output:
traceroute to 192.168.10.70 (192.168.10.70), 30 hops max, 60 byte packets
1 192.168.10.1 (192.168.10.1) 0.077 ms 0.012 ms 0.010 ms
2 192.168.10.70 (192.168.10.70) 0.833 ms 0.028 ms 0.021 ms
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| No connectivity between subnets | IP forwarding disabled or a missing route on one side | Confirm net.ipv4.ip_forward=1 and that both the router and clients have return routes |
| One direction works, the other does not | Asymmetric routing: the return path is missing on the far host | Add the reverse route on the destination network's gateway, not just the source side |
| Wrong route is chosen | A less specific route is winning because the intended one was never added | Run ip route get <dst> to see exactly which route the kernel selects |
| Forwarding works until reboot | sysctl -w changed only the runtime value | Persist the setting in /etc/sysctl.conf or /etc/sysctl.d/ |
| Overlapping subnets | Two subnet plans share addresses, so longest-prefix match behaves unexpectedly | Re-run ipcalc on the full plan and confirm zero overlap before configuring |
Advanced: Troubleshooting
- No cross-subnet traffic: confirm
ip_forward=1and that clients use the router as gateway. - Overlapping subnets: recompute with ipcalc; overlaps cause unpredictable routing.
- Asymmetric routing: ensure return routes exist on both segments.
Key Results
- Segmented one /24 into 2 role-based /26 subnets with zero overlap.
- Achieved verified bidirectional reachability across segments.
- Reduced each broadcast domain by roughly 75% versus a flat /24.
- Documented a reusable VLSM plan for future segments.
Key Takeaways
- Plan the address space on paper (or with ipcalc) before touching any interface.
- ip route get is the fastest way to see which route a packet will take.
- Persist sysctl changes; runtime-only changes vanish on reboot.
- Every routed path needs a return route: connectivity is always bidirectional.
Practice Challenges
Challenge 1: Add a third subnet
Goal: Carve a third /26 for a DMZ out of the same /24 and route it through the Linux router.
Hint: Re-run ipcalc with three 60-host requests and add routes on both the router and the clients.
Challenge 2: Simulate a failed link
Goal: Take down one router interface, observe which routes break, and add a backup static route with a higher metric.
Hint: Use ip route add ... metric 200 for the backup path and test with ip route get.
Frequently Asked Questions
How do I fix 'No connectivity between subnets'?
Likely cause: IP forwarding disabled or a missing route on one side. Fix: Confirm net.ipv4.ip_forward=1 and that both the router and clients have return routes.
How do I fix 'One direction works, the other does not'?
Likely cause: Asymmetric routing: the return path is missing on the far host. Fix: Add the reverse route on the destination network's gateway, not just the source side.
How do I fix 'Wrong route is chosen'?
Likely cause: A less specific route is winning because the intended one was never added. Fix: Run ip route get <dst> to see exactly which route the kernel selects.
How do I fix 'Forwarding works until reboot'?
Likely cause: sysctl -w changed only the runtime value. Fix: Persist the setting in /etc/sysctl.conf or /etc/sysctl.d/.
How do I fix 'Overlapping subnets'?
Likely cause: Two subnet plans share addresses, so longest-prefix match behaves unexpectedly. Fix: Re-run ipcalc on the full plan and confirm zero overlap before configuring.
Cite This Page
Citation formats
Plain text
Taki Sadik, "IP Routing and Subnetting with CIDR on Linux", TYFSADIK, https://tyfsadik.org/blog/labs/networking/ip-routing-subnetting (accessed 2026-10-22).
BibTeX
@misc{sadik-ip-routing-subnetting,
author = {Sadik, MD. Taki Yasir Faraji},
title = {IP Routing and Subnetting with CIDR on Linux},
year = {2026},
url = {https://tyfsadik.org/blog/labs/networking/ip-routing-subnetting},
urldate = {2026-10-22}
}
APA
Sadik, M. T. Y. F. (2026). IP Routing and Subnetting with CIDR on Linux. TYFSADIK. https://tyfsadik.org/blog/labs/networking/ip-routing-subnetting