Kahibaro
Discord Login Register

Subnets and routing

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.

Example:

In binary:

Where the netmask has 1 bits → network part; where it has 0 bits → host part.

So:

Network address: set all host bits to 0:

Broadcast address: set all host bits to 1:

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:

The /24 means: “the first 24 bits are network bits.” So the netmask is:

A few common CIDR lengths:

General rule:

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:

So:

Example 2: Creating four /26 networks from a /24

A /26 has 64 addresses:

From 192.168.1.0/24 you get:

Private address ranges (for subnetting at home/lab)

When you design subnets on private networks, you normally use RFC1918 ranges:

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 a

Look for lines like:

inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic noprefixroute enp0s3

This tells you:

To show a specific interface:

ip address show dev enp0s3

Adding 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 enp0s3

To remove it:

sudo ip address del 192.168.10.5/24 dev enp0s3

This 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.

As a Linux admin, you mainly:

The routing table

Use ip route to see the kernel’s routing table:

ip route show
# or:
ip r

Typical 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 100

Interpretation:

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:

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 enp0s3

Packets to:

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 enp0s3

This 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 enp0s3

Or delete a default route:

sudo ip route del default via 192.168.1.1 dev enp0s3

Example: Multi-network Linux host

Imagine a Linux machine acting as a small router or firewall:

Configure IPs:

sudo ip address add 192.168.1.1/24 dev enp0s3
sudo ip address add 10.0.0.1/24 dev enp0s8

Routing 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.1

The 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:

  1. Is the IP/subnet correct on the interface?
   ip address show dev enp0s3

Check that the inet line shows the expected address and prefix.

  1. Is there a route to the destination network?
   ip route show

Look for:

  1. Can you reach at least the gateway?
   ping -c 3 192.168.1.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.8

Output example:

Views: 28

Comments

Please login to add a comment.

Don't have an account? Register now!