DHCP
Learn how DHCP hands out IP addresses and other network settings automatically, the four-step DORA process, leases, address pools and reservations.
The problem before DHCP
Every device on a network needs an IP address, a subnet mask, a default gateway and a DNS server before it can do anything useful. In early networks, someone typed all of these into each machine by hand.
That does not scale. Setting up 200 laptops takes a long time, and moving a laptop to another room means starting over. If two people type the same address by accident, both devices lose connectivity.
DHCP (Dynamic Host Configuration Protocol) solves this. A DHCP server gives these settings automatically to every device that joins the network.
The four steps: DORA
When your phone joins a WiFi network, this exchange takes place within a fraction of a second:
- Discover: the phone broadcasts a message to everyone on the network: "Is there a DHCP server? I need an address."
- Offer: a DHCP server replies: "You can use 192.168.1.50."
- Request: the phone answers: "I would like to use 192.168.1.50."
- Acknowledge: the server confirms: "Confirmed. It is yours for now."
The first letters spell D-O-R-A. In step one the phone has no address at all, so it cannot send a message to a specific device. It has to broadcast to the whole network. The messages use UDP, with the server listening on port 67 and the client on port 68.
Leases
The address is not permanent. The server leases it for a set time, often a day or several days. About halfway through the lease, the device asks to renew it. If the device leaves the network and never renews, the address goes back into the pool for someone else.
Leases keep a small pool useful on a network where many devices come and go, for example a cafe with 50 customers a day and only 20 addresses.
Address pools and fixed addresses
A DHCP server hands out addresses from a pool, which is a range you define. A common home layout looks like this:
Network: 192.168.1.0/24
Router (gateway): 192.168.1.1
Fixed addresses: 192.168.1.2 to 192.168.1.99 (printers, servers)
DHCP pool: 192.168.1.100 to 192.168.1.199 (phones, laptops)
Keeping fixed devices outside the pool prevents a clash where the server hands out an address that a printer is already using. A pool of 100 addresses is plenty for a home. A busy public network with short leases needs a bigger one.
What DHCP provides
It gives a device more than an IP address:
- IP address
- Subnet mask
- Default gateway
- DNS servers
- Sometimes other options, such as a domain name or a time server
In most homes the router runs the DHCP server, so you never configure it yourself.
Reservations
Some devices, such as a printer or a home server, should always get the same address, but you may not want to type it into the device. A DHCP reservation tells the server: "whenever this MAC address asks for an address, always give it 192.168.1.20".
The device still uses DHCP, but its address never changes. You configure the reservation once, in the router or DHCP server, and nothing on the device.
Check what DHCP gave you
ip addr # Linux
ipconfig /all # Windows
On Windows, ipconfig /all also shows the DHCP server and the times the lease starts and ends. To release the address and ask for a new one:
sudo dhclient -r && sudo dhclient # Linux: release and renew
ipconfig /release && ipconfig /renew # Windows
The commands depend on your Linux setup. On some systems, NetworkManager handles DHCP, and disconnecting and reconnecting has the same effect.
169.254 means DHCP failed. The device stopped waiting and assigned itself an address. Check the cable, the WiFi connection and the DHCP server.When DHCP goes wrong
- No server answers. The device falls back to a
169.254.x.xaddress and cannot reach anything beyond the local link. - The pool is full. New devices get nothing until old leases expire. Shorten the lease time or enlarge the pool.
- Two servers hand out addresses. If someone plugs in a second router by mistake, this "rogue" DHCP server can give devices the wrong gateway and break their connection. Unplug the extra router.
- A duplicate address. A device with a hand-typed address inside the pool can collide with a leased one. Keep fixed devices outside the pool.
The first fix to try is simple: release and renew the lease, or restart the router.