DEV Community

Cover image for Dynamic Host Configuration Protocol (DHCP)
SAHIL
SAHIL

Posted on

Dynamic Host Configuration Protocol (DHCP)

What is DHCP?
DHCP stands for Dynamic Host Configuration Protocol. It's a network management protocol used on Internet Protocol (IP) networks for automatically assigning and dynamically allocating IP addresses and other network configuration parameters to devices connected to the network.

In simple terms, instead of manually setting the IP address, subnet mask, default gateway, and DNS servers on every single computer, you let a DHCP server do it for you, automatically.

Why Use a DHCP Server?
Using a DHCP server provides several significant advantages:

  • Centralized Management and Efficiency: It eliminates the time-consuming and error-prone process of manually configuring network settings on every host. This is especially critical in large networks.
  • Preventing IP Address Conflicts: The DHCP server tracks which IP addresses are in use. It ensures that every device gets a unique IP address for a specific period (called a lease), preventing two devices from trying to use the same address.
  • Portability and Mobility: Devices can easily move between different network segments (subnets) and automatically receive the correct configuration for their new location without manual changes.
  • Scalability: It makes adding new devices to the network trivial; they just boot up and get their configuration.

The DHCP Process (DORA)
The core communication process between a client (a device joining the network) and the DHCP server is often remembered using the acronym DORA:

  • Discover: The client broadcasts a DHCP Discover message on the network to find any available DHCP servers.
  • Offer: All DHCP servers that receive the Discover message respond with a DHCP Offer, proposing an IP address and lease time to the client.
  • Request: The client receives the offers and broadcasts a DHCP Request message, formally requesting the use of the IP address offered by a specific server (and implicitly declining the others).
  • Acknowledgment: The chosen DHCP server sends a DHCP Acknowledgment (ACK) to the client, confirming the lease of the IP address and providing the rest of the configuration parameters (subnet mask, gateway, DNS, etc.).

Key DHCP Components and Terminology

Term Description
Scope/Pool A range of IP addresses that the DHCP server is allowed to assign to clients on a particular subnet.
Lease The duration for which a client is allowed to use an assigned IP address. Clients must renew their lease before it expires.
Reservation A specific IP address permanently reserved for a specific client, identified by its MAC address. This ensures the device always gets the same IP.
DHCP Relay A component (often a router) that forwards DHCP broadcast messages between clients and DHCP servers located on different subnets.
Bootp An older, simpler protocol that DHCP evolved from.

DHCP on Linux
On Linux, the most common software package used to implement a DHCP server is ISC DHCP Server, now often superseded by the Kea DHCP server, developed by the Internet Systems Consortium (ISC).

ISC DHCP Server (often called dhcpd): The traditional, highly mature, and widely used DHCP server for Linux.

Kea DHCP: A newer, high-performance, and modular DHCP server designed to handle the demands of very large networks.

When we move to the practical part, we'll likely focus on configuring one of these services!

Let's dive into the practical setup of a DHCP server on Linux, focusing on the traditional and widely used ISC DHCP Server (dhcpd).

Practical DHCP Server Setup (ISC dhcpd)
1. Key Files, Ports, and Daemons
The following are the essential components you'll interact with when configuring and running a DHCP server on a Linux distribution like Debian or CentOS/RHEL:

Category Component Details
Daemon/Service dhcpd or isc-dhcp-server The main background process (daemon) that runs the DHCP server.
Configuration File /etc/dhcp/dhcpd.conf (or /etc/dhcpd.conf) The primary file where all the server settings, pools, and reservations are defined. This is the most important file.
Leases File /var/lib/dhcp/dhcpd.leases (location may vary) A dynamic file where the server stores a record of every IP address it has assigned (leased) and to which client (MAC address).
Port UDP Port 67 The standard destination port used by the DHCP server to listen for client requests.
Port UDP Port 68 The standard source port used by the DHCP client when sending requests to the server.

2. Basic Configuration Setup
The goal is to configure the dhcpd.conf file and ensure the DHCP server is listening on the correct network interface.

2.1 Installation
First, you need to install the DHCP server package.

On Debian/Ubuntu
sudo apt update
sudo apt install isc-dhcp-server
Enter fullscreen mode Exit fullscreen mode
 On RHEL/CentOS/Fedora
sudo dnf install dhcp
Enter fullscreen mode Exit fullscreen mode

