DHCP

The Dynamic Host Configuration Protocol (DHCP) automatically assigns IP addresses and network configuration to devices on a network. It eliminates the need for manual IP configuration, reducing errors and simplifying network management.

How DHCP Works (DORA)

DHCP uses a four-step process called DORA:

  1. DHCP Discover: The client broadcasts a request for an IP address.
  2. DHCP Offer: The server offers an IP address and configuration.
  3. DHCP Request: The client requests the offered address.
  4. DHCP ACK: The server acknowledges the assignment.

DHCP Options

DHCP can provide more than just an IP address:

  • Subnet Mask: Defines the network portion.
  • Router (Default Gateway): The next hop for external traffic.
  • DNS Servers: Name servers for hostname resolution.
  • Domain Name: The local domain suffix.
  • NTP Servers: Time synchronization servers.
  • Lease Time: How long the client may use the address.

Linux DHCP Client

The dhclient command from ISC DHCP client:

dhclient eth0                  # Request lease on eth0
dhclient -r eth0               # Release lease
dhclient -v eth0               # Verbose output
cat /var/lib/dhcp/dhclient.leases  # View lease file

systemd-networkd also provides DHCP client support via systemd-networkd and networkctl.

DHCP Server: dnsmasq

dnsmasq provides a lightweight DHCP server:

# /etc/dnsmasq.conf
interface=eth0
dhcp-range=192.168.1.50,192.168.1.150,12h
dhcp-option=3,192.168.1.1       # Gateway
dhcp-option=6,192.168.1.10      # DNS
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.1.20,infinite

DHCP Server: ISC DHCP Server

The ISC DHCP Server (dhcpd) is a full-featured DHCP server:

# /etc/dhcp/dhcpd.conf
subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.50 192.168.1.150;
    option routers 192.168.1.1;
    option domain-name-servers 192.168.1.10, 8.8.8.8;
    option domain-name "local.lan";
}
systemctl enable --now dhcpd

DHCP Server: Kea

ISC Kea is the modern successor to ISC DHCP:

kea-dhcp4 -c /etc/kea/kea-dhcp4.conf

Static DHCP Reservations

Reserve specific IPs for specific MAC addresses:

# dnsmasq
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.1.20

# ISC dhcpd
host printer {
    hardware ethernet aa:bb:cc:dd:ee:ff;
    fixed-address 192.168.1.20;
}