Class 43 of 55 • Linux Networking & Servers

Linux IP Addressing, Subnetting (CIDR), Netplan & nmcli Tutorial

A practical masterclass covering IPv4 architecture, Classful vs CIDR subnetting calculations, RFC 1918 private scopes, IPv6 essentials, and persistent static IP configuration on Ubuntu and RHEL.

An Internet Protocol (IP) address is a logical numerical label assigned to every device connected to a computer network that uses the Internet Protocol for communication. While physical MAC addresses identify hardware at the local Data Link layer (Layer 2), IP addresses operate at the Network layer (Layer 3) to enable routing across interconnected LANs, WANs, and the worldwide Internet.

In production Linux environments, system administrators must understand binary subnetting to design network topology, calculate available host capacities, prevent address conflicts, and configure persistent network adapters across different Linux distributions like Ubuntu, Debian, Rocky Linux, and Red Hat Enterprise Linux.

1. The IPv4 Address Architecture

An IPv4 address consists of 32 binary bits structured into four 8-bit groupings known as octets. In human-readable form, it is written in dotted-decimal notation:

Dotted Decimal: 192 . 168 . 1 . 100
Binary Octets:   11000000 . 10101000 . 00000001 . 01100100
Bit Length:     [8 bits] . [8 bits] . [8 bits] . [8 bits] = 32 bits total ($2^{32} \approx 4.29\text{ billion addresses}$)

Every IP address is divided into two distinct logical portions:

  • Network ID: Identifies the specific logical network or subnet the host belongs to (analogous to the street name).
  • Host ID: Identifies the unique individual machine or network interface on that network (analogous to the house number).

2. Legacy Classful Addressing vs Modern CIDR

In the original 1981 Internet specification (RFC 791), IPv4 addresses were divided into five rigid classes based on the value of their first octet:

Class Leading Bits 1st Octet Range Default Subnet Mask CIDR Prefix Max Networks Usable Hosts Per Network
Class A 0 1 – 126 255.0.0.0 /8 126 16,777,214
Class B 10 128 – 191 255.255.0.0 /16 16,384 65,534
Class C 110 192 – 223 255.255.255.0 /24 2,097,152 254
Class D 1110 224 – 239 N/A (Multicast) N/A N/A Reserved for Multicast Groups
Class E 1111 240 – 255 N/A (Experimental) N/A N/A Reserved for Research & Future Use

What happened to 127.0.0.0/8? The entire 127.0.0.0 to 127.255.255.255 range is reserved for the local loopback interface (lo). Any packet sent to 127.0.0.1 (localhost) is intercepted internally by the Linux kernel without hitting the physical network wire.

Classless Inter-Domain Routing (CIDR)

Classful boundaries caused massive address waste (e.g., an enterprise needing 300 IP addresses was forced to take a Class B block with 65,534 IPs, wasting over 65,000 addresses). In 1993, CIDR (RFC 1519) replaced classful boundaries with variable-length prefix notation (e.g. /24, /27, /29), allowing organizations to allocate exact subnet sizes matching their actual host needs.

3. Subnetting Mathematics & CIDR Cheat Sheet

The subnet mask tells the Linux kernel which bits belong to the network and which bits identify the host. For any prefix length $P$, the number of usable host addresses is calculated using the formula:

Usable Hosts = \(2^{(32 - P)} - 2\)

We always subtract 2 because the first address is the Network ID and the last address is the Directed Broadcast Address.

CIDR Prefix Subnet Mask Total Addresses Usable Hosts Common Production Use Case
/30 255.255.255.252 4 2 Point-to-Point router links (WAN interconnects)
/29 255.255.255.248 8 6 Small public IP blocks (ISP gateways, HA firewalls)
/28 255.255.255.240 16 14 DMZ server subnet, staging database cluster
/27 255.255.255.224 32 30 Departmental subnet, small office branch
/26 255.255.255.192 64 62 Medium cloud VPC subnet (AWS/GCP public tier)
/25 255.255.255.128 128 126 Half Class C subnet
/24 255.255.255.0 256 254 Standard corporate LAN, home Wi-Fi network
/23 255.255.254.0 512 510 Supernet combining two /24 blocks
/16 255.255.0.0 65,536 65,534 Large corporate campus, cloud VPC network
/8 255.0.0.0 16,777,216 16,777,214 Enterprise global private backbone (10.0.0.0/8)

Step-by-Step Subnetting Example: 192.168.10.0/26

  • Prefix: /26 → $32 - 26 = 6$ host bits.
  • Total Addresses: $2^6 = 64$ addresses per subnet.
  • Usable Hosts: $64 - 2 = 62$ hosts.
  • Subnet Mask: 255.255.255.192 (since $128 + 64 = 192$).
  • Subnet Range:
    • Network ID: 192.168.10.0
    • First Usable Host: 192.168.10.1 (usually Default Gateway)
    • Last Usable Host: 192.168.10.62
    • Broadcast Address: 192.168.10.63

4. Public vs Private IPv4 Addresses (RFC 1918)

Because the 4.3 billion IPv4 address pool was quickly exhausted, the Internet Engineering Task Force (IETF) created RFC 1918, designating three private address blocks that are never routed on the public Internet:

  • 10.0.0.0/8: Range 10.0.0.0 to 10.255.255.255 (1 Class A network, 16,777,214 hosts). Used heavily in large data centers and AWS/Azure/GCP cloud VPCs.
  • 172.16.0.0/12: Range 172.16.0.0 to 172.31.255.255 (16 contiguous Class B networks, 1,048,574 hosts). Used by Docker bridge networks (default 172.17.0.0/16).
  • 192.168.0.0/16: Range 192.168.0.0 to 192.168.255.255 (256 contiguous Class C networks, 65,534 hosts). The standard default in home routers and small offices.

