How to Configure DHCP Server on Linux 9

Share on Social Media

In this Linux tutorial, you will learn how to configure DHCP server on Linux 9 that includes all Red Hat based Linux distros. #centlinux #linux #dhcp

What is a DHCP Server? :

DHCP (Dynamic Host Configuration Protocol) server is a network server that automatically assigns IP addresses, subnet masks, default gateways, and other network parameters to client devices on a network.

When a client device connects to a network that is configured to use DHCP, it sends a broadcast request to the network requesting an IP address. The DHCP server responds to the request by assigning an IP address and other network settings to the client device. This allows the client device to communicate with other devices on the network and access the internet.

DHCP simplifies network administration by eliminating the need for network administrators to manually assign IP addresses to client devices. It also allows for easier management of IP address assignments and reduces the risk of conflicting IP addresses on the network.

Video to Configure DHCP Server on Linux:

YouTube player

Environment Specification:

We are using a minimal installed Rocky Linux 9 operating system with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating SystemRocky Linux release 9.1 (Blue Onyx)
  • Hostname – dhcp-01.centlinux-com.preview-domain.com
  • IP Address – 192.168.116.128/24

Updating Software Packages:

Login to your Rocky Linux server as root user by using a ssh client.

Execute following command to update software packages in your Linux operating system.

# dnf update -y

If above command updates a software package related to your Linux kernel, then you should reboot your Linux server with new Kernel.

# reboot

Note down the versions of Linux Kernel and Rocky Linux operating system, that are being used in this tutorial.

# uname -r
5.14.0-162.23.1.el9_1.x86_64

# cat /etc/rocky-release
Rocky Linux release 9.1 (Blue Onyx)

Bash Completion is a very handy software for auto-completion of Linux commands, especially for nmcli command.

Therefore, you should install bash-completion before configuring your DHCP server.

# dnf install -y bash-completion
# source /etc/profile.d/bash_completion.sh

Set Static IP Address:

Check status of your current network connection.

# ip -4 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    altname enp3s0
    inet 192.168.116.128/24 brd 192.168.116.255 scope global dynamic noprefixroute ens160
       valid_lft 1676sec preferred_lft 1676sec

We are working on a virtual machine and the integrated DHCP server of the vmware host is assigning a dynamic IP address to our guest machine.

You need to set a static IP Address for your DHCP server. You can use following nmcli command to set IP Address, Gateway and DNS for your network connection.

# nmcli c m ens160 ipv4.method manual ipv4.addresses 192.168.116.5/24 ipv4.gateway 192.168.116.2 ipv4.dns 192.168.116.2

Reload your network connection to apply changes.

# nmcli c down ens160 ; nmcli c up ens160

Now, again check the status of your network connection.

# ip -4 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    altname enp3s0
    inet 192.168.116.5/24 brd 192.168.116.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever

Set Hostname & Local DNS resolution by executing following commands at Linux terminal.

# hostnamectl set-hostname dhcp-01.centlinux-com.preview-domain.com
# echo 192.168.116.5 dhcp-01 dhcp-01.centlinux-com.preview-domain.com >> /etc/hosts

Install & Configure DHCP Server on Linux:

You can install Linux based DHCP server i.e. dhcpd by using dnf command.

# dnf install -y dhcp-server

Edit dhcpd configuration file by using vim text editor.

# vi /etc/dhcp/dhcpd.conf

The dhcpd configuration file is empty by default, however it provides a path to a sample configuration file for your reference.

Add following directives in this file.

default-lease-time 900;
max-lease-time 10800;

authoritative;

subnet 192.168.116.0 netmask 255.255.255.0 {
range 192.168.116.50 192.168.116.200;
option routers 192.168.116.2;
option subnet-mask 255.255.255.0;
option domain-name-servers 192.168.116.2;
}

The above directives are quiet enough to create a production grade DHCP server.

Feel free to adjust above parameters according to your environment.

Enable and start dhcpd service.

# systemctl enable --now dhcpd.service

You need to allow dhcp service in Linux firewall to receive DHCP client requests.

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

Conclusion:

In this Linux tutorial, you have learned how to configure DHCP Server on Linux 9 and other Red Hat based Linux distros. If you are new to Linux command-line then we suggest that you should attend online training: Linux command line for beginners