
How to Set Up a DHCP Server on CentOS 8: A Step-by-Step Guide
Managing IP addresses across a network can be a tedious and error-prone task. Manually assigning an IP address to every device is inefficient, especially as your network grows. This is where a DHCP (Dynamic Host Configuration Protocol) server becomes an indispensable tool, automating the entire process of IP address assignment and network configuration.
By setting up your own DHCP server, you gain granular control over your network’s IP address pool, lease times, and other critical settings. This guide provides a comprehensive walkthrough on how to install and configure a DHCP server on CentOS 8.
A Note on CentOS 8: It’s important to be aware that CentOS 8 has reached its End-of-Life (EOL). While the instructions here are accurate for CentOS 8, we strongly recommend using a production-ready alternative like Rocky Linux or AlmaLinux for new deployments. The steps in this guide are generally applicable to these RHEL-based distributions as well.
Prerequisites
Before you begin, ensure you have the following:
- A system running CentOS 8 (or a similar RHEL derivative).
- Root or sudo-level access.
- A static IP address configured on the server. Your DHCP server cannot have a dynamic IP address itself; it must be a fixed point of reference on the network.
Step 1: Install the DHCP Server Package
The first step is to install the necessary software package. The DHCP server software on CentOS is provided by the dhcp-server
package.
Open your terminal and run the following command to install it using the DNF package manager:
sudo dnf install dhcp-server -y
This command will download and install the DHCP server and all its required dependencies.
Step 2: Configure the DHCP Server
With the software installed, the next crucial step is configuration. The main configuration file is located at /etc/dhcp/dhcpd.conf
. This is where you will define the network settings that the server will hand out to client devices.
First, it’s a good practice to back up the default configuration file before making changes:
sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak
Now, open the configuration file in your preferred text editor, such as nano
or vi
:
sudo nano /etc/dhcp/dhcpd.conf
You will need to define a subnet block that matches your network’s configuration. Below is a detailed example configuration. You must modify the IP addresses, domain names, and ranges to fit your specific network environment.
# DHCP Server Configuration Example
# Define global options for all clients
option domain-name "your-domain.lan";
option domain-name-servers 8.8.8.8, 8.8.4.4;
default-lease-time 600;
max-lease-time 7200;
# This DHCP server is the official one for the local network
authoritative;
# Define the network segment this server will manage
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200;
option routers 192.168.1.1;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
}
Let’s break down what each of these parameters means:
option domain-name "your-domain.lan";
: Sets the default domain name for the clients.option domain-name-servers 8.8.8.8, 8.8.4.4;
: Specifies the DNS servers that clients should use. Here, we’re using Google’s public DNS servers as an example.default-lease-time 600;
: The default time in seconds that an IP address is “leased” to a client before it needs to be renewed.max-lease-time 7200;
: The maximum lease time in seconds if a client requests a longer duration.authoritative;
: This is a critical setting. It tells the server that it is the official DHCP server for this network segment. If it receives a request from a client it doesn’t know, it will send aDHCPNAK
message, preventing misconfigured clients from getting an invalid IP address.subnet 192.168.1.0 netmask 255.255.255.0 { ... }
: This block defines the configuration for a specific network. You must update the subnet and netmask to match your network.range 192.168.1.100 192.168.1.200;
: This defines the pool of IP addresses that the server is allowed to assign to clients. It’s important to choose a range that does not conflict with static IPs used by servers, routers, or other critical infrastructure.option routers 192.168.1.1;
: Specifies the default gateway for the clients on this subnet.
After editing the file, save and close it.
Step 3: Allow DHCP Through the Firewall
By default, the CentOS firewall will block incoming DHCP traffic. You must create a rule to allow this service so clients can reach the server. DHCP uses UDP ports 67 and 68.
Use the following firewall-cmd
commands to permanently add the dhcp
service and reload the firewall rules:
sudo firewall-cmd --add-service=dhcp --permanent
sudo firewall-cmd --reload
This ensures the DHCP service can communicate freely on the network.
Step 4: Start and Enable the DHCP Service
Now that everything is configured, you can start the DHCP service and enable it to launch automatically at system boot.
Use the following systemctl
commands:
# Start the DHCP service now
sudo systemctl start dhcpd
# Enable the DHCP service to start on boot
sudo systemctl enable dhcpd
# Check the status to ensure it's running without errors
sudo systemctl status dhcpd
If the status shows active (running)
and there are no red error messages, your DHCP server is now operational.
Actionable Advice: DHCP Reservations
For devices like printers or file servers, you often want them to have a consistent IP address without configuring it statically on the device itself. A DHCP reservation (or static lease) is the perfect solution. It ties a specific IP address to a device’s unique MAC address.
To create a reservation, you first need the device’s MAC address. Then, add a host
block to your /etc/dhcp/dhcpd.conf
file, outside of the subnet declaration:
host fileserver {
hardware ethernet 00:1A:2B:3C:4D:5E;
fixed-address 192.168.1.50;
}
host fileserver
: A descriptive name for the device.hardware ethernet
: The MAC address of the device’s network card.fixed-address
: The specific IP address you want to assign to this device.
After adding a reservation, you must restart the DHCP service for the change to take effect:
sudo systemctl restart dhcpd
Now, whenever the device with that MAC address requests an IP, it will always be assigned 192.168.1.50
. This combines the convenience of central management with the reliability of a static IP.
Source: https://kifarunix.com/install-and-setup-dhcp-server-on-centos-8/