DHCP lets a server hand out IP addresses, masks, gateways and name servers to machines as they connect, so nobody types them in by hand. A new client has no address yet, so it broadcasts a request, a server offers an address, and the client holds it on a lease that it renews. Run exactly one DHCP server on each subnet, keep routers and servers on fixed addresses outside the pool, and size the pool and the lease together: the carrier’s plan leaves 240 addresses for ordinary machines, and visitors can exhaust them if leases are long.
Part of Networking
Automatic assignment of IP addresses and network settings from a pool, with leases, reservations and the faults that rogue servers cause.
One Server Replaces A Walk Round Every Machine
Network Addressing contrasts static assignment (reliable for infrastructure that must always be at a known address) with dynamic assignment, and Resource Sharing lists DHCP as a shared service that maintains a pool, manages lease times and renews automatically. Routing Basics adds that DHCP normally supplies the default gateway along with the address and mask. This article is the build: what the exchange looks like and how to set a server up.
The exchange has four messages. These details are from general knowledge, not from the other articles: the client broadcasts a discover message, a server answers with an offer, the client requests the offered address, and the server acknowledges. They run over UDP, from port 68 on the client to port 67 on the server (UDP Protocol). The first message must be a hardware broadcast because the client has no address and does not know the server; Network Addressing notes that DHCP is one of the uses of the broadcast address. A broadcast does not cross a router (Routing Basics), so a subnet with no server on it needs a relay on its router.
What You Need
- A server with a fixed address on the subnet — Linux Server Setup
- A DHCP program — dnsmasq handles both addresses and names and suits a small network (the carrier recommends it for simpler DNS needs; that it also does DHCP is general knowledge); larger sites use a dedicated server
- The address plan — the pool must not overlap the fixed addresses (IP Addressing)
- The hardware address of every machine that needs a fixed lease
Method: Set Up A DHCP Server
Step 1: Define the pool
Use the carrier’s split: router at .1, servers at .2 to .10, and the pool from .11 to .250. In dnsmasq a pool is one line, for example dhcp-range=10.1.0.11,10.1.0.250,12h, with the last field the lease time. The 12 hours is an example, not a recommendation.
Step 2: Tell clients the gateway and the name server
For example dhcp-option=option:router,10.1.0.1 for the gateway. The name server is set the same way (DNS).
Step 3: Reserve addresses for machines that must not move
Bind a hardware address to an address, for example dhcp-host=aa:bb:cc:dd:ee:ff,10.1.0.20. Put a reserved address inside the fixed range of the plan or exclude it from the pool, and record it in the plan. Duplicate hardware addresses are possible (Ethernet Basics) and will confuse a reservation.
Step 4: Test with a machine that has never connected It should receive an address in the pool, the right gateway and the right name server. Check with the interface commands in Network Interface.
Step 5: Keep the lease table in the plan Back up the reservations; the leases themselves are rebuilt by renewals.
Worked Example: Pool And Lease Together
Pool .11 to .250: 250 - 11 + 1 = 240 addresses. A settlement has 190 permanent machines. That leaves 240 - 190 = 50 addresses for visitors.
With a 24-hour lease, every visiting device holds its address until the lease expires, even after it leaves. If 60 different visiting devices connect during one day, 190 + 60 = 250, which is more than 240, so the last 10 get no address. If the lease is shortened to 1 hour, an address is freed about an hour after a visitor leaves, so the number of addresses in use at any moment is the permanent machines plus the visitors present, not the visitors of the whole day. Short leases help a busy visitor network; long leases mean the permanent machines keep stable addresses with fewer renewals. Pick the lease from how fast machines come and go.
Common Mistakes
- Two DHCP servers on one subnet — a salvaged home router with its DHCP switched on, plugged into the settlement network, answers requests with its own address range and gateway; machines get a wrong gateway at random. Disable DHCP on every other device before the real server goes live
- A pool that overlaps fixed addresses — two machines then share an address
- Treating a self-assigned address as success — a machine on 169.254.x.x did not get an address (IPv4 Structure); check the server and the cable
- A server that is also a DHCP client — if the DHCP server restarts and the name server depends on a DHCP address, nothing comes back; give servers fixed addresses