Skip to main content
p. 252

9.19 Understanding DHCP

Under IPv4, when you set a network host to get its configuration automatically from the network, you’re telling it to use the Dynamic Host Configuration Protocol (DHCP) to get an IP address, subnet mask, default gateway, and DNS servers. Aside from not having to enter these parameters by hand, network administrators gain other advantages with DHCP, such as preventing IP address clashes and minimizing the impact of network changes. It’s very rare to see a network that doesn’t use DHCP.

For a host to get its configuration with DHCP, it must be able to send messages to a DHCP server on its connected network. Therefore, each physical network should have its own DHCP server, and on a simple network (such as the one in Section 9.1), the router usually acts as the DHCP server.

note

When making an initial DHCP request, a host doesn’t even know the address of a DHCP server, so it broadcasts the request to all hosts (usually all hosts on its physical network).

p. 253

When a machine asks a DHCP server to assign it an IP address, it’s really asking for a lease on an address for a certain amount of time. When the lease is up, a client can ask to renew the lease.

9.19.1 Linux DHCP Clients​

Although there are many different kinds of network manager systems, there are only two DHCP clients that do the actual work of obtaining leases. The traditional standard client is the Internet Software Consortium (ISC) dhclient program. However, systemd-networkd now also includes a built-in DHCP client.

Upon startup, dhclient stores its process ID in /var/run/dhclient.pid and its lease information in /var/lib/dhcp/dhclient.leases.

You can test dhclient by hand on the command line, but before doing so you must remove any default gateway route (see Section 9.11.2). To run the test, simply specify the network interface name (here, it’s enp0s31f6):

# dhclient enp0s31f6

Unlike dhclient, the systemd-networkd DHCP client can’t be run by hand on the command line. The configuration, described the systemd .network(5) manual page, is in /etc/systemd/network, but like other kinds of network configuration, can be automatically generated by Netplan.

9.19.2 Linux DHCP Servers​

You can task a Linux machine with running a DHCP server, which provides a good amount of control over the addresses it gives out. However, unless you’re administering a large network with many subnets, you’re probably better off using specialized router hardware that includes built-in DHCP servers.

Probably the most important thing to know about DHCP servers is that you want only one running on the same subnet in order to avoid problems with clashing IP addresses or incorrect configurations.