DHCP Server in Linux(RHEL or CentOS) – Installation, Configuration, Interview Questions, Tips
DHCP(Dynamic Host Configuration Protocol)
1. What is DHCP Server ?
Dynamic Host Configuration Protocol (DHCP) automatically assigns network configuration information including IP addresses and other information like subnetmask, broadcast address, etc to computers on a network.
2. How it works ?
DHCP Client & Server works in four steps below.
a. A computer configured as DHCP client sends broadcast request “DHCPDiscover” in the network.
b. DHCP server accepts the request and reply back with “DHCPOffer” with range of IPs available.
c. Client then sends the “DHCPRequest” requesting an IP address.
d. Server acknowledges and sends the DHCPAck packet with IP assignment to client.
After a specific time defined also known as lease time, the client again sends the request to renew the IP address.
3. DHCP Client Configuration
First we will look at what configuration required to become dhcp client, thereafter we will check dhcp server config.
a. Package required.:- DHCP-common & dhclient package is required for dhcp client and server both which is generally installed by default.
[root@nglinux ~]# rpm -qa | grep -i dhcp dhcp-common-4.1.1-53.P1.el6.centos.i686
b. Become DHCP Client temporarily
# To configure all interfaces. [root@nglinux ~]# dhclient or, ### To configure a specific interface [root@nglinux ~]# dhclient eth0
c. To permanently configure DHCP.
Permanent DHCP configuration is defined on specific interface file(s).
We need to define bootproto to dhcp and onboot yes to enable it on system boot.
[root@nglinux ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth2 DEVICE="eth2" BOOTPROTO="dhcp" IPV6INIT="yes" ONBOOT="yes" [root@nglinux ~]#
d. Make sure networking is enabled.
[root@nglinux ~]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=nglinux [root@nglinux ~]#
4. DHCP Server Setup
After DHCP Client configuration, lets have a look how to configure DHCP Server.
a. First install DHCP server package.
[root@nglinux ~]# yum install dhcp Failed to set locale, defaulting to C Loaded plugins: auto-update-debuginfo, fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile epel/metalink | 16 kB 00:00 epel-debuginfo/metalink | 14 kB 00:00 * base: repos.lax.quadranet.com * epel: mirrors.develooper.com * epel-debuginfo: mirrors.develooper.com * extras: mirror.fileplanet.com * updates: repos-lax.psychz.net base | 3.7 kB 00:00 base-debuginfo | 2.5 kB 00:00 epel | 3.2 kB 00:00 epel-debuginfo | 1.5 kB 00:00 extras | 3.3 kB 00:00 updates | 3.4 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package dhcp.i686 12:4.1.1-61.P1.el6.centos will be installed --> Processing Dependency: dhcp-common = 12:4.1.1-61.P1.el6.centos for package: 12:dhcp-4.1.1-61.P1.el6.centos.i686 --> Running transaction check ---> Package dhcp-common.i686 12:4.1.1-53.P1.el6.centos will be updated --> Processing Dependency: dhcp-common = 12:4.1.1-53.P1.el6.centos for package: 12:dhclient-4.1.1-53.P1.el6.centos.i686 ---> Package dhcp-common.i686 12:4.1.1-61.P1.el6.centos will be an update --> Running transaction check ---> Package dhclient.i686 12:4.1.1-53.P1.el6.centos will be updated ---> Package dhclient.i686 12:4.1.1-61.P1.el6.centos will be an update --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================ Package Arch Version Repository Size ============================================================================================================ Installing: dhcp i686 12:4.1.1-61.P1.el6.centos updates 826 k Updating for dependencies: dhclient i686 12:4.1.1-61.P1.el6.centos updates 321 k dhcp-common i686 12:4.1.1-61.P1.el6.centos updates 145 k Transaction Summary ============================================================================================================ Install 1 Package(s) Upgrade 2 Package(s) Total download size: 1.3 M Is this ok [y/N]: y Downloading Packages: (1/3): dhclient-4.1.1-61.P1.el6.centos.i686.rpm | 321 kB 00:01 (2/3): dhcp-4.1.1-61.P1.el6.centos.i686.rpm | 826 kB 00:02 (3/3): dhcp-common-4.1.1-61.P1.el6.centos.i686.rpm | 145 kB 00:01 ------------------------------------------------------------------------------------------------------------ Total 192 kB/s | 1.3 MB 00:06 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Updating : 12:dhcp-common-4.1.1-61.P1.el6.centos.i686 1/5 Installing : 12:dhcp-4.1.1-61.P1.el6.centos.i686 2/5 Updating : 12:dhclient-4.1.1-61.P1.el6.centos.i686 3/5 Cleanup : 12:dhclient-4.1.1-53.P1.el6.centos.i686 4/5 Cleanup : 12:dhcp-common-4.1.1-53.P1.el6.centos.i686 5/5 Verifying : 12:dhcp-4.1.1-61.P1.el6.centos.i686 1/5 Verifying : 12:dhcp-common-4.1.1-61.P1.el6.centos.i686 2/5 Verifying : 12:dhclient-4.1.1-61.P1.el6.centos.i686 3/5 Verifying : 12:dhclient-4.1.1-53.P1.el6.centos.i686 4/5 Verifying : 12:dhcp-common-4.1.1-53.P1.el6.centos.i686 5/5 Installed: dhcp.i686 12:4.1.1-61.P1.el6.centos Dependency Updated: dhclient.i686 12:4.1.1-61.P1.el6.centos dhcp-common.i686 12:4.1.1-61.P1.el6.centos Complete! [root@nglinux ~]#
b. Now update DHCPD interface.
### DHCPD by default file is empty. [root@nglinux ~]# cat /etc/sysconfig/dhcpd # Command line options here DHCPDARGS= [root@nglinux ~]# ### Now enter the interface name in the dhcpd file. [root@nglinux ~]# vim /etc/sysconfig/dhcpd [root@nglinux ~]# cat /etc/sysconfig/dhcpd # Command line options here DHCPDARGS=eth2 [root@nglinux ~]#
c. Default DHCPD configuration File.
[root@nglinux ~]# cat /etc/dhcp/dhcpd.conf # # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.sample # see 'man 5 dhcpd.conf' # [root@nglinux ~]# ### As advised above, copy the sample configuration file. [root@nglinux ~]# cp -p /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf cp: overwrite `/etc/dhcp/dhcpd.conf'? y [root@nglinux ~]# more /etc/dhcp/dhcpd.conf # dhcpd.conf # # Sample configuration file for ISC dhcpd
5. DHCPD Server Configuration
# cat /etc/dhcp/dhcpd.conf ### Global DHCP Configuration option domain-name "ngelinux.com"; option domain-name-servers ns1.ngelinux.com, ns2.ngelinux.com; default-lease-time 3600; max-lease-time 7200; authoritative; ### General Configuration of subnet and IP range subnet 192.168.54.0 netmask 255.255.255.0 { option routers 192.168.54.1; option subnet-mask 255.255.255.0; option domain-search "ngelinux.com"; option domain-name-servers 192.168.54.1; range 192.168.54.10 192.168.54.100; range 192.168.54.120 192.168.54.200; } ### Assigning static IPs host redhat-node { hardware ethernet 00:e0:j4:6y:23:0a; fixed-address 192.168.54.109; } host oracle-node { hardware ethernet 00:4q:8a:12:7h:3b; fixed-address 192.168.54.110; }
6. Last step to get DHCP working.
a. Start and enable services. ### On CentOS / RHEL 7 # systemctl start dhcpd # systemctl enable dhcpd ### On CentOS / RHEL 6 # service dhcpd start # chkconfig dhcpd on b. Add rule in Firewall ### On CentOS / RHEL 7 # firewall-cmd --add-service=dhcp --permanent # firewall-cmd --reload ### On CentOS / RHEL 6 # iptables -A INPUT -p tcp -m state --state NEW --dport 67 -j ACCEPT # service iptables save
7. Interview Questions Of DHCP
1. What is DHCP ? DHCP is a protocol(dynamic host configuration protocol) responsible for assigning network settings(which includes assigning ip address, subnet mask, gateway, dns servers, etc) to a host. 2. Ports used by DHCP. Requests sent on UDP port 68, Server replies on UDP 67 . 3. How DHCP works ? DHCP woks on DORA model: - Discover, Offer, Request, and Acknowledgement 4. What is DHCP Scope ? Range of possible IP Addresses that the DHCP server can lease to clients on a subnet. 5. What is Super Scope ? Collection of all individual scopes that are configured/ managed by a single server/unit in network. 6. What is default lease time to a client ? 8 days 7. What is reservation in DHCP ? When a server is mapped to a fix IP address. 8. What is DHCP relay ? DHCP relay is an intermediate host that can forward dhcp client requests to a server in cases when the server is not directly reachable. 9. Can we assign static IP address to hosts ? Yes we can assign static IP address to a particular host based on MAC address.