2.2 Editing /etc/dhcp/dhcpd.conf
This file defines the scope (the range of IPs) and the network options. You need to configure a subnet declaration that matches the network interface the server is running on.

Here is a template for a basic configuration:

# Global Parameters (apply to all subnets)
# Default lease time is the minimum time a client keeps an IP (in seconds)
default-lease-time 600;

# Max lease time is the longest time a client can hold an IP (in seconds)
max-lease-time 7200;

# Set the authoritative flag to prevent the server from answering requests for networks it doesn't serve
authoritative;

# Subnet Declaration (This MUST match the network the server is connected to)
# Example: Server's interface IP is 192.168.1.1/24

subnet 192.168.1.0 netmask 255.255.255.0 {
    # The range of addresses DHCP can assign
    range 192.168.1.100 192.168.1.150;

    # Network options to push to clients:
    # Option 3: Default Gateway/Router
    option routers 192.168.1.1;

    # Option 6: Domain Name Servers (e.g., Google's public DNS)
    option domain-name-servers 8.8.8.8, 8.8.4.4;

    # Option 15: Domain Name
    option domain-name "mylocaldomain.lan";
}
Enter fullscreen mode Exit fullscreen mode

Dive Deep: The server itself must have a static IP address (e.g., 192.168.1.1 in this example) within the subnet. It cannot rely on DHCP for its own configuration. The IP range defined in the range statement must not include the server's static IP or any other static IPs you've assigned manually.

2.3 Interface Configuration
On some Linux distributions, you must tell the DHCP daemon which network interface to listen on.

In older versions, you might edit a file like /etc/default/isc-dhcp-server and define the interface:

INTERFACESv4="eth0"
Enter fullscreen mode Exit fullscreen mode

On modern systems using systemd, this is often handled automatically or configured via network management tools.

2.4 Starting the Service
After configuring, restart or enable the service:

# Reload the configuration and start the server
sudo systemctl restart isc-dhcp-server
# or
sudo systemctl restart dhcpd

# Check the status to ensure it's running without errors
sudo systemctl status isc-dhcp-server

# Check logs for detailed information
sudo journalctl -u isc-dhcp-server
Enter fullscreen mode Exit fullscreen mode

3. Dynamic IP Usage by Clients
Once the server is running, any client configured for DHCP (the default for most devices) on that same physical network will automatically follow the DORA process:

Client Boot: A client (e.g., a laptop or phone) boots up and sends a broadcast DHCP Discover request from its network interface.

Server Response: The Linux DHCP server running dhcpd receives the request and replies with a DHCP Offer proposing an IP address from its defined range (e.g., 192.168.1.101).

Client Acceptance: After the ACK, the client configures its network interface with the leased IP address (192.168.1.101), the subnet mask (255.255.255.0), the default gateway (192.168.1.1), and the DNS servers (8.8.8.8, 8.8.4.4).

Lease Renewal:
At T.lease/2 (half the lease time), the client will attempt to renew the lease with the DHCP server to keep the IP address.

4. Advanced: Making a Reservation
You can ensure a specific device always receives the same IP address by creating a static mapping or reservation based on the device's MAC address (Hardware Ethernet Address). This is often used for printers or servers.

Add this block inside the subnet declaration in /etc/dhcp/dhcpd.conf:

host printer1 {
    hardware ethernet aa:bb:cc:11:22:33; # The MAC address of the printer
    fixed-address 192.168.1.200;       # The reserved IP address (outside the dynamic range is best)
}
Enter fullscreen mode Exit fullscreen mode

After making this change, restart the dhcpd service again. The client with that specific MAC address will now receive 192.168.1.200.

5. Firewall Considerations
Crucially, the Linux machine running the DHCP server must allow traffic on UDP Port 67. You need to configure your firewall (e.g., firewalld or ufw) to permit incoming requests.

# Using UFW (Uncomplicated Firewall - common on Ubuntu)
sudo ufw allow 67/udp

# Using firewalld (common on CentOS/RHEL/Fedora)
sudo firewall-cmd --add-service=dhcp --permanent
sudo firewall-cmd --reload
Enter fullscreen mode Exit fullscreen mode

This completes the deep dive into the practical setup! You now have a working framework for a DHCP server on Linux.

Thanks for reading and leave a like and your wonderful insights on dhcp.

Top comments (0)