Sub topic 3
DHCP at a Glance: Clients, Servers and Leases
This chapter gives you the working model of DHCP: who the parties are, what they exchange, and what a lease is. It assumes you already know IPv4 addresses and subnet masks, what a default gateway and a DNS server do, and what a LAN, a router and a broadcast are. It also assumes you understand client-server communication at a high level. Later chapters cover the message exchange, timers, options and failure cases in detail.
What DHCP does
The Dynamic Host Configuration Protocol (DHCP) is a network protocol that gives a device its IPv4 settings automatically when it joins a network. Without it, someone has to type an address, a mask, a gateway and a DNS server into every device.
DHCP delivers a bundle of settings, not only an address:
- An IPv4 address.
- A subnet mask, which tells the host which addresses are on its own subnet.
- A default gateway, which is the router the host uses to reach other networks.
- One or more DNS servers, which the host uses to resolve names.
- A lease time, which says how long the host may keep the address.
DHCP is defined in RFC 2131 (the protocol) and RFC 2132 (the options). It evolved from BOOTP, an older protocol with the same message format, and it keeps BOOTP's packet layout for compatibility.
The parties
The DHCP client
A DHCP client is any device that asks for configuration. Laptops, phones, printers, IP cameras, VoIP phones and virtual machines are usually clients. The client software is part of the operating system's network stack. On Linux it may be dhclient, dhcpcd, systemd-networkd or NetworkManager. Windows and macOS include their own clients.
A client starts with no usable IPv4 address. It must still send packets, so it uses the address 0.0.0.0 as its source. The next chapter explains how that works.
The DHCP server
A DHCP server is a program that owns a range of addresses and hands them out. It also keeps a record of which address belongs to which client. Servers run in several places:
- A home router or access point, which usually has a small built-in server.
- A Windows Server or Linux machine running a service such as ISC Kea, ISC dhcpd or dnsmasq.
- A router or Layer 3 switch in an office or branch.
- A cloud platform, where the virtual network supplies addresses to instances.
One server can serve many subnets. A subnet normally has one active server, or a pair configured for failover, so that clients get consistent answers.
The relay agent
A relay agent is a device that forwards DHCP messages between a client's subnet and a server on a different subnet. Routers do not forward broadcasts, so without a relay a server would be needed on every subnet. Relays are covered in their own chapter. For now, treat the server as reachable from the client's subnet.
The lease
A lease is a temporary grant of an address to a client. The server records the address, the client's identity, and the time when the grant ends. The client may use the address until then, and it must stop using it if the lease expires without renewal.
Leases exist because networks change. Devices leave without telling anyone. If addresses were given out permanently, a pool would fill up with addresses nobody uses. With leases, an address that is not renewed returns to the pool by itself.
The server identifies a client by a client identifier if the client sends one (option 61). Otherwise, it uses the client's hardware (MAC) address. That identity is what lets a returning client get its old address back.
Choosing a lease time
The lease time is a trade-off set by the administrator.
| Lease time | Advantage | Cost |
|---|---|---|
| Short (minutes to a few hours) | Addresses return to the pool quickly. Good for guest Wi-Fi with many short visits. | More renewal traffic. A server outage affects clients sooner. |
| Medium (about 8 to 24 hours) | Balanced. A common default for office LANs. | Departed devices hold addresses for up to a day. |
| Long (days or weeks) | Little traffic. Clients survive long server outages. | Slow reclaiming of addresses. Configuration changes reach clients slowly. |
A good rule is to make the lease long compared with how long a typical device stays, and long compared with how long you would take to repair the server. A pool that is nearly full is a reason to shorten the lease.
A first look at the exchange
A client that has no address gets one through four messages. The names are shortened to DORA.
- Discover: the client broadcasts a request to find servers.
- Offer: a server proposes an address and settings.
- Request: the client asks for the offered address, which also tells other servers that their offers were declined.
- Acknowledge: the server confirms the lease and sends the final settings.
Each step has details, such as which addresses and ports are used, that the next two chapters explain. The point here is that the whole process normally takes well under a second.
The lease life cycle in brief
A lease moves through states over time. The client does not wait for expiry. It starts asking for an extension halfway through.
By default, the two timers are:
- T1 (renewal) at 50% of the lease. The client asks the server that granted the lease to extend it.
- T2 (rebinding) at 87.5% of the lease. If the original server did not answer, the client asks any server.
A short calculation shows the numbers for a 24-hour lease.
lease_seconds = 86400 # 24-hour lease
t1 = lease_seconds * 0.5 # default renewal time: 50% of the lease
t2 = lease_seconds * 0.875 # default rebinding time: 87.5% of the lease
print(f"Renew (unicast to the original server) at {t1:.0f} s")
print(f"Rebind (broadcast to any server) at {t2:.0f} s")
print(f"Lease expires at {lease_seconds} s")The output is 43200 s for T1 and 75600 s for T2. Because renewal starts at the halfway point, a server can be down for many hours before any client loses its address. A later chapter covers the timers fully.
What a server stores
A server keeps a lease database, which is a table of bindings. Each binding holds at least:
- The IP address.
- The client identifier or MAC address.
- The lease start and end times.
- The state of the address, for example free, active or expired.
Servers write this table to disk, so that a restart does not cause two clients to receive the same address. Different servers add other fields, such as the client's hostname. You can read the file on an ISC dhcpd server, and dnsmasq keeps a simple text file, commonly /var/lib/misc/dnsmasq.leases. The location varies by distribution.
A minimal server configuration
The following dnsmasq configuration defines a pool with a 12-hour lease, hands out a gateway and DNS server, and reserves a fixed address for one device.
# /etc/dnsmasq.conf (DHCP part only)
interface=eth0
# Hand out 192.168.1.100-192.168.1.200 with a 12-hour lease
dhcp-range=192.168.1.100,192.168.1.200,255.255.255.0,12h
# Option 3 (router) and option 6 (DNS server)
dhcp-option=option:router,192.168.1.1
dhcp-option=option:dns-server,192.168.1.1
# Reservation: this MAC address always gets the same IP
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.1.50Reading it line by line:
interface=eth0limits the server to one network interface.dhcp-rangedefines the pool, the mask and the lease time. The pool holds 101 addresses.- The two
dhcp-optionlines set the gateway (option 3) and DNS server (option 6) that clients receive. dhcp-hostties one MAC address to one IP address. That device still uses DHCP, but it always gets192.168.1.50.
Scopes and reservations get their own chapter. The reserved address here lies outside the pool, which is a common way to avoid conflicts.
Viewing a lease on a client
You can see what a client received. On Linux with the ISC client, you can release and renew a lease, then check the result. On Windows, ipconfig /all shows the server and lease times.
# Linux (ISC dhclient)
sudo dhclient -r eth0 # release the current lease
sudo dhclient -v eth0 # request a new lease, verbose output
ip -4 addr show eth0 # check the address that was assigned
:: Windows (Command Prompt)
ipconfig /release
ipconfig /renew
ipconfig /allThe lines starting with :: are Windows Command Prompt comments, so run the two halves in their own shells. In the ipconfig /all output, look at these fields:
- DHCP Server: the address of the server that granted the lease.
- Lease Obtained and Lease Expires: the start and end of the lease.
- Default Gateway and DNS Servers: the options the server supplied.
Common misunderstandings
- DHCP does not make an address permanent just because the client always gets the same one. A client often gets the same address back because the server remembers it, but the address is still leased.
- A reservation does not turn DHCP off for that device. The device still sends Discover and Request, and the server still answers.
- The server does not push configuration to clients. The client starts every exchange.
- The lease time is not the same as the time a device stays connected. A device that leaves keeps its lease active on the server until it expires or the client sends a release.
- DHCP gives out addresses but does not check that they are free by itself in every case. Many servers probe an address (often with ping or ARP) before offering it, and many clients check with ARP before using it. Conflicts still happen when a device has a static address inside the pool.
Where DHCP sits in the stack
DHCP is an application-layer protocol that runs over UDP. The server listens on UDP port 67 and the client uses UDP port 68. A new client cannot use TCP, because TCP needs both sides to have addresses and a connection setup. UDP lets the client send a single broadcast datagram with no prior state.
You can watch this traffic with tcpdump:
sudo tcpdump -n -i eth0 -vv 'udp and (port 67 or port 68)'Run this on a machine on the same LAN, then renew a lease on a client. You see the four messages and the options inside them. The chapter on troubleshooting shows how to read the output.
Summary of the model
- A client with no address asks for configuration. A server answers from a pool and records a lease.
- The lease has a fixed length. The client renews at about half of it and rebinds at 87.5%.
- The server identifies clients by client identifier or MAC address and keeps a lease database.
- The lease time is an administrator's trade-off between address reuse and traffic and resilience.
Exercise
A DHCP server grants a 6-hour lease. At what times after the grant does the client try to renew and then rebind, using the default timers? Which server does it contact at each step?
Show answer
6 hours is 21600 seconds. T1 is 50%, so 10800 s (3 hours). The client contacts the server that granted the lease directly (unicast). T2 is 87.5%, so 18900 s (5 hours 15 minutes). If the original server has not answered, the client broadcasts to any server. The lease ends at 21600 s (6 hours). If no server has extended it by then, the client must stop using the address.
Exercise
An office has a pool of 100 addresses and about 90 laptops, but guests also connect to the same pool through the day and the pool often runs out. Name one lease-time change that helps and one cost of it.
Show answer
Shorten the lease, for example to 1 or 2 hours. Addresses of departed devices then return to the pool sooner. The cost is more renewal traffic, and clients are affected sooner if the server goes down. A better fix is also to enlarge the pool or put guests on a separate scope.