SkillPilotAI

Subnetting in practice

Subnetting splits one big network (here 192.168.30.0/24) into smaller subnets by borrowing host bits. It cuts broadcast traffic, separates departments for security, and the magic number method lets you find any subnet in seconds.

10.1 Why is it used?

  • One /24 for the whole office (HR, Sales, Developers, Guests): a guest phone's broadcast reaches every device, so a bigger network means more extra traffic.
  • Security: the guest phone can reach the HR PC directly, where the salary data lives.
  • Subnetting gives each department its own subnet with a router in between.
  • HR -> Sales traffic now passes the router, where rules can be applied; broadcasts stop at the router.
A subnet for each department
  1. 1.HR 192.168.30.10/26 sends to Sales 192.168.30.70/26

  2. 2.Different subnet: the packet goes to the office router

  3. 3.Router checks its rules and forwards to Sales

  4. 4.A guest broadcast from 192.168.30.200/26 reaches the router

  5. 5.Router does not forward broadcasts: HR, Sales and Developers never see it

10.2 /24 -> /26 step by step

Output
Network: 192.168.30.0/24Last octet: 00000000  (8 host bits) Step 1: 4 subnets neededStep 2: 2^2 = 4, so borrow 2 bits        /24 + 2 = /26Step 3: host bits 6, 2^6 = 64        usable 64 - 2 = 62Step 4: last octet 11000000        128 + 64 = 192Mask: 255.255.255.192
  • Borrow n bits = 2^n subnets.
  • Host bits left h = 2^h addresses, 2^h - 2 usable.

10.3 The four /26 subnets

#NetworkUsableBroadcast
1.0.1 - .62.63
2.64.65 - .126.127
3.128.129 - .190.191
4.192.193 - .254.255
  • Each subnet has 64 addresses, so networks start at 0, 64, 128, 192.
  • Give HR subnet 1, Sales subnet 2, and so on.

10.4 The magic number method

  • Magic number = 256 - the mask octet that is not 255.
  • /26: 256 - 192 = 64. /27: 256 - 224 = 32.
  • Subnets always start at multiples of the magic number.
Output
Magic = 256 - last mask octet Prefix  Mask  Magic  Subnets  Hosts/25     128   128    2        126/26     192   64     4        62/27     224   32     8        30/28     240   16     16       14

10.5 /24 -> /27: eight subnets

  • Borrow 3 bits: 2^3 = 8 subnets; 5 host bits: 30 usable each.
  • Magic 32: start at 0 and keep adding 32 up to .224; the address before the next network is the broadcast.
NetworkUsableBroadcast
.0.1 - .30.31
.32.33 - .62.63
.64.65 - .94.95
.96.97 - .126.127
.128.129 - .158.159
.160.161 - .190.191
.192.193 - .222.223
.224.225 - .254.255

10.6 Which subnet is this IP in?

Output
IP: 192.168.30.77/271. magic = 256 - 224 = 322. multiples: 0, 32, 64, 96 ...3. 64 <= 77 < 964. Network:   192.168.30.645. Broadcast: 192.168.30.956. Usable:    .65 - .94
Magic number method in 5 steps
  1. 1.Find the interesting mask octet: /27 -> 224

  2. 2.Magic = 256 - 224 = 32

  3. 3.List multiples: 0, 32, 64, 96 ... and find where 77 falls (64 <= 77 < 96)

  4. 4.Network = 64, broadcast = next network - 1 = 95

  5. 5.Usable = .65 to .94

10.7 Practical: splitting with ipcalc and sipcalc

Alpine container in Docker with ipcalc and sipcalc installed.

Terminal
ipcalc -S 26 192.168.30.0/24sipcalc -s 27 192.168.30.0/24
Output
$ ipcalc -S 26 192.168.30.0/24[Split networks]Network:  192.168.30.0/26Network:  192.168.30.64/26Network:  192.168.30.128/26Network:  192.168.30.192/26Total:  4Hosts/Net:  62$ sipcalc -s 27 192.168.30.0/24[Split network]Network - 192.168.30.0    - 192.168.30.31Network - 192.168.30.32   - 192.168.30.63Network - 192.168.30.64   - 192.168.30.95Network - 192.168.30.96   - 192.168.30.127Network - 192.168.30.128  - 192.168.30.159Network - 192.168.30.160  - 192.168.30.191Network - 192.168.30.192  - 192.168.30.223Network - 192.168.30.224  - 192.168.30.255
  • ipcalc -S 26 splits the /24 into the same four /26 networks, 62 hosts each.
  • sipcalc -s 27 shows the full range of all eight /27 subnets.

10.8 Practical: checking answers with ipcalc

