Table of Contents
Understanding Subnets
In the networking fundamentals chapter, you saw what an IP address is. Here we focus on how networks are divided (subnets) and how traffic is forwarded (routing), mostly from a Linux administrator’s perspective.
Network address, host address, and netmask
Every IPv4 address belongs to a network and identifies a host within that network.
- An IPv4 address is 32 bits.
- A subnet mask (or netmask) is also 32 bits.
- The subnet mask says which bits are the network part and which bits are the host part.
Example:
- IP:
192.168.1.10 - Netmask:
255.255.255.0
In binary:
- IP:
192→11000000168→101010001→0000000110→00001010- Netmask:
255→11111111255→11111111255→111111110→00000000
Where the netmask has 1 bits → network part; where it has 0 bits → host part.
So:
- Network part:
192.168.1 - Host part:
.10
Network address: set all host bits to 0:
192.168.1.0
Broadcast address: set all host bits to 1:
192.168.1.255
Usable host addresses: 192.168.1.1 to 192.168.1.254
CIDR notation
Instead of writing the full netmask (255.255.255.0), we often use CIDR notation:
192.168.1.10/24
The /24 means: “the first 24 bits are network bits.” So the netmask is:
- 24 ones, then 8 zeros →
11111111.11111111.11111111.00000000→255.255.255.0.
A few common CIDR lengths:
/24→ 255.255.255.0 → 256 addresses (254 usable hosts)/16→ 255.255.0.0 → 65,536 addresses/8→ 255.0.0.0 → 16,777,216 addresses/30→ 255.255.255.252 → 4 addresses (2 usable hosts) — often used for point‑to‑point links/32→ 255.255.255.255 → 1 address (used to refer to a single host interface/route)
General rule:
- Total addresses in a subnet: $2^{(32 - \text{prefix})}$
Example: /24 → $2^{(32 - 24)} = 2^8 = 256$ addresses.
Basic subnetting examples
Example 1: Splitting a /24 into two /25s
You have 192.168.1.0/24 and want two equal networks:
/24→ 256 addresses/25→ $2^{(32-25)} = 128$ addresses
So:
- First subnet:
192.168.1.0/25 - Range: 192.168.1.0 – 192.168.1.127
- Usable hosts: 192.168.1.1 – 192.168.1.126
- Second subnet:
192.168.1.128/25 - Range: 192.168.1.128 – 192.168.1.255
- Usable hosts: 192.168.1.129 – 192.168.1.254
Example 2: Creating four /26 networks from a /24
A /26 has 64 addresses:
- $2^{(32-26)} = 2^6 = 64$
From 192.168.1.0/24 you get:
192.168.1.0/26(0–63)192.168.1.64/26(64–127)192.168.1.128/26(128–191)192.168.1.192/26(192–255)
Private address ranges (for subnetting at home/lab)
When you design subnets on private networks, you normally use RFC1918 ranges:
10.0.0.0/8172.16.0.0/12(172.16.0.0 to 172.31.255.255)192.168.0.0/16
Linux tools will happily work with any valid ranges, but these are non-routable on the public internet and meant for internal use.
Viewing IP addresses and subnets on Linux
Modern systems use the ip command (from iproute2).
List IP addresses and prefix lengths
ip address show
# or shorter:
ip aLook for lines like:
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic noprefixroute enp0s3This tells you:
- IP:
192.168.1.10 - Prefix:
/24(so netmask 255.255.255.0) - Broadcast:
192.168.1.255 - Interface:
enp0s3
To show a specific interface:
ip address show dev enp0s3Adding or changing an IP address/subnet
To temporarily configure an IP and subnet on an interface:
sudo ip address add 192.168.10.5/24 dev enp0s3To remove it:
sudo ip address del 192.168.10.5/24 dev enp0s3This change is not persistent; after reboot or interface restart, it will disappear. Persisting this configuration is done in your distribution’s network configuration system (covered elsewhere).
Basics of Routing
Routing is how the system decides where to send packets that are not on the local subnet.
- If destination is on the same subnet → send directly (ARP for MAC, etc.).
- If destination is not on the same subnet → send to a router (gateway).
As a Linux admin, you mainly:
- Inspect the routing table.
- Set or change the default gateway.
- Add static routes when needed.
The routing table
Use ip route to see the kernel’s routing table:
ip route show
# or:
ip rTypical output on a simple desktop:
default via 192.168.1.1 dev enp0s3 proto dhcp metric 100
192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.10 metric 100Interpretation:
192.168.1.0/24 dev enp0s3
“For any address in 192.168.1.0/24, send outenp0s3directly.”default via 192.168.1.1 dev enp0s3
“For anything else, send to 192.168.1.1 (the default gateway).”
The default route is sometimes written as 0.0.0.0/0, meaning “all IPv4 addresses.”
Longest-prefix match
When multiple routes could match a destination, Linux uses the longest prefix match rule:
- The route with the most specific prefix (largest
/number) wins.
Example routing table snippet:
192.168.0.0/16 via 10.0.0.1 dev enp0s8
192.168.1.0/24 dev enp0s3
default via 192.168.1.1 dev enp0s3Packets to:
192.168.1.50→ matches192.168.0.0/16and192.168.1.0/24, but/24is longer than/16, so192.168.1.0/24 dev enp0s3is used.192.168.2.10→ matches only192.168.0.0/16, so goes via10.0.0.1.8.8.8.8→ doesn’t match first two routes, so goes todefaultvia192.168.1.1.
Understanding longest-prefix match is essential when troubleshooting routing issues.
Adding and removing routes (temporarily)
Add a default route (gateway)
If your system did not get a gateway via DHCP, you can add one:
sudo ip route add default via 192.168.1.1 dev enp0s3This immediately updates the routing table. It is lost on reboot unless you make it persistent through your network manager or config files.
Add a static route to a specific network
Example: to reach 10.0.0.0/24 via router 192.168.1.254 on interface enp0s3:
sudo ip route add 10.0.0.0/24 via 192.168.1.254 dev enp0s3
Now any packet to 10.0.0.x will be sent to 192.168.1.254.
Remove a route
Use the same syntax but with del:
sudo ip route del 10.0.0.0/24 via 192.168.1.254 dev enp0s3Or delete a default route:
sudo ip route del default via 192.168.1.1 dev enp0s3Example: Multi-network Linux host
Imagine a Linux machine acting as a small router or firewall:
enp0s3:192.168.1.1/24(LAN)enp0s8:10.0.0.1/24(DMZ)
Configure IPs:
sudo ip address add 192.168.1.1/24 dev enp0s3
sudo ip address add 10.0.0.1/24 dev enp0s8Routing table might show:
192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.1
10.0.0.0/24 dev enp0s8 proto kernel scope link src 10.0.0.1The host can now route between these two subnets (with IP forwarding enabled, which is handled elsewhere). Devices on each subnet would use this Linux machine as their default gateway.
Connectivity and routing troubleshooting
When things do not work as expected, focus on:
- Is the IP/subnet correct on the interface?
ip address show dev enp0s3
Check that the inet line shows the expected address and prefix.
- Is there a route to the destination network?
ip route showLook for:
- A specific
x.y.z.0/nnroute, or - A
defaultroute.
- Can you reach at least the gateway?
ping -c 3 192.168.1.1- Are you picking the right route?
Use ip route get to see which route would be used:
ip route get 8.8.8.8Output example: