Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
Networking

Subnet masks explained: how a host decides what is local

How a subnet mask works: the binary AND, the local-or-gateway decision, what a wrong mask breaks, checks on Linux and Windows, wildcard masks and planning.

CloudSat teamNovember 18, 20267 min read
Abstract illustration of a row of glowing bits, half lit and half dark, laid over a small grid of connected devices
On this pageA mask is a row of ones, then zeros

A subnet mask answers one question for every packet a host sends: is the destination on my own network, or do I hand it to the router? When the mask is right, that decision is invisible. When it is wrong, you get some of the strangest failures in networking: some addresses answer, others time out, and the internet may still load fine. This guide explains the decision itself and how to debug it. For prefix tables and host counts, keep the CIDR cheat sheet open next to it.

A mask is a row of ones, then zeros

An IPv4 mask is 32 bits. Ones mark the network part of an address, zeros the host part, and the ones always come first without gaps. 255.255.255.0 is 24 ones, written /24 in CIDR notation. 255.255.255.224 is 27 ones:

255.255.255.224  =  11111111.11111111.11111111.11100000   (/27)

Because the ones must be contiguous, each octet of a valid mask can only be 0, 128, 192, 224, 240, 248, 252, 254 or 255. A mask such as 255.255.255.250 does not exist: its binary form, 11111010, has a gap.

The AND that finds the network

A host finds its network address with a bitwise AND of its own address and the mask: a bit survives only where the mask has a one. Take 10.4.7.100/27:

address  10.4.7.100        00001010.00000100.00000111.01100100
mask     255.255.255.224   11111111.11111111.11111111.11100000
AND      10.4.7.96         00001010.00000100.00000111.01100000

The host lives in 10.4.7.96/27, whose usable addresses run from .97 to .126, with .127 as the broadcast address. Two addresses share a subnet exactly when the AND with the mask gives the same result for both.

The decision behind every packet

Take a host at 192.168.10.37/24 with its gateway at 192.168.10.1:

  1. To 192.168.10.80: the AND gives 192.168.10.0, the host's own network. The host asks for that machine's hardware address with ARP and sends the frame straight to it.
  2. To 192.168.20.5: the AND gives 192.168.20.0, a different network. The host addresses the frame to the gateway's hardware address and leaves the IP destination unchanged. The router takes it from there.
  3. To a public address such as 198.51.100.7: the same as case 2.

The routing table generalizes this logic. Each interface's subnet appears as a directly connected route, the default route catches everything else, and the most specific matching route wins. Linux shows its decision for any destination:

ip route
# default via 192.168.10.1 dev eth0 proto dhcp src 192.168.10.37 metric 100
# 192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.37
ip route get 192.168.10.80
# 192.168.10.80 dev eth0 src 192.168.10.37
ip route get 192.168.20.5
# 192.168.20.5 via 192.168.10.1 dev eth0 src 192.168.10.37

No via means the host delivers the packet itself; via names the router it will use. On Windows, ipconfig prints the mask in dotted form, route print -4 shows the table with on-link routes marked as such, and PowerShell gives prefix lengths:

Get-NetIPAddress -AddressFamily IPv4 | Format-Table InterfaceAlias, IPAddress, PrefixLength
Find-NetRoute -RemoteIPAddress 192.168.20.5

Find-NetRoute returns the source address and the route Windows would use for that destination, including its next hop.

What a wrong mask breaks

The symptoms depend on which way the mask is wrong:

Mistake What the host believes What you see
Too wide: /16 where the network is /24 Every 192.168.x.x address is local Hosts on other office subnets never answer, because ARP requests go unanswered; the internet still works
Too narrow: /25 where the network is /24, host at .37 Only .0 to .127 is local Traffic to the upper half detours through the router, slowly or not at all
Too narrow, and the gateway ends up outside the host's subnet The gateway is not reachable directly Linux refuses to add the default route; nothing beyond the segment works
Two neighbors with different masks Each decides differently One-way failures: A reaches B, but B sends its replies to the router

The last case is the hardest to spot, because each host looks correct when you check it alone. When something fails in one direction only, compare the masks on both ends before you suspect a firewall.

The neighbor table gives a too-wide mask away. Run ip neigh on Linux, or Get-NetNeighbor -AddressFamily IPv4 in PowerShell, right after a failed attempt. A remote address listed as INCOMPLETE, FAILED or Unreachable means the host tried to reach it directly, as if it were local. Some routers hide the mistake by answering those requests on behalf of the remote hosts, a feature called proxy ARP, so the same wrong mask can work on one network and fail on the next.

Cloud servers bend the textbook

Some cloud providers give a server a /32 address, or a large shared subnet, and reach the gateway through an explicit route marked as on-link. In netplan that looks like this:

network:
  version: 2
  ethernets:
    eth0:
      addresses: [203.0.113.24/32]
      routes:
        - to: default
          via: 203.0.113.1
          on-link: true

With a /32, no other address is local, so the host would never consider the gateway reachable. on-link: true tells it to deliver to the gateway directly anyway. If your server's configuration looks unusual, leave it as the provider generated it. "Fixing" the mask is a common way to take a cloud server offline.

Wildcard masks are masks turned inside out

Cisco access lists and OSPF use wildcard masks, where zeros mark the bits that must match and ones mark the bits that may vary. Subtract the subnet mask from 255.255.255.255 to get one: /24 becomes 0.0.0.255, and /27 becomes 0.0.0.31.

access-list 10 permit 192.168.10.0 0.0.0.255
router ospf 1
 network 10.4.7.96 0.0.0.31 area 0

Mixing the two styles up is a classic mistake: 0.0.0.255 written where a subnet mask belongs matches almost nothing you meant.

Choose masks for a small network

Masks do not have to be the same size across a network. Plan from the largest need down, and keep every block aligned on its own size. An office that received 10.20.0.0/22, which is 1,024 addresses, might split it like this:

Segment Devices Prefix Mask Range
Staff 200 /24 255.255.255.0 10.20.0.0 to 10.20.0.255
Guest Wi-Fi 100 /25 255.255.255.128 10.20.1.0 to 10.20.1.127
Servers 40 /26 255.255.255.192 10.20.1.128 to 10.20.1.191
Printers and cameras 20 /27 255.255.255.224 10.20.1.192 to 10.20.1.223
Management 10 /28 255.255.255.240 10.20.1.224 to 10.20.1.239

That leaves 10.20.1.240/28 and the whole of 10.20.2.0/23 for growth. Size each segment so it can double before it runs out, and write the plan down where the next person will find it. Separate segments also give a firewall clear boundaries to enforce, which is the starting point of a DMZ design.

IPv6 drops the dotted mask

IPv6 has no dotted mask, only a prefix length, and ordinary LAN segments are /64. Hosts learn which prefixes are on-link from router advertisements, so the classic wrong-mask mistakes are rarer. They still happen with manual configuration: an address entered as /128 makes every neighbor look remote.

FAQ

Why do masks only contain values like 255, 252 or 192?

Each octet of a mask is part of one continuous run of ones. Only nine patterns of eight bits fit that rule, from 0 to 255, so any other value is a typing mistake.

Hosts on the same segment should all use the same mask. A mismatch does not always fail outright, which is what makes it dangerous: it produces one-way and intermittent problems.

Why does my VPS show /32 when my laptop shows 255.255.255.0?

Your provider routes the address to your server instead of placing it in a shared LAN, and an on-link route reaches the gateway. It is a deliberate design, not an error.

Is 255.255.255.255 a valid mask?

Yes. It is a /32, one single address, used for loopback addresses, firewall rules that name one host and some cloud network setups.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.