dotlinux guide

Setting Up a Linux DHCP Server: Step-by-Step Instructions

In modern networks, the Dynamic Host Configuration Protocol (DHCP) plays a critical role in automating IP address assignment, reducing manual errors, and simplifying network management. A DHCP server dynamically assigns IP addresses, subnet masks, default gateways, and DNS server information to client devices, eliminating the need for static IP configuration on each device. Linux, with its stability and flexibility, is an excellent platform for hosting a DHCP server. This blog will guide you through setting up a DHCP server on Linux using the ISC DHCP Server (the most widely used open-source DHCP implementation). We’ll cover fundamental concepts, step-by-step configuration, advanced options, best practices, and troubleshooting to help you deploy a reliable DHCP service.

Table of Contents

Understanding DHCP

Before diving into setup, let’s clarify key DHCP concepts:

How DHCP Works: The DORA Process

DHCP operates via a four-step handshake between clients and the server, known as DORA:

  1. Discover: A client broadcasts a request for an IP address.
  2. Offer: The server responds with an available IP address and configuration.
  3. Request: The client formally requests the offered IP.
  4. Acknowledge: The server confirms the IP assignment, including lease duration and network settings.

Key Terminology

  • Lease: The duration for which a client retains an assigned IP (e.g., 24 hours).
  • Scope: A range of IP addresses the server is configured to assign (e.g., 192.168.1.100–192.168.1.200).
  • Subnet: A segment of the network (e.g., 192.168.1.0/24).
  • Static Reservation: A permanent IP assigned to a specific client (via MAC address).

Prerequisites

Before starting, ensure you have:

  • A Linux server (physical or virtual) running Ubuntu/Debian, CentOS/RHEL, or a similar distro.
  • Static IP address configured on the server (DHCP servers must have a fixed IP to avoid downtime).
  • Root or sudo access to install packages and modify system files.
  • Network connectivity to clients (the server and clients must be on the same subnet, or you’ll need a DHCP relay for remote subnets).
  • Basic familiarity with Linux command-line and network concepts (e.g., subnets, CIDR).

Step-by-Step Setup

1. Install the DHCP Server Package

The most popular DHCP server for Linux is the ISC DHCP Server (maintained by the Internet Systems Consortium). Install it using your distro’s package manager:

Ubuntu/Debian:

sudo apt update && sudo apt install isc-dhcp-server -y

CentOS/RHEL:

sudo dnf install dhcp-server -y  # For RHEL 8+/CentOS 8+
# Or for older versions (RHEL 7/CentOS 7):
sudo yum install dhcp -y

2. Configure the DHCP Server

The main configuration file for ISC DHCP Server is /etc/dhcp/dhcpd.conf. By default, this file may be empty or contain only comments. We’ll create a basic configuration.

sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak

Step 2.2: Edit the Config File

Open the file with a text editor (e.g., nano or vim):

sudo nano /etc/dhcp/dhcpd.conf

Add the following basic configuration (adjust values for your network):

# Global options (apply to all subnets unless overridden)
default-lease-time 86400;   # 24 hours (in seconds)
max-lease-time 604800;      # 7 days (maximum lease duration)
option domain-name "example.com";  # Optional: Local domain name
option domain-name-servers 8.8.8.8, 8.8.4.4;  # DNS servers (Google DNS here)

# Subnet declaration (define IP range and network settings)
subnet 192.168.1.0 netmask 255.255.255.0 {
  range 192.168.1.100 192.168.1.200;  # IP range to assign
  option routers 192.168.1.1;        # Default gateway (router IP)
  option subnet-mask 255.255.255.0;  # Subnet mask
  option broadcast-address 192.168.1.255;  # Broadcast address
}

Explanation of Key Options:

  • default-lease-time: How long clients keep the IP by default.
  • range: The pool of dynamic IPs to assign.
  • option routers: The client’s default gateway (critical for internet access).
  • option domain-name-servers: DNS servers for name resolution.

3. Specify the Network Interface

The DHCP server needs to know which network interface(s) to listen on. This step varies by distro.

For Ubuntu/Debian:

Edit /etc/default/isc-dhcp-server:

sudo nano /etc/default/isc-dhcp-server

Set INTERFACESv4 to your server’s network interface (e.g., eth0, enp0s3; check with ip addr):

INTERFACESv4="eth0"  # Replace with your interface name

For CentOS/RHEL:

The DHCP server automatically listens on all interfaces by default, but you can restrict it by editing /etc/sysconfig/dhcpd (CentOS 7) or using a systemd drop-in (CentOS 8+). For simplicity, we’ll skip this unless needed (see troubleshooting if issues arise).

4. Start and Enable the DHCP Service

Start the DHCP server and configure it to run on boot:

Ubuntu/Debian:

sudo systemctl start isc-dhcp-server
sudo systemctl enable isc-dhcp-server

CentOS/RHEL:

sudo systemctl start dhcpd
sudo systemctl enable dhcpd

Verify the service is running:

sudo systemctl status isc-dhcp-server  # Ubuntu/Debian
# OR
sudo systemctl status dhcpd             # CentOS/RHEL

You should see active (running) in the output.

5. Configure the Firewall

DHCP uses UDP ports 67 (server) and 68 (client). Allow these ports through the firewall:

For UFW (Ubuntu/Debian):

sudo ufw allow 67/udp
sudo ufw reload

For Firewalld (CentOS/RHEL):

sudo firewall-cmd --add-port=67/udp --permanent
sudo firewall-cmd --reload

Configuring Advanced DHCP Options

Static IP Reservations

To assign a fixed IP to a specific client (e.g., a printer or server), use a host declaration in dhcpd.conf. Identify the client’s MAC address (e.g., 00:1a:2b:3c:4d:5e via ip link on the client).

Add this to dhcpd.conf (inside or outside the subnet block):

# Static reservation for a printer
host printer {
  hardware ethernet 00:1a:2b:3c:4d:5e;  # Client MAC address
  fixed-address 192.168.1.50;           # Fixed IP to assign
}

Restart the service for changes to take effect:

sudo systemctl restart isc-dhcp-server  # Ubuntu/Debian
# OR
sudo systemctl restart dhcpd             # CentOS/RHEL

Additional Network Parameters

Enhance client configuration with options like NTP servers, NetBIOS, or MTU:

subnet 192.168.1.0 netmask 255.255.255.0 {
  # ... existing settings ...
  option ntp-servers 192.168.1.2;        # NTP server for time sync
  option netbios-name-servers 192.168.1.3; # NetBIOS/WINS server
  option interface-mtu 1500;              # MTU size (default 1500)
}

Testing the DHCP Server

On the Server: Check Logs

Monitor DHCP activity in real time:

sudo journalctl -u isc-dhcp-server -f  # Ubuntu/Debian
# OR
sudo journalctl -u dhcpd -f             # CentOS/RHEL

On a Client Device: Renew IP

  1. Release the current IP (Linux client):
    sudo dhclient -r eth0  # Replace "eth0" with the client's interface
  2. Request a new IP:
    sudo dhclient eth0
  3. Verify the assignment:
    ip addr show eth0  # Check for the IP in your DHCP range
    cat /var/lib/dhcp/dhclient.leases  # View lease details

For Windows Clients:

  • Open Command Prompt: ipconfig /release then ipconfig /renew.
  • Verify with ipconfig /all.

Common Practices

Organize Config Files

For large networks, split configurations into smaller files (e.g., subnets.conf, reservations.conf) and include them in dhcpd.conf:

include "/etc/dhcp/subnets.conf";
include "/etc/dhcp/reservations.conf";

Monitor Leases

View active leases:

sudo cat /var/lib/dhcp/dhcpd.leases  # Raw lease file
# OR use a tool like dhcp-lease-list (install with sudo apt install dhcp-lease-list on Ubuntu)
sudo dhcp-lease-list

Manage Multiple Subnets

For networks with multiple subnets, use a DHCP relay agent (e.g., isc-dhcp-relay on Linux routers) to forward DHCP traffic between subnets.

Best Practices

Security

  • Restrict Interfaces: Only listen on trusted interfaces to avoid serving unintended networks.
  • Static Reservations for Critical Devices: Use fixed IPs for servers, printers, and IoT devices to avoid IP conflicts.
  • Avoid Overlapping Scopes: Ensure no two DHCP servers on the same network have overlapping IP ranges.

Redundancy

Deploy DHCP failover (two servers) to ensure availability if one server fails. ISC DHCP supports failover via failover peer configurations (see references for details).

Lease Time Management

  • Use short leases (e.g., 1 hour) for temporary devices (guests, mobile phones).
  • Use long leases (e.g., 7 days) for stationary devices (desktops, servers).

Documentation

Maintain a log of static reservations, IP ranges, and server settings for troubleshooting.

Troubleshooting

Service Fails to Start

  • Check Config Syntax: Run sudo dhcpd -t -cf /etc/dhcp/dhcpd.conf to validate the config file. Fix errors like missing semicolons or invalid IPs.
  • Interface Mismatch: Ensure INTERFACESv4 (Ubuntu) is set to the correct interface.
  • IP Conflicts: Ensure the DHCP range doesn’t include static IPs already in use.

Clients Not Receiving IPs

  • Firewall Blocking: Verify UDP port 67 is open on the server.
  • Server Unreachable: Ensure the client and server are on the same subnet (or a relay is configured).
  • Scope Exhausted: Check if all IPs in the range are leased (dhcp-lease-list). Expand the range if needed.

Conclusion

Setting up a Linux DHCP server using ISC DHCP is a straightforward process that brings centralized IP management to your network. By following the steps above, you can deploy a basic server and expand it with advanced features like static reservations and redundancy. Remember to adhere to best practices for security, reliability, and scalability. With proper configuration, your DHCP server will simplify network administration and reduce manual overhead.

References