Step by step Linux DHCP server configuration in Redhat / Centos / Fedora
In this article we will cover a very interesting and important topic, i.e. H. The Linux DHCP server configuration. Linux DHCP Server stands for Dynamic Host Configuration Protocol is used to dynamically OR automatically provide the IP address to client computers. The port number of the DHCP server is 67 and the port number of the DHCP client is 68.
Centos / Fedora” src=”data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7″ data-src=”https://www.itsmarttricks.com/wp-content/uploads/2019/10/Step-By-Step-Linux-DHCP-Server-Configuration-In-Redhat.png” alt=”Step by step Linux DHCP server configuration in Redhat / Centos / Fedora” width=”783″ height=”438″ data-srcset=”https://www.itsmarttricks.com/wp-content/uploads/2019/10/Step-By-Step-Linux-DHCP-Server-Configuration-In-Redhat.png 783w, https://www.itsmarttricks.com/wp-content/uploads/2019/10/Step-By-Step-Linux-DHCP-Server-Configuration-In-Redhat-768×430.png 768w” data-sizes=”(max-width: 783px) 100vw, 783px”>
Step by step Linux DHCP server configuration in Redhat / Centos / Fedora
Where should we use DHCP servers?
Suppose we have a small office with three computers and a router. The router’s IP address is 192.168.1.1 and we are assigned the IP address so that three computers are left. In this case, we can manually assign IP addresses to each computer. Imagine working on an extensive setup with thousands of client computers, multiple network printers and so many other network devices. In such situations, it is simply impossible to manually assign IP addresses. In such a situation, we need a Linux DHCP server. The DHCP server dynamically or automatically assigns the IP address to all systems and devices.
How does the DHCP server work? OR What is DORA Process?
- If a new system is connected to the network, the first step is to look for a DHCP server in the network and if it does not find an IP address, the system sends a DHCP detection message to all devices that are connected to the network. This process is called broadcasting.
- When the DHCP server receives the DHCP detection message from the client computer, the DHCP server offers all connected devices an IP address (e.g. 192.168.1.3) and sends this via broadcast.
- When the client computer now receives the IP address, it sends a request on the network that it accepts the IP address.
- The DHCP server then confirms the IP address that the client computer can now use. This is how Linux DHCP Server works. This whole process is called DORA (Discover Offer Request Acknowledgment).
Like other services such as BIND DNS Server and NFS Server, DHCP is also a server and client type. So here, too, we need a server and a client to configure the DHCP server. Details on DHCP servers and clients are listed below:
DHCP Server Details:
Server Name: dhcpserverIP address : 192.168.0.100MAC address :
DHCP Client Details:
Host Name: client1MAC address :
Important parameters of the Linux DHCP server
- Domain name option: Mention the domain name e.g .: itsmarttricks.com
- Domain name server option: Mention DNS servers e.g: 192.168.0.100, 192.168.0.101 (Note: You can also specify the fully qualified domain name (FQDN).
- Default lease time: The default time in seconds to when the DHCP server assigns an IP to the client computer.
- max-lease-time: The maximum time in seconds until the DHCP server assigns an IP to the client computer.
- subnet: Mention the subnet’s IP address e.g .: 192.168.0.0
- Network mask: Mention the subnet mask e.g .: 255.255.255.0
- Offer: Mention the IP range that is dynamically assigned to client computers by the Linux DHCP server. e.g .: 192.168.0.2 to 192.168.0.240
- Router option: Mention the gateway IP address e.g .: 192.168.0.1
- Broadcast address option: Mention your broadcast address e.g .: 192.168.0.255
- Hardware Ethernet: Mention your MAC access OR your physical address e.g .: 00: 0C: 29: F7: BE: 27
- Hostname option: Your system’s hostname OR computer name e.g .: dhcpserver
Follow the steps below to gradually configure the Linux DHCP server in Redhat / Centos / Fedora:
Step: 1 Prepare your server before configuring the DHCP server
Before we start configuring the DHCP server, we need to prepare our server for it.
First assign a static IP address to your server. See the following command.
[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 # Set Static IP Address DEVICE=eth0 BOOTPROTO=none HWADDR=00:0c:29:41:6a:ae IPV6INIT=yes NM_CONTROLLED=yes ONBOOT=yes TYPE=Ethernet UUID="2172489a-2137-49f3-8389-6e616d9d6ca1" IPADDR=192.168.0.100 NETMASK=255.255.255.0 GATEWAY=192.168.0.1 DNS1=192.168.0.100 USERCTL=no DNS2=192.168.0.101
Give your server a correct host name. Here is my server’s hostname dhcpserver,
[[email protected] ~]# hostname # Checking Hostname dhcpserver.itsmarttricks.com
Step: 2 Install the required packages
Install the necessary packages and dependencies for Linux DHCP Server.
[[email protected] ~]# yum -y install dhcp* # Install Linux DHCP Server Package Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: centos.excellmedia.net * extras: mirror.fibergrid.in * updates: mirrors.aluhost.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package dhcp.x86_64 12:4.1.1-53.P1.el6.centos will be installed ---> Package dhcp-common.x86_64 12:4.1.1-38.P1.el6.centos will be updated --> Processing Dependency: dhcp-common = 12:4.1.1-38.P1.el6.centos for package: 12:dhclient-4.1.1-38.P1.el6.centos.x86_64 ---> Package dhcp-common.x86_64 12:4.1.1-53.P1.el6.centos will be an update ---> Package dhcp-devel.x86_64 12:4.1.1-53.P1.el6.centos will be installed --> Running transaction check ---> Package dhclient.x86_64 12:4.1.1-38.P1.el6.centos will be updated ---> Package dhclient.x86_64 12:4.1.1-53.P1.el6.centos will be an update --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================================== Installing: dhcp x86_64 12:4.1.1-53.P1.el6.centos base 823 k dhcp-devel x86_64 12:4.1.1-53.P1.el6.centos base 158 k Updating: dhcp-common x86_64 12:4.1.1-53.P1.el6.centos base 144 k Updating for dependencies: dhclient x86_64 12:4.1.1-53.P1.el6.centos base 322 k Transaction Summary ============================================================================================================================================================== Install 2 Package(s) Upgrade 2 Package(s) Total download size: 1.4 M Downloading Packages: (1/4): dhclient-4.1.1-53.P1.el6.centos.x86_64.rpm | 322 kB 00:00 (2/4): dhcp-4.1.1-53.P1.el6.centos.x86_64.rpm | 823 kB 00:00 (3/4): dhcp-common-4.1.1-53.P1.el6.centos.x86_64.rpm | 144 kB 00:00 (4/4): dhcp-devel-4.1.1-53.P1.el6.centos.x86_64.rpm | 158 kB 00:00 -------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 954 kB/s | 1.4 MB 00:01 warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 Importing GPG key 0xC105B9DE: Userid : CentOS-6 Key (CentOS 6 Official Signing Key)Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Updating : 12:dhcp-common-4.1.1-53.P1.el6.centos.x86_64 1/6 Installing : 12:dhcp-4.1.1-53.P1.el6.centos.x86_64 2/6 Installing : 12:dhcp-devel-4.1.1-53.P1.el6.centos.x86_64 3/6 Updating : 12:dhclient-4.1.1-53.P1.el6.centos.x86_64 4/6 Cleanup : 12:dhclient-4.1.1-38.P1.el6.centos.x86_64 5/6 Cleanup : 12:dhcp-common-4.1.1-38.P1.el6.centos.x86_64 6/6 Verifying : 12:dhcp-common-4.1.1-53.P1.el6.centos.x86_64 1/6 Verifying : 12:dhcp-devel-4.1.1-53.P1.el6.centos.x86_64 2/6 Verifying : 12:dhcp-4.1.1-53.P1.el6.centos.x86_64 3/6 Verifying : 12:dhclient-4.1.1-53.P1.el6.centos.x86_64 4/6 Verifying : 12:dhcp-common-4.1.1-38.P1.el6.centos.x86_64 5/6 Verifying : 12:dhclient-4.1.1-38.P1.el6.centos.x86_64 6/6 Installed: dhcp.x86_64 12:4.1.1-53.P1.el6.centos dhcp-devel.x86_64 12:4.1.1-53.P1.el6.centos Updated: dhcp-common.x86_64 12:4.1.1-53.P1.el6.centos Dependency Updated: dhclient.x86_64 12:4.1.1-53.P1.el6.centos Complete!
You can use the following command to check whether packages have been installed correctly or not.
[[email protected] ~]# rpm -qa | grep dhcp # Confirm the Installed DHCP Server Package dhcp-4.1.1-53.P1.el6.centos.x86_64 dhcp-common-4.1.1-53.P1.el6.centos.x86_64 dhcp-devel-4.1.1-53.P1.el6.centos.x86_64
After installation, check the installation directory of the Linux DHCP server and the path of all configuration files. We can do this with the following command.
[[email protected] ~]# rpm -ql dhcp # Check the Path of Installation Directories and Configuration Files /etc/dhcp # Installation Directory /etc/dhcp/dhcpd.conf # Main Configuration file of DHCP Server /etc/dhcp/dhcpd6.conf /etc/openldap/schema/dhcp.schema /etc/portreserve/dhcpd /etc/rc.d/init.d/dhcpd /etc/rc.d/init.d/dhcpd6 /etc/rc.d/init.d/dhcrelay /etc/rc.d/init.d/dhcrelay6 /etc/sysconfig/dhcpd /etc/sysconfig/dhcpd6 /etc/sysconfig/dhcrelay /etc/sysconfig/dhcrelay6 /usr/bin/omshell /usr/sbin/dhcpd # Binary Files /usr/sbin/dhcrelay /usr/share/doc/dhcp-4.1.1 /usr/share/doc/dhcp-4.1.1/3.0b1-lease-convert /usr/share/doc/dhcp-4.1.1/IANA-arp-parameters /usr/share/doc/dhcp-4.1.1/README.ldap /usr/share/doc/dhcp-4.1.1/api+protocol /usr/share/doc/dhcp-4.1.1/dhclient-tz-exithook.sh /usr/share/doc/dhcp-4.1.1/dhcpd-conf-to-ldap /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /usr/share/doc/dhcp-4.1.1/dhcpd6.conf.sample /usr/share/doc/dhcp-4.1.1/draft-ietf-dhc-ldap-schema-01.txt /usr/share/doc/dhcp-4.1.1/ms2isc /usr/share/doc/dhcp-4.1.1/ms2isc/Registry.perlmodule /usr/share/doc/dhcp-4.1.1/ms2isc/ms2isc.pl /usr/share/doc/dhcp-4.1.1/ms2isc/readme.txt /usr/share/doc/dhcp-4.1.1/sethostname.sh /usr/share/doc/dhcp-4.1.1/solaris.init /usr/share/man/man1/omshell.1.gz /usr/share/man/man5/dhcpd.conf.5.gz /usr/share/man/man5/dhcpd.leases.5.gz /usr/share/man/man8/dhcpd.8.gz /usr/share/man/man8/dhcrelay.8.gz /var/lib/dhcpd /var/lib/dhcpd/dhcpd.leases # Linux DHCP Server Lease File /var/lib/dhcpd/dhcpd6.leases
So the installation directory of DHCP Server is / etc / dhcp and is the main configuration file of Linux DHCP Server dhcpd.conf, By default, the dhcpd.conf file looks like this. As you can see below, there is a message for us that we can copy the sample file dhcpd.conf / usr / share / doc / dhcp * Directory.
[[email protected] ~]# cat /etc/dhcp/dhcpd.conf # # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.sample # see 'man 5 dhcpd.conf' #
Step: 3 Prepare the configuration file
So let’s go ahead and copy that dhcpd.conf.sample File as dhcpd.conf, The system prompts to overwrite because we already have the file dhcpd.conf in the path / etc / dhcp. So just come in Yes and press Enter to continue copying.
[[email protected] ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf cp: overwrite `/etc/dhcp/dhcpd.conf'? yes
Step: 4 Configure the Linux DHCP server
Now let’s configure the Linux DHCP server. Just edit that / etc / dhcp / dhcpd / conf File with your favorite text editor.
The configurations shown below are global variables and apply to all subnets that we declare in the dhcpd.conf file. Simply edit the configuration file and make the changes according to your scenario.
option domain-name "itsmarttricks.com"; option domain-name-servers 192.168.0.100, 192.168.0.101; default-lease-time 600; max-lease-time 7200; # Use this to enble / disable dynamic dns updates globally. #ddns-update-style none; # If this DHCP server is the official DHCP server for the local # network, the authoritative directive should be uncommented. authoritative;
Then declare a subnet according to your network scenario. Here I take the IP address from 192.168.0 and subnet as 255.255.255.0
Example output:
subnet 192.168.0.0 netmask 255.255.255.0 { range 192.168.0.5 192.168.0.240; # IP Address Range option domain-name-servers 192.168.0.100, 192.168.0.101; # DNS Servers option domain-name "itsmarttricks.com"; # Domain Name option routers 192.168.0.1; # Gateway Address option broadcast-address 192.168.0.255; default-lease-time 600; max-lease-time 7200; }
Where :
The range applies to the permissible IP address range, i. H. 192.168.0.5 to 192.168.0.240, The DHCP server assigns an IP address to the client computers in this area. And other settings are common, such as the domain name, the router address, i.e. H. The gateway address, the DNS server, etc., which I have already explained above.
After making any necessary changes to the dhcpd.conf configuration file, the Linux DHCP server service is started with the following command.
[[email protected] ~]# /etc/init.d/dhcpd start # Start the DHCP Server Service Starting dhcpd: [ OK ]
Configure the DHCP service to start at system startup.
[[email protected] ~]# chkconfig --level 35 dhcpd on # Start DHCP Service at Startup [[email protected] ~]# chkconfig --list dhcpd # Confirm the Startup Configuration dhcpd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
DHCP client side configuration
Configure the Ethernet connection from the client to get the IP address from the DHCP server. Simply edit the ifcfg-ethX store and adjust BOOTPROTO = dhcp (Highlighted in red). See the following sample output.
Note: Replace X with your ethernet connection number
[[email protected] ~]# nano /etc/sysconfig/network-scripts/ifcfg-eth1 # Configure Ethernet Connection HWADDR=00:0C:29:F7:BE:27 TYPE=Ethernet BOOTPROTO=dhcp DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="eth1" UUID=51af58db-2b74-4b31-9016-2e8682cafcb4 ONBOOT=yes LAST_CONNECT=1495972689
Then restart the network service with the following command.
[[email protected] ~]# /etc/init.d/network restart # Restart the Network Service
After the restart, the network service system receives the IP address from the Linux DHCP server. To confirm the same, we can use ifconfig Command to check the IP address. See the following command.
[[email protected] ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:F7:BE:27 inet addr:192.168.0.5 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fef7:be27/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:39 errors:0 dropped:0 overruns:0 frame:0 TX packets:121 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5184 (5.0 KiB) TX bytes:7917 (7.7 KiB)
As soon as the client receives an IP address from the DHCP server, you can see all client information such as the assigned IP address, MAC address and the name of the client computer /var/lib/dhcpd.leases Server.
Note : Leasing information for IP addresses is stored in /var/lib/dhcpd.leases File. By default, it is supplied with the installation of the DHCP server package. If this is not the case, you have to create it manually, otherwise you will not be able to start the DHCP service.
[[email protected] ~]# cat /var/lib/dhcpd/dhcpd.leases # Checking the dhcpd.leases file # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-4.1.1-P1 server-duid " 00 01 00 01 275v261 00 14)Aj256"; lease 192.168.0.5 { starts 0 2017/05/28 12:02:32; ends 0 2017/05/28 12:12:32; cltt 0 2017/05/28 12:02:32; binding state active; next binding state free; hardware ethernet 00:0c:29:f7:be:27; client-hostname "client1"; }
Extended functions in Linux DHCP Server
Another useful feature that you can configure in Linux DHCP Server is to reserve the IP address for a specific user using their MAC address (physical address). Usually, the DHCP server gives the client an IP address for a certain time. When you restart the system, DHCP may assign a different IP address.
The advantage of IP reservation is that the IP address never changes. In this case, suppose you have a network printer that is configured in so many client systems. If your IP address changes frequently, users cannot use the printer. To avoid this, you need to reserve an IP address for this printer in Linux DHCP Server.
Here I have reserved an IP address for one of my client computers, i. H. client1, See the following sample output.
### host declaration ### host client1 { option host-name "client1"; # Hostname of the Client Computer hardware ethernet 00:0c:29:f7:be:27; # MAC Address of the Client Computer fixed-address 192.168.0.130; # IP Address You want to assign to that Client }
After making the necessary changes, restart the DHCP service for the changes to take effect.
[[email protected] ~]# /etc/init.d/dhcpd restart # Restart the DHCP Service Shutting down dhcpd: [ OK ] Starting dhcpd: [ OK ]
Then restart the network service from the DHCP client and you will receive the reserved IP address. See the following sample output.
[[email protected] ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:F7:BE:27 inet addr:192.168.0.130 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fef7:be27/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:56 errors:0 dropped:0 overruns:0 frame:0 TX packets:174 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:8534 (8.3 KiB) TX bytes:11943 (11.6 KiB)
Here I mentioned my dhcpd.conf configured file for your reference.
# Configure Global Variables which are common for all Declared Subnets & will applied to all Subnets option domain-name "itsmarttricks.com"; option domain-name-servers 192.168.0.100, 192.168.0.101; default-lease-time 600; max-lease-time 7200; authoritative; log-facility local7; # Declare a Subnet with IP Range subnet 192.168.0.0 netmask 255.255.255.0 { range 192.168.0.5 192.168.0.240; option domain-name-servers 192.168.0.100, 192.168.0.101; option domain-name "itsmarttricks.com"; option routers 192.168.0.1; option broadcast-address 192.168.0.255; default-lease-time 600; max-lease-time 7200; } # Host Declaration for IP Address Reservation host client1 { option host-name "client1"; hardware ethernet 00:0c:29:f7:be:27; fixed-address 192.168.0.130; }
Read also – How to configure vsftpd Linux FTP server in Redhat / Centos / Fedora
That’s all. In this article we have explained step by step Linux DHCP server configuration In Redhat / Centos / Fedora. I hope you like this article. If you like this article, just share it. If you have any questions about this article, please comment.