{"id":4178,"date":"2015-11-10T12:35:55","date_gmt":"2015-11-10T18:35:55","guid":{"rendered":"http:\/\/www.poweradmin.com\/blog\/?p=4178"},"modified":"2015-10-07T13:00:15","modified_gmt":"2015-10-07T18:00:15","slug":"how-to-install-and-configure-a-dhcp-server-on-a-linux-machine-part-2","status":"publish","type":"post","link":"https:\/\/www.poweradmin.com\/blog\/how-to-install-and-configure-a-dhcp-server-on-a-linux-machine-part-2\/","title":{"rendered":"How to install and configure a DHCP Server on a Linux machine &#8211; Part 2"},"content":{"rendered":"<p><span style=\"font-family: verdana, geneva, sans-serif;\">Hi folks,<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">In the <a href=\"http:\/\/www.poweradmin.com\/blog\/how-to-install%E2%80%A6machine-part-1\/\">previous\u00a0article<\/a> we\u2019ve configured a <a href=\"http:\/\/www.poweradmin.com\/blog\/a-brief-history-of-linux\/\">CentOS<\/a>\u00a0machine and installed the DHCP package. We\u2019ve then created a sample configuration file for our machine, but we haven\u2019t finished deploying our DHCP server yet. In this article we\u2019ll finish our configuration and hopefully, we\u2019ll be able to obtain an IP address lease from our DHCP machine. Note that all these configurations will be made in my virtualized environment by using two Virtual Machines (a DHCP server and a DHCP client).<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Now that the sample configuration file has been copied in our install directory, open the<i> dhcpd.conf<\/i> file using your favorite editor. I\u2019ve edited the file and kept only the information that we require for our server. The file should look similar to:<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i>authoritative;<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i>ddns-update-style none;<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i>option domain-name \u201cppscu.com\u201d;<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i>option domain-name-servers 10.10.10.1; 127.0.0.1<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i>option routers 10.10.10.1<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i>default-lease-time 600;<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i>max-lease-time 7200;<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i>subnet 10.10.10.0 netmask 255.255.255.0 {<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i> range 10.10.10.150 10.10.10.200;<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><b><i> }<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The #sign is used to comment lines so whatever is written after this character is not executed. It\u2019s important to put the<b><i> \u201c;\u201d <\/i><\/b>symbol at the end of each line.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">I will try to explain each parameter used in our configuration file:<\/span><\/p>\n<ul>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\"><b>authoritative \u2013<\/b> an authoritative server will respond to requests coming from clients that are part of the same subnet<b>. <\/b>Simply put, if a client from a subnet that was not configured on the DHCP server requests an IP renewal, the DHCP server will send <i>DHCP NACK<\/i> messages forcing the client to release its IP configuration.<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\"><b>ddns-update-style none<\/b> \u2013 disables dynamic DNS<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\"><b>option domain-name \u201cppscu.com\u201d <\/b>\u2013 sets the domain name of the DHCP clients<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\"><b>option domain-name-servers 10.10.10.1<\/b> \u2013 sets the DNS servers used by clients<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\"><b>option routers 10.10.10.1 \u2013<\/b> configured the network routers<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\"><b>default-lease-time 600; <\/b>and <b>max-lease-time 7200; \u2013 <\/b>the number of seconds a client can hold its IP allocation before submitting a renewal<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Note that if we include these settings in the subnet configuration section, the settings will only be applied to that particular subnet. In this example we\u2019ve configured these settings for the whole server thus all subnets configured on this machine will carry the same settings.<\/span><\/p>\n<ul>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\"><b>subnet 10.10.10.0 netmask 255.255.255.0 { \u2013 <\/b>the subnet in which the <a href=\"http:\/\/www.poweradmin.com\/blog\/how-to-implement-dhcp-relay-agent-roas-ospf-open-shortest-path-first\/\">DHCP server<\/a> operates<\/span><\/li>\n<li><span style=\"font-family: verdana, geneva, sans-serif;\"><b>range 10.10.10.150 10.10.10.200; } \u2013<\/b> the pool of IP addresses used by DHCP clients<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">After we\u2019ve finished our configuration, it\u2019s time to test the configuration file for errors by typing<b><i> dhcpd -t<\/i><\/b>. If there are no errors received, the DHCP daemon can be started. You can verify its status by typing<b> <i>service \u2013status-all | grep dhcpd<\/i><\/b><\/span><\/p>\n<p><a href=\"http:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/service-status-all-dhcpd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4181 aligncenter\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/service-status-all-dhcpd.png\" alt=\"service status all dhcp\" width=\"629\" height=\"35\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/service-status-all-dhcpd.png 629w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/service-status-all-dhcpd-300x17.png 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\"><\/a><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Start the DHCP daemon by typing<b><i> \/sbin\/service dhcpd star<\/i>t<\/b>. You can stop it by typing: <b><i>\/sbin\/service dhcpd stop<\/i><\/b><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">If you type <b><i>ps -el | grep dhcpd<\/i><\/b>, you should be able to see the daemon running on your machine. If you have any problems with the server at this point, type<b><i> service dhcpd restart<\/i><\/b> to restart the DHCP daemon:<\/span><\/p>\n<p><a href=\"http:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/grep-dhcpd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4182 aligncenter\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/grep-dhcpd.png\" alt=\"grep dhcpd\" width=\"806\" height=\"148\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/grep-dhcpd.png 806w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/grep-dhcpd-300x55.png 300w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\"><\/a><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">We can also check if the server <i>listens on UDP port 67<\/i> by typing <b><i>netstat \u2013paun<\/i><\/b><\/span><\/p>\n<p><a href=\"http:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/netstat-paun.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4183 aligncenter\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/netstat-paun.png\" alt=\"netstat -paun\" width=\"838\" height=\"438\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/netstat-paun.png 838w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/netstat-paun-300x157.png 300w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\"><\/a><\/p>\n<p><i>p = display PID\/Program name for sockets; a \u2013 all; u \u2013 show only UDP ports; n \u2013 numeric<\/i><\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n<h2><b><i>CLIENT CONFIGURATION<\/i><\/b><\/h2>\n<p>On the client side, you will need to type <b><i>dhclient [interface name]<\/i><\/b>to configure the DHCP client to listen on the specified interface. After this step is complete, if you type<b><i>ifconfig <\/i><\/b>or<b><i>ip addr show<\/i><\/b>, you should be able to see the leased IP address:<\/p>\n<p><a href=\"http:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/ip-addr-show.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4184 aligncenter\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/ip-addr-show.png\" alt=\"ip addr show\" width=\"724\" height=\"459\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/ip-addr-show.png 724w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/ip-addr-show-300x190.png 300w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\"><\/a><\/p>\n<p>The dhclient configuration file is stored in <i>\/etc\/dhcp\/dhclient.conf<\/i>. By editing this file, we can set custom settings for the DHCP client:<\/p>\n<p><a href=\"http:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhclient.conf_.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4185 aligncenter\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhclient.conf_.png\" alt=\"dhclient.conf\" width=\"705\" height=\"828\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhclient.conf_.png 705w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhclient.conf_-255x300.png 255w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\"><\/a><\/p>\n<p>The database of the DHCP client is stored under<i> \/var\/lib\/dhcp\/dhclient.leases<\/i><\/p>\n<p>Here you will be able to see all the DHCP data the client has received from the server.<\/p>\n<p>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/p>\n<p>If we return on the DHCP server, we can check out its database in which the leased IP addresses are stored which can be found in <b><i>\/var\/lib\/dhcpd\/dhcpd.leases<\/i><\/b>. Execute <b><i>cat<\/i><\/b> <b><i>\/var\/lib\/dhcpd\/dhcpd.leases<\/i><\/b> to view its content:<\/p>\n<p><a href=\"http:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhcpd.leases.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4186 aligncenter\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhcpd.leases.png\" alt=\"dhcpd.leases\" width=\"627\" height=\"259\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhcpd.leases.png 627w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhcpd.leases-300x124.png 300w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\"><\/a><\/p>\n<p>By checking this file we can also verify what IP addresses have been leased to our DHCP clients. The number of leased IP addresses can be easily viewed by typing <i>dhcpd<\/i> once the daemon has been started:<\/p>\n<p><a href=\"http:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhcpd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4187 aligncenter\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhcpd.png\" alt=\"dhcpd\" width=\"806\" height=\"355\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhcpd.png 806w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/dhcpd-300x132.png 300w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\"><\/a><\/p>\n<p><i><br>\n<\/i>If we want to reserve a specific IP address for a DHCP client, we\u2019ll need to type in the following:<\/p>\n<p><b><i>host ubuntu1 { <\/i><\/b> host identification within the dhcpd.conf file<\/p>\n<p><b><i> hardware ethernet 00:0c:29:bd:5b:69; <\/i><\/b>\u2013 <a href=\"http:\/\/www.poweradmin.com\/blog\/what-is-ipv6\/\">MAC address<\/a> of the host<\/p>\n<p><b><i> option domain-name-servers 10.10.10.1; <\/i><\/b> \u2013 specific DNS servers used by this host (optional parameter)<\/p>\n<p><b><i> option routers 10.10.10.1; <\/i><\/b>\u2013 specific gateway used by the host (optional)<\/p>\n<p><b><i> fixed-address 10.10.10.160; <\/i><\/b> \u2013 host\u2019s reserved IP address<\/p>\n<p><b><i>}<\/i><\/b><\/p>\n<p>Once you\u2019ve made this configuration, restart the DHCP daemon by typing <b><i>service dhcpd restart<\/i><\/b>. On the client side, we\u2019ll need to disable\/enable the network interface by typing<b><i> ifconfig eth0 down<\/i><\/b> followed by<b><i> ifconfig eth0 up<\/i><\/b>.<\/p>\n<p>Another method of renewing the leased IP address is by typing: <b><i>sudo dhclient -r eth0<\/i><\/b> followed by <b><i>sudo dhclient eth0<\/i><\/b><i>.<\/i><\/p>\n<p>If there are no problems on the server, the DHCP client will renew its IP address:<\/p>\n<p><a href=\"http:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/ifconfig.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4188 aligncenter\" src=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/ifconfig.png\" alt=\"ifconfig\" width=\"662\" height=\"182\" srcset=\"https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/ifconfig.png 662w, https:\/\/www.poweradmin.com\/blog\/wp-content\/uploads\/2015\/11\/ifconfig-300x82.png 300w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\"><\/a><\/p>\n<p>We can further modify our <i>dhcpd.config <\/i>file to set custom settings for a group of DHCP clients by adding the following entries:<\/p>\n<p><b>group {<\/b><\/p>\n<p><b><i>option domain-name-servers 10.10.10.1; <\/i><\/b>\u2013 global settings applied to this group of hosts<\/p>\n<p><b><i>option routers 10.10.10.1;<\/i><\/b><\/p>\n<p><b><i> host ubuntu1 { 00:0c:29:bd:5b:69; } <\/i><\/b><i>\u2013 <\/i>each DHCP client with its corresponding MAC address<\/p>\n<p><b><i> host ubuntu2 { 00:0c:29:bd:5b:70; }<\/i><\/b><\/p>\n<p><b><i> host ubuntu3 { 00:0c:29:bd:5b:71; }<\/i><\/b><\/p>\n<p>}<\/p>\n<p>As you\u2019ve seen in this article, the DHCP client-server configuration it\u2019s pretty easy to understand once you\u2019ve learned the basics for this service. We\u2019ve now finished our DHCP deployment with a server capable of offering dynamic IP allocation to our network hosts. We\u2019ve also tested that the lease works by configuring a DHCP client. Hope this article will serve you well in understanding how to deploy a DHCP infrastructure on Linux machines. Wish you a great day and hope you\u2019ll visit us soon to check out our latest articles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi folks, In the previous\u00a0article we\u2019ve configured a CentOS\u00a0machine and installed the DHCP package. We\u2019ve then created a sample configuration file for our machine, but we haven\u2019t finished deploying our DHCP server yet. In this article we\u2019ll finish our configuration and hopefully, we\u2019ll be able to obtain an IP address lease from our DHCP machine. [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":4153,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-4178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","category-technical"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts\/4178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/comments?post=4178"}],"version-history":[{"count":5,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts\/4178\/revisions"}],"predecessor-version":[{"id":4191,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/posts\/4178\/revisions\/4191"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/media\/4153"}],"wp:attachment":[{"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/media?parent=4178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/categories?post=4178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poweradmin.com\/blog\/wp-json\/wp\/v2\/tags?post=4178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}