How to setup DHCP Server in Linux 9

Share on Social Media

Discover the step-by-step process to setup DHCP Server in Linux 9 effortlessly. Empower your network infrastructure with dynamic IP address allocation, streamlining device connectivity and management. #centlinux #linux #dhcp

YouTube player

What is 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.

How does a DHCP server Work?

A DHCP (Dynamic Host Configuration Protocol) server works by dynamically assigning IP addresses and other network configuration parameters to devices on a network. Here’s how it typically operates:

  1. Client Request: When a device (a client) connects to a network, it initially does not have an IP address or other network configuration settings. It sends out a broadcast message called a DHCPDISCOVER message to discover DHCP servers available on the network.
  2. DHCP Offer: DHCP servers on the network receive the DHCPDISCOVER message and respond with a DHCPOFFER message. This message contains an available IP address and other network configuration parameters, such as subnet mask, default gateway, DNS servers, and lease duration.
  3. Client Request Acceptance: The client receives one or more DHCPOFFER messages from different DHCP servers and selects one offer to accept. It sends a DHCPREQUEST message to the chosen DHCP server, indicating its acceptance of the offered configuration.
  4. Assignment and Acknowledgment: The DHCP server that received the DHCPREQUEST message acknowledges the client’s request by sending a DHCPACK message. This message confirms the assignment of the IP address and other configuration parameters to the client.
  5. Configuration Renewal: The client uses the assigned IP address and network configuration parameters to communicate on the network. Periodically, it will attempt to renew its lease on the IP address by sending a DHCPREQUEST message to the DHCP server. If the DHCP server agrees to renew the lease, it responds with a DHCPACK message, extending the lease duration. If the lease expires without renewal, the IP address is returned to the pool of available addresses for reassignment.

Overall, DHCP simplifies network administration by automating the process of IP address allocation and configuration, allowing for efficient management of IP addresses and network resources. It is commonly used in both small and large networks to streamline device connectivity and maintenance.

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
  • 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
# echo 192.168.116.5 dhcp-01 dhcp-01.centlinux.com >> /etc/hosts

Install & Setup DHCP Server in 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

Final Thoughts

Mastering to setup DHCP Server in Linux 9, opens up a realm of possibilities for network management. By dynamically allocating IP addresses, you streamline connectivity and simplify device management, enhancing the efficiency and scalability of your network infrastructure. If you are new to Linux command-line then we suggest that you should attend online training: Linux command line for beginnersshow?id=oLRJ54lcVEg&bids=1074652

Leave a Comment