Terminal
ipcalc 192.168.30.77/27ipcalc 192.168.30.150/26
Output
$ ipcalc 192.168.30.77/27Address:    192.168.30.77Network:    192.168.30.64/27Netmask:    255.255.255.224 = 27Broadcast:  192.168.30.95HostMin:    192.168.30.65HostMax:    192.168.30.94Hosts/Net:  30$ ipcalc 192.168.30.150/26Address:    192.168.30.150Network:    192.168.30.128/26Netmask:    255.255.255.192 = 26Broadcast:  192.168.30.191HostMin:    192.168.30.129HostMax:    192.168.30.190Hosts/Net:  62
  • 192.168.30.77/27: network .64, broadcast .95, HostMin .65 to HostMax .94: same as the magic number answer.
  • 192.168.30.150/26: network .128, broadcast .191 (magic 64).

10.9 Practical: one switch, two subnets

netshoot container: Linux bridge br0 as the switch; h1 192.168.30.10/26 and h2 .20/26 in subnet 1, h3 .70/26 in subnet 2; r1 is the router (.1 and .65).

Terminal
ip netns exec h1 ip routeip netns exec h1 ping -c 2 192.168.30.20ip netns exec h1 ping -c 2 192.168.30.70
Output
$ ip netns exec h1 ip route192.168.30.0/26 dev eth0 proto kernel  scope link src 192.168.30.10$ ip netns exec h1 ping -c 2 192.168.30.2064 bytes from 192.168.30.20: icmp_seq=1 ttl=64 time=5.91 ms64 bytes from 192.168.30.20: icmp_seq=2 ttl=64 time=0.077 ms2 packets transmitted, 2 received, 0% packet loss$ ip netns exec h1 ping -c 2 192.168.30.70ping: connect: Network unreachable
  • h1's routing table has only its own 192.168.30.0/26 route, no gateway.
  • h2 replies with ttl=64: no router in between.
  • h3 is on the same switch but .70 is outside h1's subnet: Network unreachable (h1 does not even send it).

10.10 Practical: connecting two subnets with a router

Terminal
ip netns exec r1 ip -4 -br addrip netns exec h1 ip routeip netns exec h1 ping -c 2 192.168.30.70ip netns exec h1 traceroute -n 192.168.30.70
Output
$ ip netns exec r1 ip -4 -br addrlo        UNKNOWN  127.0.0.1/8eth0@if6  UP       192.168.30.1/26eth1@if7  UP       192.168.30.65/26$ ip netns exec h1 ip routedefault via 192.168.30.1 dev eth0192.168.30.0/26 dev eth0 proto kernel  scope link src 192.168.30.10$ ip netns exec h1 ping -c 2 192.168.30.7064 bytes from 192.168.30.70: icmp_seq=1 ttl=63 time=4.20 ms64 bytes from 192.168.30.70: icmp_seq=2 ttl=63 time=0.074 ms2 packets transmitted, 2 received, 0% packet loss$ ip netns exec h1 traceroute -n 192.168.30.70 1  192.168.30.1  0.011 ms  0.006 ms  0.007 ms 2  192.168.30.70  0.006 ms  0.006 ms  0.008 ms
  • r1 has one address per subnet: 192.168.30.1/26 and 192.168.30.65/26.
  • h1 now has default via 192.168.30.1.
  • Ping succeeds with ttl=63: one router in between decreases TTL by 1.
  • traceroute: hop 1 is 192.168.30.1, hop 2 is h3 itself.
h1 to h3 through r1
  1. 1.h1 192.168.30.10: .70 is another subnet, use the default gateway

  2. 2.Frame goes through the switch to r1 (192.168.30.1)

  3. 3.r1 routes it out of 192.168.30.65

  4. 4.Frame goes through the same switch to h3 192.168.30.70

  5. 5.Reply arrives with ttl=63

10.11 Common mistakes

  • Myth: subnetting gives more IPs. Every subnet loses its network and broadcast addresses: one /24 = 254 usable, four /26 = 4 x 62 = 248.
  • The real benefit is organisation, security and fewer broadcasts, not more addresses.
  • Assuming the same switch means the same network: different subnets need a router.
  • Picking a prefix by subnets only and forgetting hosts (or the other way round).
NeedToo smallRight answer
5 departments2 bits = 4 subnets3 bits = 8 subnets
25 hosts each/28 = 14 hosts/27 = 30 hosts
Result-/27: 8 subnets x 30 hosts

10.12 Interview tips

  • 5 departments x 25 hosts from a /24: borrow 3 bits -> /27 (8 subnets, 30 hosts each); /28 has only 14 hosts.
  • 192.168.30.200/27: magic 32, network .192, broadcast .223.
  • Borrow n bits = 2^n subnets; usable hosts = 2^h - 2.
  • Magic number = 256 - mask octet; subnets start at its multiples.
  • Devices in different subnets need a router; TTL drops by 1 through it.