DEV Community

Vivesh
Vivesh

Posted on

Subnets and Subnet Masks: A DevOps Guide to Calculating Networks and Hosts

In the world of networking, subnets and subnet masks are foundational concepts that enable efficient use of IP address space. As a DevOps engineer, understanding these concepts is essential for designing scalable, secure, and efficient infrastructures. This article delves into subnets, subnet masks, and how to calculate networks and hosts within a subnet.


What is a Subnet?

A subnet (short for "subnetwork") is a logically segmented portion of a larger network. Subnets allow organizations to divide their network into smaller, manageable sections. This segmentation improves performance, simplifies management, and enhances security.

Why Subnetting is Important

  1. Efficient IP Address Management: Prevents wastage of IP addresses.
  2. Improved Network Performance: Reduces broadcast traffic within subnets.
  3. Enhanced Security: Segments sensitive data and resources.
  4. Easier Troubleshooting: Limits network issues to a specific subnet.

Subnet Masks Explained

A subnet mask is a 32-bit number that distinguishes the network portion of an IP address from the host portion. It works alongside an IP address to determine which part of the address identifies the network and which part identifies the device (host).

Subnet Mask Format

The subnet mask is written in dotted decimal notation, like an IP address. For example:

  • 255.255.255.0 corresponds to /24 in CIDR (Classless Inter-Domain Routing) notation.
  • 255.255.254.0 corresponds to /23 in CIDR notation.

How to Calculate Subnet Networks and Hosts

Step 1: Understand the IP Address Structure

An IPv4 address consists of 32 bits, divided into 4 octets (8 bits each). For example:

192.168.1.0
Enter fullscreen mode Exit fullscreen mode

Each octet can range from 0 to 255 (8 bits = 2⁸ = 256 possible values).

Step 2: Determine the Subnet Mask

The subnet mask specifies how many bits are allocated for the network portion. For example:

  • /24 means the first 24 bits are for the network, leaving 8 bits for hosts.
  • /26 means the first 26 bits are for the network, leaving 6 bits for hosts.

Step 3: Calculate the Number of Subnets

The formula to calculate the number of subnets is:

Number of Subnets = 2^n
Enter fullscreen mode Exit fullscreen mode

Where n is the number of bits borrowed for subnetting (from the host portion).

For example:

  • A /24 network (default mask: 255.255.255.0) subnetted to /26 means borrowing 2 bits:
  2^2 = 4 subnets
Enter fullscreen mode Exit fullscreen mode

Step 4: Calculate the Number of Hosts per Subnet

The formula to calculate the number of usable hosts per subnet is:

Number of Hosts = (2^h) - 2
Enter fullscreen mode Exit fullscreen mode

Where h is the number of bits remaining for the host portion, and the -2 accounts for the network and broadcast addresses.

For example:

  • A /26 network leaves 6 bits for hosts:
  (2^6) - 2 = 64 - 2 = 62 usable hosts per subnet
Enter fullscreen mode Exit fullscreen mode

Step 5: Define Subnet Ranges

For a /26 network starting at 192.168.1.0:

  • Subnet 1: 192.168.1.0 - 192.168.1.63 (Network: .0, Broadcast: .63)
  • Subnet 2: 192.168.1.64 - 192.168.1.127 (Network: .64, Broadcast: .127)
  • Subnet 3: 192.168.1.128 - 192.168.1.191 (Network: .128, Broadcast: .191)
  • Subnet 4: 192.168.1.192 - 192.168.1.255 (Network: .192, Broadcast: .255)

Example Calculation

Problem Statement

You are given a 192.168.10.0/24 network. Subdivide it into subnets that can each support up to 30 hosts.

Solution

  1. Calculate the required subnet mask:

    • To support 30 hosts, we need at least 5 bits for the host portion (2⁵ = 32, and 32 - 2 = 30 usable hosts).
    • This leaves 27 bits for the network portion (32 - 5 = 27).
    • Subnet mask: /27 or 255.255.255.224.
  2. Calculate the number of subnets:

    • Borrowing 3 bits (from /24 to /27) creates:
     2³ = 8 subnets
    
  3. Subnet Ranges:

    • Subnet 1: 192.168.10.0 - 192.168.10.31
    • Subnet 2: 192.168.10.32 - 192.168.10.63
    • Subnet 3: 192.168.10.64 - 192.168.10.95, and so on.

Tools for Subnetting

For complex networks, manual calculations can be tedious. Here are tools to simplify the process:

  1. Online Subnet Calculators – Input your IP address and desired subnet mask.
  2. CLI Commands – Use tools like ipcalc or subnetcalc.
  3. Cloud Networking Dashboards – Platforms like AWS, Azure, and GCP provide built-in subnet management features.

_Happy Learning !!! _

Top comments (0)