Sub topic 1
Addresses, prefix lengths, and the default gateway
This chapter builds the working model for the rest of the tutorial. Before you write any Netplan or NetworkManager configuration, you need to know what the three core values of an IPv4 setup mean: the address, the prefix length, and the default gateway. A mistake in any one of them can leave a server unreachable, and each fails in its own way.
Prerequisites
This chapter assumes you can:
- Use a Linux shell and edit files with a terminal editor.
- Read dotted-decimal IPv4 addresses such as
192.168.1.50. - Run commands with
sudo. - Reach the server's console, or have another way to recover if a network change cuts off SSH.
The commands here only read state. Nothing in this chapter changes the configuration of your server.
What an IPv4 address identifies
An IPv4 address is a 32-bit number, written as four decimal octets (each 0 to 255) separated by dots. An address belongs to an interface, not to the machine. A server with two network interfaces normally has at least two addresses. One interface can also hold several addresses.
An address has two parts: a network part and a host part. All machines on the same network share the network part. The host part tells them apart. The address alone does not say where the split falls. The prefix length does.
Prefix length and the subnet mask
The prefix length is the number of leading bits in the address that belong to the network part. It is written after a slash. This is CIDR notation (Classless Inter-Domain Routing).
In 192.168.1.50/24, the first 24 bits are the network and the last 8 bits are the host. The older way to write the same thing is a subnet mask, where the network bits are all ones: 255.255.255.0.
| Prefix | Subnet mask | Addresses in block | Usable hosts |
|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,216 | 16,777,214 |
| /16 | 255.255.0.0 | 65,536 | 65,534 |
| /24 | 255.255.255.0 | 256 | 254 |
| /25 | 255.255.255.128 | 128 | 126 |
| /26 | 255.255.255.192 | 64 | 62 |
| /30 | 255.255.255.252 | 4 | 2 |
In a normal subnet, two addresses in the block are reserved. The first (host bits all zero) is the network address. The last (host bits all one) is the broadcast address. That is why usable hosts are two fewer than the block size. A /31 (point-to-point links) and /32 (a single host) are special cases that this chapter does not cover.
Working out the network from an address
Take 192.168.1.50/24.
- Write the prefix as a mask:
255.255.255.0. - Apply a bitwise AND of the address and the mask. The result is the network address:
192.168.1.0. - The broadcast address sets all host bits to one:
192.168.1.255. - Usable hosts run from
192.168.1.1to192.168.1.254.
A prefix that is not on an octet boundary needs more care. For 192.168.1.50/26, the block size is 64. The blocks start at 0, 64, 128, and 192 in the last octet. Address .50 falls in the block starting at .0, so the network is 192.168.1.0/26, the broadcast is 192.168.1.63, and usable hosts are .1 to .62.
How the prefix decides where traffic goes
The prefix length is what the kernel uses to answer one question for every outgoing packet: is the destination on my own network, or must I send it elsewhere?
- If the destination falls inside a network the interface is directly attached to, the server sends the packet straight to that host. It finds the host's hardware (MAC) address with ARP (Address Resolution Protocol).
- If it does not, the server sends the packet to the default gateway and lets that router carry it onward.
When you assign 192.168.1.50/24 to an interface, the kernel automatically adds a route for 192.168.1.0/24 through that interface. This is a connected route. You do not configure it by hand.
The default gateway
The default gateway is the router on your local network that receives all traffic for destinations outside it. In the routing table it appears as the default route, written default or 0.0.0.0/0. The prefix length of 0 matches every address, so it is used only when no more specific route matches. Linux always chooses the longest matching prefix.
A routing table on a simple server might hold two entries:
192.168.1.0/24 dev enp1s0: the connected network.default via 192.168.1.1 dev enp1s0: everything else goes to192.168.1.1.
Two rules follow from how delivery works.
- The gateway address must be inside a directly connected network. The server can only reach the gateway by ARP on its own link. A gateway of
10.0.0.1on a server whose only network is192.168.1.0/24cannot work, and the kernel rejects the route with "Network is unreachable". - A host normally has one default route. Two defaults with the same metric cause ambiguous or unpredictable behavior. A second default route is valid only if it has a different metric, as in a failover design.
Reading the real values on a server
Use ip from the iproute2 package to see what the kernel is using. The -br flag gives brief, one-line-per-interface output.
ip -br address show
ip route show
ip route get 9.9.9.9Typical output looks like this:
lo UNKNOWN 127.0.0.1/8 ::1/128
enp1s0 UP 192.168.1.50/24 fe80::5054:ff:fe12:3456/64default via 192.168.1.1 dev enp1s0 proto static
192.168.1.0/24 dev enp1s0 proto kernel scope link src 192.168.1.509.9.9.9 via 192.168.1.1 dev enp1s0 src 192.168.1.50 uid 1000
cacheRead these like this:
- The state column (
UP) shows the link state.DOWNmeans the interface is not active, and no address can work. 192.168.1.50/24is the address and prefix together. The slash form is howipalways prints it.proto kernelmarks the connected route that the kernel created.proto staticmeans a configuration tool or an administrator set the route.proto dhcpmeans a DHCP client did.src 192.168.1.50is the source address the server will place on packets.ip route getasks the kernel which route it would use for a given destination, without sending anything. It is the most direct way to check a gateway decision.
Common mistakes with prefix and gateway
These errors come from the model above, and each has a distinct symptom.
| Mistake | What happens |
|---|---|
| Prefix too short (`/16` instead of `/24`) | The server believes more addresses are local. It tries to reach off-network hosts directly with ARP, gets no reply, and those hosts time out. |
| Prefix too long (`/28` instead of `/24`) | The server believes peers in the same real subnet are remote, and sends their traffic through the gateway. This may work, or cause odd asymmetry. |
| Gateway outside the subnet | The route cannot be installed. You see "Network is unreachable", or the tool reports an error. |
| Wrong gateway inside the subnet | The route installs, but off-network traffic goes nowhere. Local hosts still respond. |
| Missing default route | Local network works. Every outside destination fails with "Network is unreachable". |
| Duplicate address | Another host uses the same IP. Connections drop intermittently as the two machines answer ARP in turn. |
A useful diagnostic split: if you can ping the gateway but not an outside IP, the problem is at or beyond the gateway. If you cannot ping the gateway, check the address, prefix, link state, and cabling or virtual network first.
Checking for a duplicate address
Before you pick a static address, confirm that nobody else is using it. With the iputils package, arping in duplicate address detection mode sends ARP probes and exits with a non-zero status if a reply arrives.
sudo arping -D -c 3 -I enp1s0 192.168.1.50
echo "exit status: $?"Run this before assigning the address, from a host on the same network. Exit status 0 means no one answered, and 1 means another host claims the address. Note that some distributions ship a different arping implementation with different options, so check man arping if the flags are rejected.
Choosing an address for a server
Pick the address deliberately, because a static address that overlaps a DHCP pool will eventually collide with a lease.
- Find the DHCP range on your router or DHCP server and choose an address outside it. Many sites reserve a block, for example
.2to.99, for static hosts. - Alternatively, create a DHCP reservation that always gives the server the same address. The server still uses DHCP, but the address is stable.
- Use the private ranges (RFC 1918) for internal networks:
10.0.0.0/8,172.16.0.0/12, and192.168.0.0/16. - Record the address, prefix, and gateway in your documentation. You will enter them again in the Netplan and NetworkManager chapters.
The three values you will need later are the address with its prefix (192.168.1.50/24), the gateway (192.168.1.1), and the DNS servers, which later chapters cover.
Summary of the model
- The address names an interface. The prefix length says which part of the address is the network.
- The prefix length drives a local-or-remote decision for every packet.
- The default gateway is the next hop for everything remote. It must sit inside a connected network.
ip -br address,ip route, andip route getshow what the kernel is really using.
Exercise
A server has the address 10.20.30.77/26 and the default gateway 10.20.30.100. Work out the network address, the broadcast address, and the usable host range. Then say whether the gateway is valid and why.
Show answer
A /26 has a block size of 64 in the last octet, so blocks start at 0, 64, 128, and 192. The address .77 falls in the block starting at .64. The network address is 10.20.30.64, the broadcast address is 10.20.30.127, and usable hosts run from 10.20.30.65 to 10.20.30.126. The gateway 10.20.30.100 is inside that range, so it is on the connected network and is valid. A gateway such as 10.20.30.130 would be outside the block, and the kernel would refuse the route.
Exercise
On a server, ip route show prints only 192.168.1.0/24 dev enp1s0 proto kernel scope link src 192.168.1.50. Pings to 192.168.1.20 work. Pings to 9.9.9.9 fail with "Network is unreachable". What is missing, and which command would show the route the kernel would use for 9.9.9.9?
Show answer
The default route is missing. The only route is the connected network, so no entry matches 9.9.9.9. Run ip route get 9.9.9.9 to see the kernel's choice. With no default route it reports "Network is unreachable". The fix is to add a default gateway in the persistent configuration, for example via 192.168.1.1.