Dynamic Host Configuration Protocol DHCP Server | Linux.
A standard Internet protocol that enables the dynamic configuration of hosts on an Internet Protocol (IP) internetwork. Dynamic Host Configuration Protocol (DHCP) is an extension of the bootstrap protocol (BOOTP).
How DHCP Works?
DHCP is a client-server protocol that uses DHCP servers and DHCP clients. A DHCP server is a machine that runs a service that can lease out IP addresses and other TCP/IP information to any client that requests them. For example, on Linux System example Ubuntu you can install the DHCP Server service to perform this function.
The DHCP server typically has a pool of IP addresses that it is allowed to distribute to clients, and these clients lease an IP address from the pool for a specific period of time, usually several days. Once the lease is ready to expire, the client contacts the server to arrange for renewal.
DHCP clients are client machines that run special DHCP client software enabling them to communicate with DHCP servers. All versions of Linux and Windows include DHCP client software, which is installed when the TCP/IP protocol stack is installed on the machine.
DHCP clients obtain a DHCP lease for an IP address, a subnet mask, and various DHCP options from DHCP servers in a four-step process:
1. DHCP DISCOVER:
The client broadcasts a request for a DHCP server.
2. DHCP OFFER:
DHCP servers on the network offer an address to the client.
3. DHCP REQUEST:
The client broadcasts a request to lease an address from one of the offering DHCP servers.
4. DHCPACK:
The DHCP server that the client responds to acknowledges the client, assigns it any configured DHCP options, and updates its DHCP database. The client then initializes and binds its TCP/IP protocol stack and can begin network communication.
Setting Up a DHCP Server
Assuming you have already set up the physical connections between your DHCP server and the client computers on your network (presumably an Ethernet LAN), the minimum tools you need to get the DHCP server working are:
- A firewall that allows DHCP access .
- A configured /etc/dhcpd.conf file.
- A running dhcpd server daemon (which can be started at boot time).
After the DHCP server is running, it broadcasts its availability as a DHCP server to the LAN. A client simply boots up (with an Ethernet network interface turned on and DHCP identified as its method of getting network addresses), and the information it needs to get up and running on the network is fed to it from the server.
Step 1: Install DHCP Server on CentOS 8 / RHEL 8
Install DHCP server package using the dnf installer.
sudo yum -y install dhcp-server
This will install any dependency required to run DHCP Server on CentOS 8 / RHEL 8.
Step 2: Configure DHCP Server on CentOS 8 / RHEL 8
Edit the DHCP server configuration file on CentOS 8 / RHEL 8.
sudo vi /etc/dhcp/dhcpd.conf
Configuration file will be populated with these parameters:
- Domain name: example.com
- DNS Server: ns1.example.com
- DHCP network: 192.168.20.0
- DHCP Subnet mask: 255.255.255.0
- Range of IP addresses to allocate: 192.168.20.30 – 192.168.20.200
- Default gateway: 192.168.20.1
- DHCP Lease Time: 600
- DHCP Maximum Lease Time: 7200
The DHCP server configuration file looks like this:
# Set DNS name and DNS server's IP address or hostname
option domain-name "example.com";
option domain-name-servers ns1.example.com;
# Declare DHCP Server
authoritative;
# The default DHCP lease time in seconds
default-lease-time 600;
# Set the maximum lease time in seconds max-lease-time 7200;
# Set Network address, subnet mask and gateway subnet 192.168.20.0 netmask 255.255.255.0 {
# Range of IP addresses to allocate range dynamic-bootp 192.168.20.30 192.168.20.200;
# Provide broadcast address
option broadcast-address 192.168.20.255;
# Set default gateway
option routers 192.168.20.1;
}
host myserver {
hardware ethernet 00:50:56:8c:20:fd;
fixed-address 192.168.20.100;
}
Start and enable the dhcpd service after making the changes in the configuration file.
sudo systemctl enable --now dhcpd
sudo systemctl start --now dhcpd
If you have firewalld running, allow the service port to be accessible from your network.
sudo firewall-cmd --add-service=dhcp --permanent
sudo firewall-cmd --reload
Step 3: How to check if DHCP is working or not on server and Leased ip addresses
Check the /var/lib/dhcp/dhchpd.leases file. If the client has successfully been assigned addresses from the DHCP server, a lease line should appear in that file. There should be one set of information that looks like the following for each client that has leased an ip address.
Step 4: Configure DHCP Client
Install DHCP client in your Linux machine to get an IP address automatically.
CentOS 8 / RHEL 8 / Fedora
$ sudo yum -y install dhcp-client
CentOS 7/6
$ sudo yum -y install dhcp-client
Step 5: How to check for ip address in client machine
We can use ifconfig command to check for the ip address on our machine.
[deep@localhost ~]$ ifconfig or ip address show
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
mtu 1500 inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
To know that ip address is assigned from DHCP server use following command
[deep@localhost ~]$ ip r
default via 10.0.2.2 dev enp0s3 proto dhcp metric 100
10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 metric 100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown
Or
[deep@localhost ~]$ nmtui
DHCP CONFIGURATION
Platform
We are going to use Centos 8 for server and Ubuntu 20.04.3 for client configuration.
DHCP CONCEPT
DHCP CONCEPT
● Domain name: example.com
● DNS Server: ns1.example.com
● DHCP network: 192.168.20.0
● DHCP Subnet mask: 255.255.255.0
● Range of IP addresses to allocate: 192.168.20.30 – 192.168.20.200
● Default gateway: 192.168.20.1
● DHCP Lease Time: 600
● DHCP Maximum Lease Time: 7200
● Fixed IP address to computer with MAC: 00:50:56:8c:20:fd ; 192.168.20.100
DHCP Platform
Step 1: Install DHCP Server on CentOS.
Step 2: Configure DHCP sever.
Step 3: Enable and start DHCP services.
Step 4: Allow port 67,68 on firewall.
Step 5: Client side configuration
Step 6: Check for leased ip address
Install DHCP Server on CentOS.
sudo yum -y install dhcp-server
Configuring DHCP Server
Edit following configuration file:
sudo vi /etc/dhcp/dhcpd.conf
Configuring DHCP Server
option domain-name “techpaper.com";
option domain-name-servers ns1.techpaper.com;
authoritative;
default-lease-time 600;
max-lease-time 7200;
subnet 192.168.20.0 netmask 255.255.255.0 {
range dynamic-bootp 192.168.20.30 192.168.20.200;
option broadcast-address 192.168.20.255;
option routers 192.168.20.1;
}
host myserver {
hardware ethernet 00:50:56:8c:20:fd;
fixed-address 192.168.20.100;
}
Enable DHCP Service
sudo systemctl start --now dhcpd
sudo systemctl enable --now dhcpd
Allow connection through Firewall
If your machine have firewall enable allow dhcp service.
sudo firewall-cmd --add-service=dhcp --permanent
sudo firewall-cmd --reload
Check for Leased IP address
If the client has successfully been assigned addresses from the DHCP server /var/lib/dhcp/dhchpd.leases , a lease line should appear in that file. There should be one set of information that looks like the following for each client that has leased an ip address.
Configure DHCP Client
By default dhcp-client package is already installed. Below is the command to install
$ sudo yum -y install dhcp-client
Use ifconfig command to check for the ip address on our machine.
[deep@localhost ~]$ ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
To check if this IP address is assigned from DHCP server use following command
[deep@localhost ~]$ ip r
default via 10.0.2.2 dev enp0s3 proto dhcp metric 100