How to install and configure a DHCP server on Centos 8

DHCP (Dynamic Host Configuration Protocol) is used to automatically assign an IP address to mobile devices, laptops, PCs, and other network devices so that they can communicate. It uses a connectionless service model using UDP (Custom Datagram Protocol) .DHCP uses the well known UDP port 67 for the DHCP server and UDP port 68 for client. DHCP operations are divided into four phases: server discovery, IP lease offer, IP lease request, and IP lease confirmation. These stages are often referred to as DORA for Discovery, Offer, Request and Confirmation. In this tutorial, we will learn how to install and configure a DHCP server on Centos8. So let’s get started.

Installing and configuring DHCP

I have a DHCP server with a static IP address 192.168.1.1/24. The DHCP server will automatically assign an IP address to other devices on the 192.168.1.0/24 network.

I have ens37 interface which is used for DHCP server. To assign a static IP address to this interface, you can use the following command:

# nmtui edit

You can also assign an IP address to enter the network settings.

To check if an IP address is assigned or not, use any of the following commands:

# ip a
# ifconfig

Ifconfig

At this point, the assigned IP address is 192.168.1.1/24.

To install DHCP packages, open a terminal and use the following command.

# dnf install –y dhcp-server

Set up a DHCP server

After installing the package, it’s time to configure the DHCP server.

DHCP server configuration

The main DHCP server configuration file is /etc/dhcp/dhcpd.conf. Before proceeding with the configuration, make a copy of the original backup file using the following command:

# cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bk

DHCP server configuration

To configure a DHCP server, edit the /etc/dhcp/dhcpd.conf configuration file using the following command:

# vim /etc/dhcp/dhcpd.conf

Edit the dhcpd.conf file

default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
authoritative;
subnet 192.168.1.0 netmask 255.255.255.0 {
  range 192.168.1.50 192.168.15.200;
  option routers 192.168.1.1;
  option subnet-mask 255.255.255.0;
  option domain-name-servers 192.168.1.1;

}

DHCP configuration

Here the DHCP server will reserve an IP address for 10 min (600 sec) and maximum for 2 hours (7200 sec) for a specific device.

IN Subnet section defines DHCP configuration for 192.168.1.0/24 Network:

IN Range section defines the assigned IP address from 192.168.1.50 – 192.168.1.200

IN Routers defines the default gateway.

IN Subnet mask defines the subnet mask to be assigned to each host.

IN Domain name server specifies the DNS name servers to be assigned to each host.

You can add more than one subnet according to your needs. Once you’re done with the config file, start the service with this command:

# systemctl enable dhcpd
# systemctl start dhcpd

Enable DHCPD

Start DHCP service

To check that the DHCP service is running, use the following command:

# systemctl status dhcpd

Check DHCP status

Firewall configuration

After restarting the service, allow the DHCP service through the firewall using the following command:

# firewall-cmd --add-service=dhcp --permanent

Configure firewall

Restart your firewall for it to take effect by using the following command:

# firewall-cmd --reload

Reboot your firewall

Testing DHCP server on Centos

As you can see, the client machine (Centos) got an IP address automatically. 192.168.1.128 from server 192.168.1.1

IP address assigned by DHCP

Testing the DHCP Server on Windows

As you can see from the state of the network adapter of the Windows client machine, the machine automatically received an IP address. 192.168.1.128 from server 192.168.1.1, as it shown on the picture.

DHCP server testing

Reserve IP address on DHCP server

If you have the MAC address of the device, you can also bind an IP address to it by opening the configuration file vim /etc/dhcp/dhcpd.conf and add the following lines at the end of the page to associate an IP address with a specific device.

host vitux {
  hardware ethernet 00:50:56:8c:20:fd;
  fixed-address 192.168.1.150;
}

Reserve IP address on DHCP server

It will bind the IP address 192.168.1.150 with the machine whose MAC address is 00: 50: 56: 8c: 20: fd

Conclusion

In this tutorial, we will learn how to set up a DHCP server on Centos8. We saw that the host machine automatically obtained an IP address from a DHCP server and also saw how to bind an IP address to a specific machine using a MAC address.

How to install and configure a DHCP server on Centos 8

Sidebar