STEP BY STEP LINUX DHCP SERVER CONFIGURATION IN REDHAT/CENTOS/FEDORA
Introduction
In this article we are going to discuss on a very intresting and important topic i.e. DHCP Server Configuration. Linux DHCP Server stands for Dynamic Host Configuration Protocol is used to provide IP Address to client Computers Dynamically OR Automatically. The Port Number of DHCP Server is 67 and The Port Number of DHCP Client is 68.
Where Should we use DHCP Server ?
Suppose we have a small office with three computers with a router. Ip Address of the Router is 192.168.1.1 and We have a task to assign IP Address to remaining three computers. In that case we can go and assign IP Address to each and every computer manually. But just Imagine You are working on a large setup with Thousands of Client Computers, Multiple Network Printers and so many other networking devices. In such kind of situating it’s just impossible to assign IP Addresses manually. In such situation we need Linux DHCP Server. DHCP Server will assign the IP Address to all systems and devices dynamically or automatically.
How DHCP Server Works ? OR What is DORA Process ?
- When a new system connected to Network, As a first step he searches for a DHCP Server in Network and when he unable to found the same as he is currently don’t have any IP Address, the system sends a DHCP Discover message to all devices which are connected to network. This process is called as Broadcasting.
- When DHCP Server receives the DHCP Discover message from the Client Computer, DHCP Server offers and broadcast’s a IP Address (Eg: 192.168.1.3) to all connected devices.
- Now when Client Computer receives the IP Address he broadcasts a request on network that he accepts the IP Address.
- Then DHCP Server Acknowledge the IP Address that Now Client Computer can use the IP Address. This is how Linux DHCP Server Works. This Whole process is called as DORA (Discover Offer Request Acknowledgement).
Like other Services Like BIND DNS Server, NFS Server, DHCP is also a Server & client type of Server. So Here also we need a Server and a Client to configure DHCP Server. DHCP Server & Client details are mentioned below :
DHCP Server Details :
Server Name : dhcpserver
IP Address : 192.168.0.100
MAC Address :
DHCP Client Details :
Hostname : Client1
MAC Address :
Important Parameters of Linux DHCP Server
- option domain-name : Mention Domain Name eg : elinuxbook.com
- option domain-name-servers : Mention DNS Servers eg: 192.168.0.100, 192.168.0.101 (Note : You can also mention the FQDN)
- default-lease-time : The Default time in Seconds till the time DHCP Server will assign a IP to Client Computer.
- max-lease-time : The Maximum time in Seconds till the time DHCP Server will assign a IP to Client Computer.
- subnet : Mention the Subnet IP Address eg : 192.168.0.0
- netmask : Mention the Subnet Mask eg : 255.255.255.0
- range : Mention the IP Range which will dynamically assigned by Linux DHCP Server to Client Computers. eg : 192.168.0.2 to 192.168.0.240
- option routers : Mention the Gateway IP Address eg : 192.168.0.1
- option broadcast-address : Mention your Broadcast Address eg : 192.168.0.255
- hardware ethernet : Mention your MAC Accress OR Physical Address eg : 00:0C:29:F7:BE:27
- option host-name : Your systems Hostname OR Computer Name eg : dhcpserver
Follow the Below Steps for Linux DHCP Server Configuration
Step : 1 Prepare your Server before DHCP Server Configuration
Before we start configuration of DHCP Server first we need to prepare our server for that.
First Assign a Static IP Address to your Server. Refer the command below.
[root@dhcpserver ~]# 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 a proper Hostname to your Server. Here my Server’s Hostname is dhcpserver.
[root@dhcpserver ~]# hostname # Checking Hostname
dhcpserver.elinuxbook.com
Step : 2 Install Required Packages
Install required packages and dependencies for Linux DHCP Server.
[root@localhost ~]# 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) <centos-6-key@centos.org>
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 confirm if packages are properly installed or not by using below command.
[root@localhost ~]# 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 let’s check what is the Installation directory of Linux DHCP Server and Path of all configuration files. We can do so using below command.
[root@localhost ~]# 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 the main configuration file of Linux DHCP Server is dhcpd.conf. By default the dhcpd.conf file would look like as shown below. As you can see below there is a message for us that we can copy the sample dhcpd.conf file from /usr/share/doc/dhcp* directory.
[root@localhost ~]# 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 the dhcpd.conf.sample file as dhcpd.conf. System will ask for Overwrite as we already have dhcpd.conf file at /etc/dhcp path. So just enter yes and Press enter to continue the Copy.
[root@localhost ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf cp: overwrite `/etc/dhcp/dhcpd.conf'? yes
Also Read :
- HOW TO CONFIGURE SLAVE DNS SERVER WITH BIND ( SECONDARY DNS SERVER ) IN LINUX
- HOW TO INSTALL AND CONFIGURE CACHING-ONLY DNS SERVER WITH BIND IN RHEL/CENTOS
- STEP BY STEP SSL CONFIGURATION IN APACHE WEB SERVER
Step : 4 Configure the Linux DHCP Server
Now let’s go ahead and configure the Linux DHCP Server. Just edit the /etc/dhcp/dhcpd/conf file by using your preferred text editor.
Below shown configurations are global variables and will apply to all subnet’s we declare on dhcpd.conf file. So just edit the configuration file and make changes as per your scenario.
option domain-name "elinuxbook.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 as per you network Scenario. Here I am taking IP Address starting from 192.168.0 and Subnet as 255.255.255.0
Sample 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 "elinuxbook.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 :
Range is for allowed IP Address Range i.e. 192.168.0.5 to 192.168.0.240. DHCP Server will assign IP Address to client computers within these range. And Other settings are common like Domain Name, Router Address i.e. Gateway Address, DNS Servers and so on which I already explained above.
After all required changes in dhcpd.conf configuration file start the Linux dhcp server service using below command.
[root@dhcpserver ~]# /etc/init.d/dhcpd start # Start the DHCP Server Service
Starting dhcpd: [ OK ]
Configure DHCP Service to start at system startup.
[root@dhcpserver ~]# chkconfig --level 35 dhcpd on # Start DHCP Service at Startup [root@dhcpserver ~]# 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 client side to get IP from DHCP Server. To do so just edit the ifcfg-ethX file and set BOOTPROTO=dhcp (Highlighted in Red color). Refer the sample output below.
Note : Replace X with your Ethernet Connection Number
[root@client1 ~]# 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 using below command.
[root@dhcpserver ~]# /etc/init.d/network restart # Restart the Network Service
Once you restart the Network Service system will get the IP Address from Linux DHCP Server. To confirm the same we can use ifconfig command to check the IP Address. Refer the command below.
[root@client1 ~]# 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)
Once client get IP Address from DHCP Server you can see all the client information like The assigned IP Address, MAC Address, Client Computer Name in /var/lib/dhcpd.leases Server.
Note : IP Address lease Informations are stored in /var/lib/dhcpd.leases file. By Default it comes with DHCP Server Package Installation but if it is not there then you have to create it manually otherwise you will unable to start the DHCP Service.
[root@dhcpserver ~]# 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 "\000\001\000\001 \275v\261\000\014)Aj\256"; 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"; }
Advance Features in Linux DHCP Server
One more useful feature you can configure in Linux DHCP Server is you can Reserve IP Address for some user using his MAC Address (Physical Address). Normally DHCP Server lease IP Address to client for some particular time. When you restart the system it is possible that DHCP will assign you a different IP Address.
The benefit of IP Reservation is the IP Address will never change. Suppose you have a Nework printer which is configured in so many client systems in that case if your IP Address change frequently then users will unable to use printer. For avoid such situation you have to reserve a IP Address for that printer in Linux DHCP Server.
Here I reserved a IP Address for one of my client computer i.e. client1. Refer the Sample Output below.
### 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 required changes just restart the dhcp service to changes take effect.
[root@dhcpserver ~]# /etc/init.d/dhcpd restart # Restart the DHCP Service
Shutting down dhcpd: [ OK ]
Starting dhcpd: [ OK ]
Then restart the Network Service from DHCP client side and you will get the Reserved IP Address. Refer the Sample Output below.
[root@client1 ~]# 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 "elinuxbook.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 "elinuxbook.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; }
If you found this article useful then Like Us, Share Us, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.
Hi,
DHCP Server has been configured dynamically, clients systems gets the ip addresses. Now, through DHCP Server, how I can list all the client systems informations like domain name and domain name servers ip’s. What is that command?, please say me the ans. If you are not getting my point, please get back to me via mail or mobile – 9071933731
Thanks & regards,
Subrata
Hi Subrata,
You can get all connected client informations from dhcpd lease file by using the below command from Server.
cat /var/lib/dhcpd/dhcpd.leases