VLSM Step by Step
By the end of this lesson you will be able to design a VLSM address plan by hand, explain why the largest subnet must go first, measure wasted addresses, and use a small Python planner to check your design.
In the last lesson every subnet had the same size. Real networks are not like that. A computer lab may need 100 addresses while a link between two routers needs only 2. Giving both a /25 wastes over a hundred addresses. VLSM fixes this.
What VLSM means
VLSM stands for Variable Length Subnet Masking. It simply means: different subnets of the same network can have different prefix lengths. A big department gets a big subnet; a small link gets a tiny one.
Think of a tailor in Ludhiana with one long roll of cloth. If every customer gets the same 3 metres, short customers waste cloth and tall ones go short. A good tailor measures each customer and cuts the biggest pieces first, so the leftover cloth stays in one usable piece. VLSM is cutting the address roll to measure.
The VLSM method
Follow these steps every time:
- List the requirements (hosts needed in each subnet).
- Sort them from largest to smallest. This is the most important step.
- For each one, find the smallest block that fits: the smallest power of 2 that is at least hosts + 2.
- Allocate blocks one after another, starting at the beginning of the address space. Each block starts where the previous one ended.
- Write down the network address, prefix, first host, last host and broadcast for each.
Why largest first? A block must start at a multiple of its own size. If small blocks go first, they leave awkward gaps that big blocks cannot use. Largest first keeps every block naturally aligned with no gaps.
Worked example
Kabir manages the network for a coaching institute in Kota. He has 192.168.50.0/24 and needs:
| Subnet | Hosts needed |
|---|---|
| Classroom lab | 100 |
| Office | 50 |
| Library | 25 |
| Staff room | 10 |
| Router link A | 2 |
| Router link B | 2 |
Step 1 and 2: the list is already sorted by size.
Step 3: find each block.
- Lab: 100 + 2 = 102. Next power of 2 is 128. Prefix 32 - 7 = /25.
- Office: 52 → 64, /26.
- Library: 27 → 32, /27.
- Staff room: 12 → 16, /28.
- Links: 4 → 4, /30 each.
Step 4: allocate in order.
- Lab:
192.168.50.0/25(0 to 127) - Office:
192.168.50.128/26(128 to 191) - Library:
192.168.50.192/27(192 to 223) - Staff room:
192.168.50.224/28(224 to 239) - Link A:
192.168.50.240/30(240 to 243) - Link B:
192.168.50.244/30(244 to 247)
Addresses 248 to 255 are still free for the future.
A VLSM planner in Python
Let us turn the method into code. Python's ipaddress does the address maths, and we only need to follow the steps.
import ipaddress
def vlsm(base, needs):
base = ipaddress.ip_network(base)
start = int(base.network_address)
plan = []
for name, hosts in sorted(needs.items(), key=lambda x: -x[1]): # step 2
size = 4
while size - 2 < hosts: # step 3
size *= 2
prefix = 32 - size.bit_length() + 1
net = ipaddress.ip_network(f"{ipaddress.ip_address(start)}/{prefix}")
if not net.subnet_of(base):
raise ValueError(f"Not enough space for {name}")
plan.append((name, hosts, net))
start += size # step 4
return plan
needs = {"Staff room": 10, "Classroom lab": 100, "Router link A": 2,
"Office": 50, "Library": 25, "Router link B": 2}
for name, hosts, net in vlsm("192.168.50.0/24", needs):
h = list(net.hosts())
print(f"{name:<14}{hosts:>4} {str(net):<18} {h[0]} - {h[-1]} bc {net.broadcast_address}")Classroom lab 100 192.168.50.0/25 192.168.50.1 - 192.168.50.126 bc 192.168.50.127 Office 50 192.168.50.128/26 192.168.50.129 - 192.168.50.190 bc 192.168.50.191 Library 25 192.168.50.192/27 192.168.50.193 - 192.168.50.222 bc 192.168.50.223 Staff room 10 192.168.50.224/28 192.168.50.225 - 192.168.50.238 bc 192.168.50.239 Router link A 2 192.168.50.240/30 192.168.50.241 - 192.168.50.242 bc 192.168.50.243 Router link B 2 192.168.50.244/30 192.168.50.245 - 192.168.50.246 bc 192.168.50.247
The requirements were given in a jumbled order, but the planner sorted them first, so the result matches our hand plan exactly. The subnet_of check stops the planner if the base network runs out of space.
How much did VLSM save?
Let us compare with equal-size subnetting. To fit the 100-host lab with equal subnets, every subnet must be a /25, and a /24 holds only two of those. We need six subnets, so equal subnetting would not even fit. VLSM fits all six and leaves room to spare.
needs = [100, 50, 25, 10, 2, 2]
def block(h):
size = 4
while size - 2 < h:
size *= 2
return size
vlsm_used = sum(block(h) for h in needs)
fixed_used = len(needs) * block(max(needs))
print("VLSM uses:", vlsm_used, "addresses, free:", 256 - vlsm_used)
print("Equal /25 subnets would need:", fixed_used, "addresses (only 256 exist)")
for h in needs:
print(f" need {h:>3}, block {block(h):>3}, wasted {block(h) - 2 - h}")VLSM uses: 248 addresses, free: 8 Equal /25 subnets would need: 768 addresses (only 256 exist) need 100, block 128, wasted 26 need 50, block 64, wasted 12 need 25, block 32, wasted 5 need 10, block 16, wasted 4 need 2, block 4, wasted 0 need 2, block 4, wasted 0
Some waste remains, because blocks come only in powers of 2. That is normal. Good planners also leave room for growth: if the office may grow to 70 people next year, give it a /25 now.
What goes wrong if you skip sorting
Here is what happens if Kabir allocates the router links first: link A takes .0/30, link B takes .4/30. Now the lab needs a /25, which must start at a multiple of 128. The next one is .128, so addresses 8 to 127 are stranded unless smaller subnets happen to fill them. The plan becomes messy and may not fit. Always sort first.
VLSM and route summarisation
Because all of Kabir's subnets sit inside 192.168.50.0/24, other routers only need one route to reach all six: 192.168.50.0/24. This is called route summarisation. Careful VLSM planning, where each site's subnets come from one larger block, keeps routing tables small. You will see more in Routing & Static Routes.
Common mistakes
- Allocating in the order given. Always sort from largest to smallest first.
- Forgetting the +2. A 30-host need fits a /27 (30 hosts), but a 31-host need requires a /26.
- Starting a block at a non-multiple of its size. A /26 must start at 0, 64, 128 or 192 in the last octet.
- Overlapping subnets. The next block must start after the previous broadcast address.
Key points
- VLSM lets subnets of one network have different prefix lengths.
- Sort requirements from largest to smallest, then allocate blocks back to back.
- Block size = smallest power of 2 that is at least hosts + 2.
- Each block starts at a multiple of its own size, which sorting guarantees.
- Router-to-router links usually get a /30 (or a /31).
- Plans built from one parent block can be summarised as a single route.
Exercise
Pooja has 10.10.8.0/22 for a branch in Kochi with these needs: Sales 300 hosts, Support 120, Accounts 60, Servers 14, two router links of 2 hosts. Plan it by hand, then run the vlsm function to check. How many addresses are left free?
Show solution
import ipaddress
def vlsm(base, needs):
base = ipaddress.ip_network(base)
start = int(base.network_address)
plan = []
for name, hosts in sorted(needs.items(), key=lambda x: -x[1]):
size = 4
while size - 2 < hosts:
size *= 2
prefix = 32 - size.bit_length() + 1
net = ipaddress.ip_network(f"{ipaddress.ip_address(start)}/{prefix}")
plan.append((name, net))
start += size
return plan
needs = {"Sales": 300, "Support": 120, "Accounts": 60, "Servers": 14, "Link 1": 2, "Link 2": 2}
plan = vlsm("10.10.8.0/22", needs)
for name, net in plan:
print(f"{name:<9}{net}")
used = sum(net.num_addresses for _, net in plan)
print("Free addresses:", 1024 - used)Sales 10.10.8.0/23 Support 10.10.10.0/25 Accounts 10.10.10.128/26 Servers 10.10.10.192/28 Link 1 10.10.10.208/30 Link 2 10.10.10.212/30 Free addresses: 296
Sales needs 302 addresses, so it gets a /23 (512). The other blocks follow in order. A /22 has 1,024 addresses and the plan uses 728, leaving 296 free.
Quick Quiz
1. In VLSM, which subnet should be allocated first?
Largest first keeps every block aligned and avoids gaps.
2. A subnet needs 60 hosts. Which prefix is the best fit?
A /26 gives 62 hosts; a /27 gives only 30.
3. After allocating 192.168.1.0/25 and 192.168.1.128/26, where does the next block start?
The /26 ends at .191, so the next block starts at .192.
4. What prefix is usually used for a point-to-point link between two routers?
A /30 has exactly 2 usable addresses, one for each router.
Learn Computer Networks with a mentor
Free lessons get you started. Our Computer Science Fundamentals training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
