Sub topic 1
Addresses, Prefix Lengths, and the Default Gateway
Prerequisites
This chapter assumes that you can use a Linux shell, understand basic IPv4 notation, and run commands with sudo. It focuses on the address settings a server needs before you configure them. The examples use IPv4 and the interface name enp1s0. Your server may use another name, such as eth0 or ens160.
The three settings that control IPv4 communication
An IPv4 address identifies a network interface. It has four decimal numbers separated by periods, such as 192.168.1.50. The address alone is not enough. The server also needs a prefix length and usually a default gateway.
A prefix length says how many bits belong to the network portion of the address. It is written after a slash:
192.168.1.50/24The /24 prefix is equivalent to the subnet mask 255.255.255.0. It means that addresses from 192.168.1.1 through 192.168.1.254 are normally in the same local IPv4 network. The address ending in .0 identifies the network, and .255 is the broadcast address in this example.
A default gateway is the local router that forwards traffic to destinations outside the server's local network. For this network, the gateway might be 192.168.1.1. The gateway must have an address in the same local network as the server.
A DNS server is different. DNS translates names such as example.com into IP addresses. DNS does not decide where packets go. The routing table and the default gateway perform that job.
| Setting | Example | Purpose |
|---|---|---|
| IPv4 address | `192.168.1.50` | Identifies the server interface |
| Prefix length | `/24` | Defines the local network boundary |
| Default gateway | `192.168.1.1` | Sends traffic to other networks |
| DNS server | `9.9.9.9` | Resolves hostnames to addresses |
Local and remote destinations
The prefix length lets Linux decide whether a destination is local. With 192.168.1.50/24, the server treats 192.168.1.20 as local. It can send traffic directly to that host through the local network interface.
The server treats 9.9.9.9 as remote because it is outside 192.168.1.0/24. Linux sends that traffic to the default gateway. The gateway then forwards it toward the destination.
The prefix must describe the network supplied by the administrator or hosting provider. A wrong prefix can create two common problems. If it is too broad, Linux may try to reach a remote address directly instead of using the gateway. If it is too narrow, Linux may send traffic to the gateway even when the destination is locally connected.
Inspect the current settings
Use these commands before changing anything:
ip -br address show
ip route
ip route show defaultip -br address show lists addresses in a compact form. Look for the interface name, its state, and an address such as 192.168.1.50/24.
ip route displays the routing table. A line like this defines the default route:
default via 192.168.1.1 dev enp1s0It means that Linux sends destinations without a more specific route through 192.168.1.1, using enp1s0.
A server can have multiple interfaces and multiple routes. The most specific matching route wins. If two routes are equally specific, Linux uses their metrics to choose between them. This matters on multi-homed servers, where adding a second default route without planning can send replies through the wrong interface.
Common errors
- A prefix such as
/16is used when the network is actually/24. The server then believes many remote addresses are local. - The gateway is outside the configured subnet. Linux cannot reach it through the interface.
- The address is already assigned to another device. ARP conflicts cause intermittent or complete connectivity loss.
- The default route is missing. Local hosts may work, but remote networks do not.
- A gateway is configured on the wrong interface. Traffic leaves through an unexpected network path.
Exercise
A server has 192.168.10.25/24, and its gateway is 192.168.20.1. What is wrong?
Show answer
The gateway is outside the server's local 192.168.10.0/24 network. The administrator must provide a gateway in the same subnet or correct the server's address and prefix according to the network design.