Install and Setup DHCP Server on CentOS 8

|
Last Updated:
|
|

Do you want to automatically assign IP addresses to the network devices in your LAN? Well, then you need to install and setup a Dynamic Host Configuration Protocol (DHCP) server. Follow through this guide to learn how to install and setup DHCP server on CentOS 8.

Installing DHCP Server on CentOS 8

install and Setup DHCP Server on CentOS 8
Our Deployment Architecture

Run system update

Begin by resynchronizing system packages to their latest versions.

dnf update

In CentOS 8, the DHCP server is provided by the dhcp-server package. This package can be installed by running the command below;

dnf install dhcp-server
Dependencies resolved.
=======================================================================================================================================================
 Package                              Arch                            Version                                    Repository                       Size
=======================================================================================================================================================
Installing:
 dhcp-server                          x86_64                          12:4.3.6-30.el8                            BaseOS                          529 k

Transaction Summary
=======================================================================================================================================================
Install  1 Package

Total download size: 529 k
...

Configure DHCP Server on CentOS 8

When installed, the dhcp-server package creates an empty configuration file /etc/dhcp/dhcpd.conf.

It also creates a sample configuration file for ISC dhcpd under /usr/share/doc/dhcp-server/dhcpd.conf.example.

NOTE: This guide only provides DHCP server configuration for IPv4 addresses only.

To begin with, you need to define the parameters and declarations options that stores the network information for the DHCP clients. These parameters and declarations should be made on the DHCPd configuration file, /etc/dhcp/dhcpd.conf.

Use the sample configuration file, /usr/share/doc/dhcp-server/dhcpd.conf.example to guide you when defining these options.

First off, be aware that dhcpd listens only on interfaces for which it finds subnet declaration in dhcpd.conf.

If you declare a subnet that is not associated with the interface on which DHCP server is listening on, DHCP server daemon will fail to start with the error;

No subnet declaration for enp0sX (x.x.x.x).
** Ignoring requests on enp0sX.  If this is not what
you want, please write a subnet declaration
in your dhcpd.conf file for the network segment
to which interface enp0s3 is attached. **
...

For example, to set DHCP server to listen on an interface, enp0s8, whose IP address details are shown below;

ip add sh dev enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:36:48:ae brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.13/24 brd 192.168.10.255 scope global dynamic enp0s8
       valid_lft 1032sec preferred_lft 1032sec

Next, open the DHCP server configuration file for editing.

vim /etc/dhcp/dhcpd.conf

Configure your DHCP server such that your configuration may look like;

#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp-server/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
######################################################
#Define the domain name of your DHCP server
option domain-name "kifarunix-demo.com";

# Define the IP address or Hostname of your DNS server
option domain-name-servers ns1.kifarunix-demo.com;

#If you have multiple DNS servers, specify them as;
# option domain-name-servers ns1.kifarunix-demo.com, ns2.kifarunix-demo.com;

# Define how long should an endpoint use the assigned IP address
default-lease-time 3600;    # One hour

# Define the maximum lease time
max-lease-time 7200; # Two hours

# Other values you can use are 86400 (one day), 604800 (one week) and 2592000 (30 days).
# 
# Declare the DHCP server official for the local network
authoritative;

# Change the default log file for DHCP server.
log-facility local7;

#### Subnet Declaration ####
# The subnet declaration includes a range of IP addresses that a DHCP server can assign to clients.
# and global options to apply for every DHCP client.
#
# Define the subnet of the DHCP server interface (Network address and subnet mask)
subnet 192.168.10.0 netmask 255.255.255.0 {
    # Define the broadcast address
    option broadcast-address 192.168.10.255;
    # Define the gateway
    option routers 192.168.10.1;
    # Define the range of IP addresses to be leased
    range 192.168.10.110 192.168.10.120;
}

Defining Static IP Address Using DHCP

If you need to define static IP addresses using DHCP for specific hosts in your network, you need to get the MAC address of the host. After which, you can add the host declaration configuration into DHCP server configuration file.

For example, to assign a static IP address to the host, webdev01.kifarunix-demo.com with MAC address, 01:23:45:67:89:ab, create the host declaration as shown below;

# Fixed Host IP Address assigning
host webdev01 {
    # Define the MAC address
    hardware ethernet 08:00:27:57:57:ad;
    # Define Fixed IP address
    fixed-address 192.168.10.125;
}

If you configured DHCP to log to a specific facility instead of the using the default, /var/log/messages, you need to create the log directory. This can be done by modifying the Rsyslog configuration.

For example, to configure DHCP server to log to /var/log/dhcpd/dhcpd.log, you need to create the directory /var/log/dhcpd.

Hence, since we configured to log to facility local7, simply add the line below to Rsyslog.conf.

echo "local7.debug /var/log/dhcpd/dhcpd.log" >> /etc/rsyslog.conf

Check Rsyslog for syntax errors;

rsyslogd -N1

Create the log directory.

mkdir /var/log/dhcpd

Restart Rsyslog

systemctl restart rsyslog

Running DHCP Daemon

Once the configuration is done, you can start and enable DHCPd by executing the command;

systemctl enable --now dhcpd

Check the status;

systemctl status dhcpd
● dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-10-20 10:01:59 EAT; 2min ago
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)
 Main PID: 6598 (dhcpd)
   Status: "Dispatching packets..."
    Tasks: 1 (limit: 5072)
   Memory: 5.3M
   CGroup: /system.slice/dhcpd.service
           └─6598 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid
...

Also check the logs on, /var/log/dhcpd/dhcpd.log.

tail /var/log/dhcpd/dhcpd.log

Open DHCP UDP Port on Firewall

To allow remote clients to query your DHCP server for IP addresses, you need to open DHCP UDP port 67 on firewalld if it is running.

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

Reload FirewallD

firewall-cmd --reload

Configure DHCP Client on CentOS 8

To verify that your DHCP server is working, you need to configure DHCP client. In this guide, we are using another CentOS 8 server as DHCP client.

Install DHCP client package by running the command below if it is not already installed.

dnf install dhcp-client

Once the DHCP client is installed, simply run dhclient command to get the DHCP IP.

dhclient

Now check the assigned IP addresses.

On the host with DHCP static IP mapping (webdev01) for our case;

ip add
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:57:57:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.125/24 brd 192.168.56.255 scope global dynamic enp0s8
       valid_lft 3059sec preferred_lft 3059sec
    inet6 fe80::76d2:60c9:4cea:3ec8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

On the other host, the addressed assigned should be within the defined range.

nmcli dev show enp0s8
GENERAL.DEVICE:                         enp0s8
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         08:00:27:5A:86:69
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     Wired connection 1
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.10.113/24
IP4.GATEWAY:                            192.168.10.5
IP4.ROUTE[1]:                           dst = 192.168.10.0/24, nh = 0.0.0.0, mt = 0
IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 192.168.56.2, mt = 0
IP4.DOMAIN[1]:                          kifarunix-demo.com
...

You have successfully setup DHCP server and client. That marks the end of our guide.

Read more about DHCP parameters and options on ISC DHCP manual pages.

ISC DHCP Manual pages

Related guides

How to Install and Setup DHCP Server on Fedora 29/Fedora 28/CentOS 7

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

1 thought on “Install and Setup DHCP Server on CentOS 8”

  1. I love you, you saved my neck. Thanks to your tutorial I repaired the service in 35 minutes.
    Thank you very much!!!!

    Reply

Leave a Comment