In this blog we will go through on How to Install and configure DHCP on Rocky Linux and other RHEL derivatives such as Red Hat Enterprise Linux, CentOS Stream or Alma Linux.
Introduction
Dynamic Host setup Protocol, or DHCP IP is used to automatically assign IP addresses and other network setup parameter. It makes network administration simpler by doing away with the need for manual IP address setup. By assigning the IP address to each device dynamically rather than manually, DHCP streamlines the process of configuring a network. You may utilize this highly scalable technology on networks that have a variety of devices. Even non-technical individuals may set up modest business or residential networks using DHCP. Thus, understanding how to configure DHCP is helpful for maintaining the network and resolving connection problems. We will go through how to configure DHCP on Rocky Linux 9 (an operating system based on RHEL) in this article.
Pre-requisite
We will need two machines running with Rocky Linux 8 or 9. One for DHCP Server with static IP configured and other for the client machines for testing automatic IP assignment after successful bring up of DHCP Server.
DHCP Server : 192.168.5.3
You may use any other RHEL derivatives such as RHEL, CentOS Stream or Alma Linux. If you nee support to Install OS refer How to Install Rocky Linux
DHCP Packages Installation
Make sure the system is running on the latest version
sudo dnf update -y
If the above actions has upgraded the kernel packages make sure you reboot the system.
You are now ready to install the DHCP packages
sudo dnf install dhcp-server -y
Configuration
Once the packages is installed its now time to modify the default DHCP configuration files. Run the below commadn to open the file using any editor of your choice
sudo vim /etc/dhcp/dhcpd.conf
Replace the default content of the file with the below content. Make sure you change the subnet details of your environment
default-lease-time 3600;
max-lease-time 86400;
authoritative;
subnet 192.168.5.0 netmask 255.255.255.0 {
range 192.168.5.3 192.168.10.253;
option routers 192.168.5.1;
option subnet-mask 255.255.255.0;
option domain-name-servers 192.168.5.1;
}
Save and Close the file once you finish editing
We will now go through each of those above values to understand better for you to use as per your network environment
- default-lease-time : The value specifies how long the DHCP server will lease an address to a client. In this case, the default-lease-time value is 3600 seconds or 1 hour. The max-lease-time is the maximum duration that the IP will be leased to a client. In our case, this is set to 86400 seconds or 24 hours.
- subnet : In this setup, 192.168.5.0, is the subnet and 255.255.255.0 is the subnet mask. The IP address range starts from 192.168.5.2 right through 192.168.5.253.
- option routers : defines the default gateway. In this case, 192.168.5.1.
- option subnet-mask : determines the subnet-mask assignment to each client or host. In this case, 255.255.255.0.
- domain-name-servers : specifies the DNS servers. In this case 192.168.5.1.
Configure the Firewall services for DHCP
sudo firewall-cmd --add-service=dhcp --permanent
sudo firewall-cmd --reload
Now we can restart the DHCP services and will enable the service to be persistent across the reboot
sudo systemctl start dhcpd.service
sudo systemctl enable dhcpd.service
This was one of the method of setting and configuring the DHCP server. Let us look another method of setting DHCP using the dnsmasq
DHCP server Using dnsmasq
DNS caching, DHCP, DNS forwarding, and other services are all offered by the open-source Dnsmasq DHCP server and DNS forwarder. For small-scale installations in a small network, this tool serves as the DHCP. Let us install the packages
sudo dnf install dnsmasq -y
Now the package is installed let us open the default configuration file for further configuration
sudo vim /etc/dnsmasq.conf
Replace the default content of the file with the below lines. Chose the interface as per your environment in the file below
interface=enp0s3
dhcp-range=192.168.5.3,192.168.5.253,24h dhcp-option=option:router,192.168.5.1 dhcp-option=option:dns-server,192.168.5.1
Once you’re done setting up Dnsmasq, test its configuration to make sure your changes are valid:
sudo dnsmasq --test
If the configuration is correct you see something like this
dnsmasq: syntax check OK.
Finally restart the service followed by the enabling it
sudo systemctl start dnsmasq
sudo systemctl enable dnsmasq
Test DHCP Server using Client
We will now check the DHCP IP assignment on a client within our own LAN. Let us start with installing the dhcp-client
sudo dnf -y install dhcp-client
We can now use dhclient command to request for an IP address from the DHCP Server
$ sudo dhclient <interface>
You will see the expected output as below
$ sudo dhclient eth0
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 46:74:00:10:4b:93 brd ff:ff:ff:ff:ff:ff
inet 192.168.5.5/24 brd 192.168.5.255 scope global noprefixroute dynamic eth0
valid_lft 3594sec preferred_lft 3594sec
You can verify this by going back to the dhcp server and search the client machine ip address in /var/log/message file
$ sudo tail -50 /var/log/messages | grep -i 192.168.5.5
Conclusion
This concludes our discussion of the easy methods for installing and configuring DHCP on Rocky Linux 9. For a change, we have included the dnsmasq method for configuring DHCP, as it also supports a DHCP server. Additionally, we advise you to update the configuration file with the information you now possess. If not, you might encounter a problem while trying to activate the dhcpd.service on the computer. For more info you may also refer to the official Website ISC DHCP