host declaration block within your dhcpd.conf file. This method ensures that a client always receives the same lease regardless of the subnet's dynamic pool configuration. By binding the hardware address directly to an IP via the hardware ethernet and fixed-address directives, you override the default random assignment behavior for that specific device.
Understanding DHCP Lease Rotation
By default, the DHCP server manages a pool of available IP addresses and assigns them to clients as they request connections. When a client disconnects or its lease expires, the IP is returned to the pool and may be assigned to a different machine next time it connects. This process is known as lease rotation or dynamic assignment. While this is efficient for general networks where devices change frequently, it causes issues for servers, printers, or network infrastructure components that require a consistent network identity.
The solution lies in creating static reservations within the DHCP configuration. Unlike a true static IP configured on the network interface card (NIC) itself, a DHCP reservation keeps the IP management centralized in the server configuration. This approach allows you to maintain a single source of truth for all IP assignments while preventing specific devices from receiving changing addresses. For more context on managing multiple interfaces, you might find Configuring Multiple IPs on eth0 for NIC Testing useful for understanding broader networking setups.
Locating the Configuration File
The primary configuration file for the ISC DHCP server is typically located at /etc/dhcp/dhcpd.conf. On some distributions, such as older versions of Debian or Ubuntu, it might be found in /etc/dhcp3/dhcpd.conf. You need root access to edit this file, so use a text editor like nano or vim with sudo privileges.
sudo nano /etc/dhcp/dhcpd.conf
Before making changes, it is best practice to create a backup of the original file. This allows you to revert changes if a syntax error prevents the DHCP service from starting. Run the following command to copy the current configuration:
sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.backup
Once backed up, open the file for editing. The structure of the file generally consists of global declarations, subnet declarations, and host declarations. Your new static assignment will go inside a host block.
Constructing the Host Declaration
To prevent rotation, you must identify the client by its unique MAC address and assign it a fixed IP. The syntax requires a host block containing the hardware ethernet directive and the fixed-address directive. You can name the host block anything descriptive, such as the hostname of the client or a comment describing its function.
First, determine the MAC address of the client machine. On Linux, you can find this by running ip link show or ifconfig -a. The MAC address format is usually six pairs of hexadecimal digits separated by colons (e.g., 00:11:22:33:44:55). Ensure you use the correct interface identifier, typically eth0, enp0s3, or similar, depending on your system's naming convention.
Next, add the following block to your dhcpd.conf file. Place this block outside of any subnet declaration blocks, typically at the end of the file or after the global options section:
host my-server {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.1.100;
}
In this example, replace my-server with a meaningful name, update the MAC address to match your target device, and set the fixed-address to the IP you wish to reserve. This IP must fall within the range of the subnet defined in your configuration but should ideally be outside the dynamic pool range to avoid conflicts, although modern DHCP servers handle overlap gracefully by prioritizing reservations.
Validating and Applying Changes
After saving the file, you must verify the syntax before restarting the service. An incorrect configuration will cause the DHCP daemon to fail to start, potentially leaving your network without IP assignment services. Use the dhcpd command with the -t flag for testing:
sudo dhcpd -t
If the test passes, you will see no output or a success message. If there are errors, the command will display the line number and description of the problem. Correct any syntax issues, such as missing semicolons or mismatched braces, and re-test.
Once validated, restart the DHCP service to apply the changes. The command varies by distribution:
# For systemd-based systems
sudo systemctl restart isc-dhcp-server
# For older SysVinit systems
sudo service dhcpd restart
Restarting the service forces the server to reload the configuration file. Existing leases are maintained unless the lease database file is also cleared, but new requests from the specified MAC address will immediately receive the fixed IP.
Managing Lease Files
The DHCP server stores active leases in a file, typically /var/lib/dhcp/dhcpd.leases. If a client previously obtained a dynamic IP and then you configure a reservation, the old lease might persist until it expires. To force immediate application of the new static IP, you can clear the lease file for that specific entry or delete the entire lease file and restart the server. Note that deleting the lease file removes records for all clients, which may cause temporary network disruption as clients renegotiate their addresses.
For environments involving virtual machines, resolving static IP issues can be tricky due to generated MAC addresses. If you are working with KVM or QEMU, check out KVM/QEMU: Resolving Static IP Address Assignment Issues with TAP Devices for insights on handling virtual network interfaces.
Frequently Asked Questions
What happens if I assign two hosts the same MAC address?
The DHCP server will likely assign the IP to whichever client requests it first or based on the order of processing in the config file. This creates unpredictable behavior and network conflicts. Always ensure each reservation has a unique MAC address.
Can I use a hostname instead of a MAC address?
No, DHCP relies on Layer 2 identifiers like MAC addresses for precise client identification. Hostnames are negotiated after the IP is assigned and are not reliable for initial reservation because they can be spoofed or changed easily by the client OS.
Does this method work for IPv6?
The concept is similar but uses different syntax. For IPv6, you would use the duid (DUID) or ia-na statements along with fixed-address in an host block, though many administrators prefer SLAAC or DHCPv6 prefixes for IPv6 static assignments.
Why does my client still get a different IP after configuring a reservation?
This often happens if the client has an old lease cached locally. You should renew the IP address on the client side (e.g., dhclient -r followed by dhclient) or wait for the existing lease to expire so the server can issue the new fixed address.
Is it better to use DHCP reservations or static IPs on the device?
DHCP reservations are generally preferred because they keep all IP management in one central location. If you move a device to a different physical port or subnet, you only need to update the server config rather than logging into every individual device to change its network settings.