Private IP hosts communicate with the public Internet through Network Address Translation (NAT / Masquerading) performed by Linux routers via iptables or nftables.

5. Introduction to IPv6 in Linux

To eliminate address exhaustion permanently, IPv6 provides 128 bits of address space ($2^{128} \approx 3.4 \times 10^{38}$ unique addresses). Written in 8 groups of four hexadecimal digits separated by colons:

2001:0db8:85a3:0000:0000:8a2e:0370:7334
Compressed Notation (RFC 5952): 2001:db8:85a3::8a2e:370:7334
  • Link-Local Scope: Always starts with fe80::/10 (automatically generated for internal communication on a single switch/link).
  • Loopback: ::1 (equivalent to IPv4 127.0.0.1).
  • Global Unicast: Starts with 2000::/3 (globally routable public addresses).

6. Configuring Static IP Addresses on Modern Linux

Method A: Ubuntu 20.04/22.04/24.04 & Debian (Netplan)

Ubuntu utilizes Netplan, which reads declarative YAML configuration files stored in /etc/netplan/:

# 1. Locate the netplan configuration file
ls /etc/netplan/
# Usually: 01-netcfg.yaml or 50-cloud-init.yaml

# 2. Edit the netplan configuration with sudo
sudo nano /etc/netplan/01-netcfg.yaml

Configure the static IP with exact indentation (Netplan strictly forbids tab characters):

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.1.150/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 1.1.1.1
# 3. Test and apply netplan configuration safely
sudo netplan try
# Or apply directly:
sudo netplan apply

Method B: RHEL, Rocky Linux, AlmaLinux & Fedora (NetworkManager / nmcli)

Enterprise Red Hat families manage persistent network configurations via NetworkManager:

# 1. Identify active connection name
nmcli con show

# 2. Modify connection eth0 to static IP, gateway, and DNS
sudo nmcli con mod "eth0" ipv4.addresses 192.168.1.150/24
sudo nmcli con mod "eth0" ipv4.gateway 192.168.1.1
sudo nmcli con mod "eth0" ipv4.dns "8.8.8.8 1.1.1.1"
sudo nmcli con mod "eth0" ipv4.method manual

# 3. Reload and reactivate the connection profile
sudo nmcli con up "eth0"

# 4. Alternatively, use the interactive terminal UI
sudo nmtui

7. Practical Terminal Lab: Subnetting & IP Verification

Lab 7.1: Calculating Subnet Boundaries with ipcalc

The ipcalc utility eliminates calculation errors when planning subnets:

# Install ipcalc on Debian/Ubuntu or RHEL
sudo apt install ipcalc -y   # Debian/Ubuntu
sudo dnf install ipcalc -y   # RHEL/Rocky

# Calculate complete subnet details for 172.16.50.0/27
ipcalc 172.16.50.0/27

Example Output:

Address:   172.16.50.0          10101100.00010000.00110010.000 00000
Netmask:   255.255.255.224 = 27 11111111.11111111.11111111.111 00000
Wildcard:  0.0.0.31             00000000.00000000.00000000.000 11111
=>
Network:   172.16.50.0/27       10101100.00010000.00110010.000 00000
HostMin:   172.16.50.1          10101100.00010000.00110010.000 00001
HostMax:   172.16.50.30         10101100.00010000.00110010.000 11110
Broadcast: 172.16.50.31         10101100.00010000.00110010.000 11111
Hosts/Net: 30                   (Class B)

Lab 7.2: Adding a Temporary Secondary IP

In Linux, a single physical interface can host multiple IP addresses simultaneously:

# Add a secondary virtual IP to eth0
sudo ip addr add 192.168.1.200/24 dev eth0

# Verify both IP addresses are active
ip -br a show dev eth0

# Remove the secondary IP
sudo ip addr del 192.168.1.200/24 dev eth0

8. Student Viva & Exam Practice Q&A

A /25 prefix has 7 host bits ($2^7 = 128$ total addresses). The subnet starts at Network ID 192.168.1.128 and spans 128 addresses (128 to 255). Therefore, the Broadcast Address is 192.168.1.255, and the usable host range is 192.168.1.129 through 192.168.1.254.
A static IP is manually hardcoded into system configuration files (e.g. Netplan or NetworkManager) and never changes between reboots, which is mandatory for production web, database, and DNS servers. A dynamic IP is leased temporarily from a local DHCP server via client daemons like dhclient or systemd-networkd; the lease must be renewed periodically and may change.
If a Linux host is configured for DHCP but fails to receive an answer from any DHCP server, the system automatically assigns itself an address in the 169.254.0.0/16 range (Automatic Private IP Addressing). This allows local ad-hoc communication between devices on the same switch without internet connectivity.

9. Frequently Asked Questions (FAQ)

Commands from the iproute2 suite (such as ip addr add or ip route add) directly modify volatile kernel routing and interface tables in RAM. Upon reboot, the kernel reinitializes network state from persistent configuration files on disk (such as /etc/netplan/*.yaml in Ubuntu or NetworkManager keyfiles in /etc/NetworkManager/system-connections/ on RHEL).
A router is a physical or virtual Layer 3 device that interconnects different subnets. A default gateway is the specific IP address of the local router interface assigned to a Linux host where all packets destined for non-local external networks (e.g., the Internet) are forwarded.
A /28 subnet leaves $32 - 28 = 4$ host bits. $2^4 = 16$ total addresses. Subtracting 2 for Network ID and Broadcast yields exactly 14 usable host IP addresses.
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!