Skip to content
KALANBLON
Show contents

Chapter 2 of 7 · 20 min

Splitting into subnets

By the end of this chapter, you'll be able to

  • Compute the number of addresses and usable hosts of a prefix
  • Find the network address, broadcast address and host range of a subnet
  • Split a network into subnets of equal size
  • Choose the smallest prefix suited to a number of machines

How many addresses in a prefix?

With a /n prefix, 32 - n bits remain for the host part, so 2 to the power of (32 - n) addresses. Two of them are reserved:

  • the network address (all host bits at 0), which designates the network itself;
  • the broadcast address (all host bits at 1), which sends a message to all machines in the network.

That leaves 2^(32 - n) - 2 usable hosts.

for prefix in [24, 25, 26, 27, 28, 30]:
    addresses = 2 ** (32 - prefix)
    print(f"/{prefix}: {addresses:3} addresses, {addresses - 2:3} hosts")
/24: 256 addresses, 254 hosts
/25: 128 addresses, 126 hosts
/26:  64 addresses,  62 hosts
/27:  32 addresses,  30 hosts
/28:  16 addresses,  14 hosts
/30:   4 addresses,   2 hosts

Each bit added to the prefix halves the size of the network. A /30 (2 hosts) is typically used to link two routers together.

A subnet's card

import ipaddress

network = ipaddress.ip_network("192.168.10.64/26")
hosts = list(network.hosts())
print("network  :", network.network_address)
print("broadcast:", network.broadcast_address)
print("hosts    :", hosts[0], "to", hosts[-1], f"({len(hosts)})")
print("mask     :", network.netmask)
network  : 192.168.10.64
broadcast: 192.168.10.127
hosts    : 192.168.10.65 to 192.168.10.126 (62)
mask     : 255.255.255.192

A /26 has 64 addresses: from .64 (the network) to .127 (the broadcast), with 62 hosts in between.

Splitting a network

Splitting a /24 into /26 subnets means borrowing 2 bits from the host part. 2 bits give 4 combinations, so 4 subnets of 64 addresses each:

The fourth starts at .192 and ends at .255: the whole /24 is used, without overlap.

Step 5 of 5
Text description of the animation
  1. At first, a single large network of 256 addresses, from .0 to .255.
  2. We borrow 2 host bits: the first subnet starts at .0 and holds 64 addresses.
  3. The second starts 64 addresses further, at .64.
  4. The third starts at .128.
  5. The fourth starts at .192 and ends at .255: the whole /24 is used, without overlap.
import ipaddress

school = ipaddress.ip_network("192.168.10.0/24")
for subnet in school.subnets(new_prefix=26):
    print(subnet, "→ broadcast", subnet.broadcast_address)
192.168.10.0/26 → broadcast 192.168.10.63
192.168.10.64/26 → broadcast 192.168.10.127
192.168.10.128/26 → broadcast 192.168.10.191
192.168.10.192/26 → broadcast 192.168.10.255

Choosing the right size

For a group of m machines, look for the largest prefix (the smallest network) such that 2^(32 - n) - 2 ≥ m. Think about growth too: a network that is just big enough today will be full next year.

def prefix_for(machines):
    for prefix in range(30, 0, -1):
        if 2 ** (32 - prefix) - 2 >= machines:
            return prefix

for need in [10, 30, 31, 100, 200]:
    print(need, "machines → /" + str(prefix_for(need)))
10 machines → /28
30 machines → /27
31 machines → /26
100 machines → /25
200 machines → /24

31 machines do not fit in a /27 (30 hosts): a /26 is needed.

Mini-lab: your own splitPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: every subnet's card

The network 172.16.8.0/24 must be split into 8 subnets of equal size. Which prefix should you use? Write a program that prints, for each one, its address, its first and its last host address.

Hint 1
8 subnets = 2 to the power of 3: you must borrow 3 bits, so go from /24 to /27.
Hint 2
list(subnet.hosts()) gives the hosts; take the first and the last element.
Show the solution and common mistakes
import ipaddress

for subnet in ipaddress.ip_network("172.16.8.0/24").subnets(new_prefix=27):
    hosts = list(subnet.hosts())
    print(f"{str(subnet):17} {hosts[0]} – {hosts[-1]}")
172.16.8.0/27     172.16.8.1 – 172.16.8.30
172.16.8.32/27    172.16.8.33 – 172.16.8.62
172.16.8.64/27    172.16.8.65 – 172.16.8.94
172.16.8.96/27    172.16.8.97 – 172.16.8.126
172.16.8.128/27   172.16.8.129 – 172.16.8.158
172.16.8.160/27   172.16.8.161 – 172.16.8.190
172.16.8.192/27   172.16.8.193 – 172.16.8.222
172.16.8.224/27   172.16.8.225 – 172.16.8.254

Each /27 subnet has 32 addresses, so 30 hosts, and starts 32 addresses after the previous one.

Common mistakes:

  • Choosing /26 for 8 subnets: 2 borrowed bits only give 4 subnets.
  • Giving .31 or .32 as the last host of the first subnet: .31 is the broadcast, and .32 already belongs to the next subnet.

Summary

  1. 1A /n prefix holds 2^(32 - n) addresses, of which 2^(32 - n) - 2 are usable hosts.
  2. 2The network address (host bits at 0) and the broadcast (host bits at 1) are not given to machines.
  3. 3Borrowing k bits from the host part creates 2^k subnets of equal size.
  4. 4Choose the smallest network that holds all the machines, with a margin for growth.
  5. 5Python's ipaddress module computes networks, broadcasts and host ranges.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.