Stackvora.tech

IPv4 Subnetting Cheat Sheet

A comprehensive IPv4 subnetting reference covering CIDR, subnet mask calculations, network/broadcast addresses, and practical examples for network engineers and sysadmins.

Effective IP address management is a foundational skill for network engineers and system administrators. As IPv4 address space becomes increasingly constrained, efficient subnetting is crucial for optimizing network design, enhancing security through segmentation, and reducing broadcast traffic. This guide provides a detailed reference for understanding and applying IPv4 subnetting principles, building upon the essential CIDR notations and their corresponding subnet masks.

While tools exist for automatic calculation, a solid grasp of manual subnetting is invaluable for troubleshooting, advanced network design, and certification exams. We'll cover the binary underpinnings, key calculations, and practical implications of various subnet sizes.

Understanding IPv4 Addresses and CIDR

An IPv4 address is a 32-bit number, typically represented in dotted-decimal notation (e.g., 192.168.1.10). This 32-bit address is logically divided into two parts: the network portion and the host portion. The Classless Inter-Domain Routing (CIDR) notation, introduced in RFC 1519 and updated in RFC 4632, simplifies the representation of this division. Instead of relying on traditional A/B/C classes, CIDR uses a suffix (e.g., /24) to indicate the number of bits in the network portion.

For instance, 192.168.1.0/24 means the first 24 bits define the network, and the remaining 8 bits define host addresses within that network. The network portion identifies the specific network, while the host portion uniquely identifies a device within that network.

The Subnet Mask

The subnet mask is another way to express the division between the network and host portions. It's a 32-bit number where all network bits are set to 1 and all host bits are set to 0. When an IP address is logically ANDed with its subnet mask, the result is the network address.

Calculating the Subnet Mask from CIDR

To derive the subnet mask from a CIDR prefix /N, set the first N bits to 1 and the remaining (32-N) bits to 0. Convert this binary representation to dotted-decimal.

Example for /24:

  • Network bits: 24 (all 1s)
  • Host bits: 8 (all 0s)
  • Binary: 11111111.11111111.11111111.00000000
  • Dotted-decimal: 255.255.255.0

Example for /27:

  • Network bits: 27 (first 27 are 1s)
  • Host bits: 5 (remaining 5 are 0s)
  • Binary: 11111111.11111111.11111111.11100000
  • Dotted-decimal: 255.255.255.224 (11100000 in binary is 128+64+32 = 224)

Network Address and Broadcast Address

Within any subnet, two addresses are reserved and cannot be assigned to hosts:

  • Network Address: The first address in the range, where all host bits are 0. This identifies the network itself.
  • Broadcast Address: The last address in the range, where all host bits are 1. Packets sent to this address are delivered to all devices on that specific subnet.

The number of usable host addresses is calculated by 2^(32-prefix) - 2. The -2 accounts for the reserved network and broadcast addresses.

Common CIDR Prefixes and Their Details

Here's an expansion of common subnet sizes, showing their CIDR, subnet mask, and usable host count:

CIDR Prefix Subnet Mask Total IP Addresses Usable Host Addresses Example Network Address Example Broadcast Address
/24 255.255.255.0 256 254 192.168.1.0 192.168.1.255
/25 255.255.255.128 128 126 192.168.1.0 or 192.168.1.128 192.168.1.127 or 192.168.1.255
/26 255.255.255.192 64 62 192.168.1.0, .64, .128, .192 192.168.1.63, .127, .191, .255
/27 255.255.255.224 32 30 192.168.1.0, .32, .64, ... 192.168.1.31, .63, .95, ...
/28 255.255.255.240 16 14 192.168.1.0, .16, .32, ... 192.168.1.15, .31, .47, ...
/29 255.255.255.248 8 6 192.168.1.0, .8, .16, ... 192.168.1.7, .15, .23, ...
/30 255.255.255.252 4 2 192.168.1.0, .4, .8, ... 192.168.1.3, .7, .11, ...
/31 255.255.255.254 2 2 192.168.1.0, .2, .4, ... 192.168.1.1, .3, .5, ...
/32 255.255.255.255 1 1 192.168.1.10 192.168.1.10

Note on /31 and /32:

  • A /31 network has only two IP addresses. While traditionally it would yield 0 usable hosts (2 - 2 = 0), RFC 3021 defines /31 as usable for point-to-point links, where the network address becomes one endpoint and the broadcast address becomes the other.
  • A /32 network refers to a single host address (e.g., 192.168.1.10/32). It's commonly used for loopback interfaces, or when advertising a host route.

Practical Subnetting: An Example

Suppose you are given the network 10.0.0.0/8 and need to create subnets for 5 departments, each requiring approximately 50-60 usable host IPs.

  1. Determine required host bits: We need to support up to 60 hosts. We look for the smallest 2^H value greater than 60 + 2 (for network and broadcast).
    2^5 = 32 (too small) 2^6 = 64 (sufficient) So, we need 6 host bits.
  2. Calculate new prefix length: Total bits (32) - host bits (6) = 26 network bits. The new prefix is /26.
  3. Calculate subnet mask: A /26 prefix has a subnet mask of 255.255.255.192.
  4. Identify subnet addresses and ranges: The increment (or block size) for the last octet is 2^(32-26) = 2^6 = 64.

The subnets would be:

  • Subnet 1:
    • Network Address: 10.0.0.0/26
    • Usable Hosts: 10.0.0.1 - 10.0.0.62
    • Broadcast Address: 10.0.0.63
  • Subnet 2:
    • Network Address: 10.0.0.64/26
    • Usable Hosts: 10.0.0.65 - 10.0.0.126
    • Broadcast Address: 10.0.0.127
  • Subnet 3:
    • Network Address: 10.0.0.128/26
    • Usable Hosts: 10.0.0.129 - 10.0.0.190
    • Broadcast Address: 10.0.0.191
  • ...and so on.

Using ipcalc and other tools

While manual calculation is fundamental, tools can quickly verify your work or perform complex calculations. On Linux, ipcalc is a common utility:


$ ipcalc 192.168.10.130/27
Address:   192.168.10.130
Netmask:   255.255.255.224
Wildcard:  0.0.0.0.31
=
Network:   192.168.10.128/27
HostMin:   192.168.10.129
HostMax:   192.168.10.158
Broadcast: 192.168.10.159
Hosts/Net: 30

This output directly confirms the network, host range, and broadcast address for the given IP and prefix. Other command-line tools like net-tools (ifconfig for mask) or modern iproute2 (ip addr show) provide network information directly from interfaces, though not full subnet calculations.

Common Pitfalls

  • Forgetting Reserved Addresses: Always subtract 2 from the total addresses to get usable hosts (except for /31 and /32).
  • Incorrect Octet Focus: When subnetting, identify the 'interesting' octet (the one where the network and host bits split). All preceding octets will be full 255s, and all following octets will be full 0s in the subnet mask.
  • Binary Conversion Errors: A single bit error in binary-to-decimal conversion (or vice-versa) will lead to an incorrect subnet mask, network address, or broadcast address. Practice converting 8-bit sequences (e.g., 128, 192, 224, 240, 248, 252, 254, 255) until they are second nature.
  • Overlapping Subnets: Incorrectly calculating subnet ranges can lead to IP address conflicts or routing issues if subnets are accidentally configured to overlap.
  • Misunderstanding Host Requirements: Always factor in growth when allocating subnets. If a department currently needs 25 hosts but expects to double in a year, a /27 (30 usable hosts) might be too small, making a /26 (62 usable hosts) a better choice.

Back to the knowledge base · Ask the AI assistant