🇨🇳 中文

IP Addresses and CIDR Explained: A Complete Networking Guide

Learn IPv4 address structure, classful addressing (A/B/C/D/E), subnet masks, and CIDR notation. Includes a subnet cheat sheet and practical Linux commands for network engineers and developers.

Bruce

IPCIDRNetworkingSubnet MaskLinux

Linux

1436  Words

2018-10-06


Every device on the internet needs an IP address to communicate. Whether you are configuring cloud infrastructure, debugging connectivity issues, or setting up container networks, a solid grasp of IP addressing and CIDR is essential. This guide walks through IPv4 address structure, the legacy class system, subnet masks, and modern CIDR notation from the ground up.

Why IP Addressing Matters

ScenarioWhat You Need to Know
Server setupStatic IPs, firewall rules
TroubleshootingConnectivity analysis, fault isolation
Cloud servicesVPC design, security group configuration
Container orchestrationKubernetes networking, Docker network modes

Understanding IP fundamentals is a prerequisite for every other networking topic.

IPv4 Address Basics

Address Structure

An IPv4 address is a 32-bit binary number. For readability, it is written as four decimal numbers separated by dots — a format called dotted decimal notation.

Diagram showing the IPv4 address structure and the relationship between binary and decimal representations

Binary:  11000000.10101000.00000001.00000001
Decimal: 192.168.1.1

Each decimal number is called an octet (8 bits). Its range is 0 to 255 because the maximum value of 8 binary digits is 2^8 - 1 = 255.

Network ID vs. Host ID

Every IP address consists of two logical parts:

PartPurposeUsed For
Network IDIdentifies the network the device belongs toRouting decisions
Host IDIdentifies a specific device within that networkLocal addressing

This split is what makes routing scalable: routers only need to store network-level entries, not an entry for every single host on the internet.

Reserved Addresses

Certain IP addresses serve special purposes and cannot be assigned to regular hosts:

TypeDescriptionExample
Network addressHost bits all set to 0192.168.1.0
Broadcast addressHost bits all set to 1192.168.1.255
Loopback addressUsed for local testing127.0.0.1
Private addressesInternal use only, non-routable10.x.x.x, 192.168.x.x

Classful Addressing (Legacy)

In the early days of the internet, IP addresses were divided into five classes — A through E. Although CIDR replaced this system in 1993, understanding classful addressing helps explain why modern notation exists.

Class A

  • Network ID: First 8 bits (1st octet)
  • Host ID: Remaining 24 bits (octets 2-4)
  • Leading bit: 0
Format: 0NNNNNNN.HHHHHHHH.HHHHHHHH.HHHHHHHH
        └─Net ID─┘└──────── Host ID ────────┘
PropertyValue
Range1.0.0.0 – 126.255.255.255
Number of networks126 (0 and 127 are reserved)
Hosts per network2^24 - 2 = 16,777,214
Default subnet mask255.0.0.0 (/8)

Note: The entire 127.x.x.x block is reserved for loopback (e.g., 127.0.0.1).

Class B

  • Network ID: First 16 bits (octets 1-2)
  • Host ID: Remaining 16 bits (octets 3-4)
  • Leading bits: 10
Format: 10NNNNNN.NNNNNNNN.HHHHHHHH.HHHHHHHH
        └──── Net ID ────┘└── Host ID ──┘
PropertyValue
Range128.0.0.0 – 191.255.255.255
Number of networks2^14 = 16,384
Hosts per network2^16 - 2 = 65,534
Default subnet mask255.255.0.0 (/16)

Class C

  • Network ID: First 24 bits (octets 1-3)
  • Host ID: Remaining 8 bits (4th octet)
  • Leading bits: 110
Format: 110NNNNN.NNNNNNNN.NNNNNNNN.HHHHHHHH
        └────────── Net ID ──────────┘└Host┘
PropertyValue
Range192.0.0.0 – 223.255.255.255
Number of networks2^21 = 2,097,152
Hosts per network2^8 - 2 = 254
Default subnet mask255.255.255.0 (/24)

Class D (Multicast)

  • Leading bits: 1110
  • Range: 224.0.0.0 – 239.255.255.255
  • Purpose: Multicast — one-to-many communication

Class D addresses have no network/host split. They are used to send data to a group of receivers simultaneously.

Class E (Reserved)

  • Leading bits: 1111
  • Range: 240.0.0.0 – 255.255.255.255
  • Purpose: Reserved for experimental use

Note: 255.255.255.255 is the limited broadcast address, used to reach all devices on the local network.

Class Summary

ClassLeading Bits1st Octet RangeDefault MaskUse Case
A01-126/8Large networks
B10128-191/16Medium networks
C110192-223/24Small networks
D1110224-239Multicast
E1111240-255Reserved

Subnet Masks

What Is a Subnet Mask?

A subnet mask is a 32-bit value that tells you which bits of an IP address belong to the network and which belong to the host.

  • 1-bits mark the network portion
  • 0-bits mark the host portion
IP address:  192.168.1.100
             11000000.10101000.00000001.01100100

Subnet mask: 255.255.255.0
             11111111.11111111.11111111.00000000
             └────────── Network ──────────┘└Host┘

Network address: 192.168.1.0 (IP AND mask)

How Subnet Masks Work

Performing a bitwise AND between an IP address and its subnet mask yields the network address:

