How to Configure DHCP Server in CentOS/RHEL 7

Dynamic Host Configuration Protocol (DHCP) As the name suggests, DHCP is used to control the network configuration of a host through a remote server. DHCP is an excellent alternative to the time-consuming manual configuration of network settings on a host or a network device. It automatically assigns IP addresses and other network configuration information to systems on a network.

The following Diagram showing the DHCP Operation.

Basic DHCP operation in Linux

Let us check how to configure the DHCP server in CentOS/RHEL 7. First we need to set up a static IP address for the DHCP Server. Use nmcli or edit the configuration file.

configure DHCP server CentOS 7

We need to install the DHCP Package using YUM or RPM. Run ‘yum install dhcp -y’ to install dhcp package.

# yum install dhcp -y
install dhcp package RHEL 7

Let us check what all configuration files created after the dhcp package installation.

# rpm -qlc dhcp
rpm -qlc dhcp

Installing the dhcp package creates a file, /etc/dhcp/dhcpd.conf, which is merely an empty configuration file. This is configuration file for IPv4 DHCP.

dhcp configuration file dhcpd.conf

Looking into this file we could see that there is a sample configuration file available for reference. If you cat the /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example file, you would get a better idea to configure the DHCP server. There are a lot of sample configurations available.

dhcpd.conf.example example dhcpd.conf file

The first step in configuring a DHCP server is to create the configuration file that stores the network information for the clients. Use this file to declare options for client systems.

Here is my DHCP server configuration. my IP address range is 192.168.1.101 to 192.168.1.111. and additionally we are give some options like netmask, router, domain, DNS etc.

# vi /etc/dhcp/dhcpd.conf
subnet 192.168.1.0 netmask 255.255.255.0 {
  range 192.168.1.101 192.168.1.111;
  option domain-name-servers 192.168.1.200;
  option domain-search "thegeeksearch.com";
  option subnet-mask 255.255.255.0;
  option routers 192.168.1.100;
  default-lease-time 600;
  max-lease-time 7200;
}

Thats all about DHCP Configuration. There are many options available in DHCP configuration could you refer the man pages and example file for more information. Let’s restart the DHCP service and enable service.

# systemctl restart dhcpd
# systemctl status  dhcpd
# systemctl enable dhcpd

Now we need to work on the client-side. Check any ip address is configured in the client. Run ‘ip add show’ command.

# ip add show
DHCP configuration RHEL 7

There no IP address is configured for eno16777736 interface now. Run ‘dhclient eno16777736’ or take a reboot or restart NetworkManager this will get the IP address from the DHCP server.

dhcpclient command to get IP address from DHCP server

You could see the following entries in the /var/log/messages in the DHCP server.

DHCPDISCOVER and DHCPOFFER messages

Fore IP lease information you could see in /var/lib/dhcpd/dhcpd.leases file in DHCP server. You could see something similar to this.

:var:lib:dhcpd:dhcpd.leases file