# Are these two hosts on the same network?
192.168.1.100 AND 255.255.255.0 = 192.168.1.0
192.168.1.200 AND 255.255.255.0 = 192.168.1.0
# Same result — they can communicate directly.

192.168.1.100 AND 255.255.255.0 = 192.168.1.0
192.168.2.100 AND 255.255.255.0 = 192.168.2.0
# Different result — traffic must go through a router.

Calculating Usable Hosts

The formula for usable host addresses in a subnet:

Usable hosts = 2^(host bits) - 2

Why subtract 2? Because two addresses in every subnet are reserved:

  • Network address: all host bits set to 0 (e.g., 192.168.1.0)
  • Broadcast address: all host bits set to 1 (e.g., 192.168.1.255)

CIDR (Classless Inter-Domain Routing)

Why CIDR Was Needed

Classful addressing led to massive address waste:

ScenarioNeedClassful AllocationWasted
300 hosts300 IPsClass C (254) too small, Class B (65,534)65,234
2,000 hosts2,000 IPsClass B (65,534)63,534

In 1993, the IETF published RFC 1518 and RFC 1519, introducing CIDR (Classless Inter-Domain Routing) to solve this problem once and for all.

CIDR Notation

CIDR uses slash notation to express the network prefix length:

Format:  IP_address/prefix_length
Example: 192.168.1.0/24

/24 means the first 24 bits are the network ID and the remaining 8 bits are for hosts — equivalent to the subnet mask 255.255.255.0.

CIDR Calculation Example

Suppose you need a subnet that supports 2,000 hosts:

Required host bits: 2^11 = 2048 > 2000, so 11 host bits
Network bits:       32 - 11 = 21
CIDR notation:      xxx.xxx.xxx.xxx/21
Subnet mask:        11111111.11111111.11100000.00000000 = 255.255.224.0
Usable hosts:       2^11 - 2 = 2,046

Advantages of CIDR

AdvantageDescription
Flexible allocationAssign exactly the number of IPs you need
Route aggregationCombine multiple networks into a single route (supernetting)
Slower exhaustionMore efficient use of the IPv4 address space

Subnetting in Practice

Steps to Subnet a Network

  1. Determine how many subnets you need (or how many hosts per subnet)
  2. Calculate how many host bits to borrow
  3. Derive the new subnet mask
  4. List the address range for each subnet

Example: Split 192.168.1.0/24 into 4 Subnets

Original network: 192.168.1.0/24
Subnets needed:   4
Bits borrowed:    2 (2^2 = 4)
New mask:         /26 (255.255.255.192)

Results:

SubnetNetwork AddressUsable RangeBroadcastUsable Hosts
1192.168.1.0/26.1 – .62.6362
2192.168.1.64/26.65 – .126.12762
3192.168.1.128/26.129 – .190.19162
4192.168.1.192/26.193 – .254.25562

Subnet Cheat Sheet

A quick reference for common CIDR prefixes:

CIDRSubnet MaskUsable HostsTypical Use
/30255.255.255.2522Point-to-point links
/29255.255.255.2486Tiny subnets
/28255.255.255.24014Small offices
/27255.255.255.22430Small departments
/26255.255.255.19262Medium departments
/25255.255.255.128126Large departments
/24255.255.255.0254Standard Class C equivalent
/23255.255.254.0510Medium networks
/22255.255.252.01,022Large networks
/21255.255.248.02,046Large networks
/20255.255.240.04,094Data centers
/16255.255.0.065,534Standard Class B equivalent
/8255.0.0.016,777,214Standard Class A equivalent

Private IP Addresses

RFC 1918 defines three private address ranges for internal networks. These addresses are not routed on the public internet:

RangeCIDRAvailable IPsLegacy Class
10.0.0.0 – 10.255.255.25510.0.0.0/816,777,216A
172.16.0.0 – 172.31.255.255172.16.0.0/121,048,576B
192.168.0.0 – 192.168.255.255192.168.0.0/1665,536C

Home routers and corporate LANs use these private ranges and reach the public internet through NAT (Network Address Translation).

Useful Commands

Viewing IP Configuration on Linux

# Show all network interfaces
ip addr show

# Show the routing table
ip route show

# Show a specific interface
ip addr show eth0

Calculating Network Addresses

# Use the ipcalc utility
ipcalc 192.168.1.100/24

# Sample output:
# Network:   192.168.1.0/24
# Netmask:   255.255.255.0
# Broadcast: 192.168.1.255
# HostMin:   192.168.1.1
# HostMax:   192.168.1.254
# Hosts/Net: 254

Testing Connectivity

# Ping test
ping 192.168.1.1

# Trace the route
traceroute 8.8.8.8

For more Linux networking commands, see Linux/macOS Command Cheat Sheet.

Summary

Here are the key takeaways:

  1. IPv4 addresses are 32-bit numbers split into a network ID and a host ID
  2. Classful addressing divided addresses into five classes (A–E) but wasted large blocks of address space
  3. Subnet masks separate the network portion from the host portion using a bitwise AND operation
  4. CIDR replaced classful addressing with flexible prefix-length notation, enabling right-sized allocations
  5. Subnetting lets you carve a large network into smaller, more manageable segments

These fundamentals underpin everything from cloud VPC design and Kubernetes pod networking to firewall rules and DNS configuration.

Further Reading

References

Comments

Join the discussion — requires a GitHub account