{"id":676,"date":"2020-10-18T18:46:55","date_gmt":"2020-10-18T18:46:55","guid":{"rendered":"https:\/\/linuxways.net\/?p=676"},"modified":"2020-11-25T21:10:43","modified_gmt":"2020-11-25T21:10:43","slug":"how-to-configure-dhcp-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-configure-dhcp-in-linux\/","title":{"rendered":"How to install and configure DHCP server in Linux"},"content":{"rendered":"<p>DHCP (stands for dynamic host configuration protocol) dynamically assigns unique IP addresses and other information to client systems. Some of the information that is assigned by DHCP includes hostname, domain name, IP address of the default gateway and DNS server, etc. It saves the system administrator from the hassle of manually assigning IP addresses and related information to each of the client systems. Usually, in the home or small company setup, routers serve the role of a DHCP server. However, in a large setup, we need to configure a dedicated server to serve as a DHCP server. In Linux, you can easily set up a DHCP server and assigns IP addresses to clients.<\/p>\n<p><strong>In this tutorial, we will demonstrate how to configure DHCP in Linux to automatically assign IP addresses to clients.<\/strong><\/p>\n<h2>How DHCP works<\/h2>\n<p>Let&#8217;s explain a little about how DHCP works:<\/p>\n<p>Consider a scenario where the client and DHCP server are connected to the same wired network.<\/p>\n<ol>\n<li>When a client machine is turned on, it sends a broadcast message <strong>DHCPDISCOVER<\/strong> to every device on the network.<\/li>\n<li>When this message reached the DHCP server, it sends back a <strong>DHCPOFFER<\/strong> message with an IP address to client.<\/li>\n<li>The client then sends a <strong>DHCPREQUEST<\/strong> to DHCP server to request for this IP address.<\/li>\n<li>The DHCP server then sends <strong>DHCPACK<\/strong> to acknowledge the request and grants the client with an IP address.<\/li>\n<\/ol>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Minimum two Linux machines (For DHCP server and client)<\/li>\n<li>User with sudo privileges<\/li>\n<li>Static IP configured on DHCP server<\/li>\n<\/ul>\n<h2>Note:<\/h2>\n<p>For the demonstration purpose, we have used three Linux machines which were on the same LAN. You can think of them as all are connected through each other via switch.<\/p>\n<p><strong>DHCP server<\/strong><\/p>\n<p>For DHCP server, we have used:<\/p>\n<ul>\n<li>Ubuntu 20.04 LTS with static IP \u201c192.168.9.1\u201d and hostname \u201cubuntu\u201d<\/li>\n<\/ul>\n<p><strong>DHCP client<\/strong><\/p>\n<p>For DHCP client, we have used two machines:<\/p>\n<ul>\n<li>Ubuntu 20.04 with hostname \u201cmypc\u201d<\/li>\n<li>Mint 20 with hostname \u201cmint\u201d<\/li>\n<\/ul>\n<h2>DHCP Server Installation<\/h2>\n<p>First, update the system repository index in your system. Issue the below command in Terminal to do so:<\/p>\n<pre>$ sudo apt update<\/pre>\n<p>Now issue the following command in Terminal to install DHCP server:<\/p>\n<pre>$ sudo apt-get install isc-dhcp-server -y<\/pre>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-677\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-338.png\" alt=\"install dhcp linux\" width=\"778\" height=\"271\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-338.png 778w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-338-300x104.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-338-768x268.png 768w\" sizes=\"auto, (max-width: 778px) 100vw, 778px\" \/><\/h2>\n<h2>DHCP Server Configuration<\/h2>\n<p>Before configuring a Linux system as a DHCP server, make sure it has a static IP address configured so that clients can easily connect to it. Otherwise, it will be difficult for the clients to find which DHCP server to connect to.<\/p>\n<p>In the <strong>\/etc\/default\/isc-dhcp-server<\/strong> file, you will need to specify the interface on which DHCP server will listen to. Issue the following command in Terminal to edit this file:<\/p>\n<pre>$ sudo nano\u00a0\/etc\/default\/isc-dhcp-server<\/pre>\n<p>In the<strong> INTERFACESv4 e<\/strong>ntry, specify the interface you want the DHCP server to listen to such as ens33, eth0, eth1, etc.<\/p>\n<pre>INTERFACESv4=\"interface_name\"<\/pre>\n<p>In our scenario, the interface is<strong> ens33<\/strong>, so the entry would change to:<\/p>\n<pre>INTERFACESv4=\"ens33\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-678\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-339.png\" alt=\"install dhcp linux\" width=\"717\" height=\"361\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-339.png 717w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-339-300x151.png 300w\" sizes=\"auto, (max-width: 717px) 100vw, 717px\" \/><\/p>\n<p>Once you have configured the file, save, and close it.<\/p>\n<p>Now we will configure DHCP Server using its default configuration file located at <strong>\/etc\/dhcp\/dhcpd.conf<\/strong>.<\/p>\n<p>Edit the <strong>\/etc\/dhcp\/dhcpd.conf<\/strong> file as follows:<\/p>\n<pre>$ sudo nano \/etc\/dhcp\/dhcpd.conf<\/pre>\n<p>Uncomment the following line by removing the <strong>#<\/strong> symbol before it.<\/p>\n<pre>authoritative;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-679\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-340.png\" alt=\"install dhcp linux\" width=\"642\" height=\"267\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-340.png 642w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-340-300x125.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<p>Also, add the following lines in the file:<\/p>\n<pre>default-lease-time 600;\n\nmax-lease-time 7200;\n\n# subnet\n\nsubnet 192.168.9.0 netmask 255.255.255.0 {\n\n# range of subnet\n\nrange 192.168.9.5 192.168.9.15;\n\n# gateway address\n\noption routers 192.168.9.1;\n\n# DNS server address\n\noption domain-name-servers 8.8.8.8, 8.8.4.4;\n\n}\n\n<img loading=\"lazy\" decoding=\"async\" width=\"880\" height=\"479\" class=\"wp-image-680\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-341.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-341.png 880w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-341-300x163.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-341-768x418.png 768w\" sizes=\"auto, (max-width: 880px) 100vw, 880px\" \/><\/pre>\n<p>Once you have configured the file, save, and close it.<\/p>\n<h3>Assign a static IP address to clients from DHCP server<\/h3>\n<p>The DHCP server issues dynamic IP addresses to the clients by default. However, you can also configure it to assign fixed or static IP address to clients. Fixed addresses are required for some network devices like printers, routers, or when you need to remotely connect a device from outside. To assign a static IP address to a specific machine, you will need its MAC address.<\/p>\n<p>Let\u2019s say we want to assign a client with MAC address <strong>00:0c:29:c1:d5:d4<\/strong> a fixed IP <strong>192.168.1.13<\/strong> every time.<\/p>\n<p>Edit the <strong>\/etc\/dhcp\/dhcpd.conf<\/strong> file and add host entry containing the MAC address of the client and the fixed IP address you want to assign to the client:<\/p>\n<pre>host mint {\n\nhardware ethernet 00:0c:29:c1:d5:d4;\n\nfixed-address 192.168.9.13;\n\n}<\/pre>\n<p><strong>Note:<\/strong> Fixed IP address should not be from the range of the Dynamic IP range.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"430\" class=\"wp-image-681\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-342.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-342.png 834w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-342-300x155.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-342-768x396.png 768w\" sizes=\"auto, (max-width: 834px) 100vw, 834px\" \/><\/p>\n<p>Now, every time the client with the matching MAC address (00:0c:29:c1:d5:d4) sends a DHCPDISCOVER message to the DHCP server, will receive the same IP address 192.168.9.13.<\/p>\n<h3>Restart DHCP service<\/h3>\n<p>Once you are done with the configurations, restart the DHCP server service. Issue the below command in Terminal to do so:<\/p>\n<pre>$ sudo systemctl restart isc-dhcp-server.service<\/pre>\n<p>Now verify the status of the DHCP server using the below command:<\/p>\n<pre>$ sudo systemctl status isc-dhcp-server.service<\/pre>\n<p>The following output shows the DHCP service is active and running successfully without any errors.<\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" width=\"1113\" height=\"134\" class=\"wp-image-682\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-343.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-343.png 1113w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-343-300x36.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-343-1024x123.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-343-768x92.png 768w\" sizes=\"auto, (max-width: 1113px) 100vw, 1113px\" \/>DHCP Client Configuration<\/h2>\n<p>Now we will configure two client machines. One client will be assigned random IP from the subnet range defined in the configuration file. The other client will be assigned the static IP.<\/p>\n<p>By default, dynamic IP addressing is configured in Ubuntu OS. However, if it is using the static IP address, you can configure it to obtain an IP address from the DHCP server.<\/p>\n<h3>Client 1- Obtain Dynamic IP address from the DHCP server<\/h3>\n<p>First, find your network interface name using the following command:<\/p>\n<pre>$ ifconfig<\/pre>\n<p>Once you have found the interface name, issue the following command in Terminal to configure the network interface:<\/p>\n<pre>$ sudo nano \/etc\/network\/interfaces<\/pre>\n<p>Insert the below lines in the file:<\/p>\n<pre>auto &lt;interface_name&gt;\n\niface &lt;interface_name&gt;inet dhcp<\/pre>\n<p>In our case, it would be:<\/p>\n<pre>auto ens33\n\niface ens33 inet dhcp<\/pre>\n<p>Save and close the file and restart the network-manager service using the following command in Terminal:<\/p>\n<pre>$ sudo systemctl restart network-manager.service<\/pre>\n<p>Now run the ifconfig command to verify the IP address of your client.<\/p>\n<pre>$ ifconfig<\/pre>\n<p>You can see in the following output that the client has received the IP address 192.168.9.5 which is from the subnet range defined in the DHCP server configuration.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"827\" height=\"232\" class=\"wp-image-683\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-344.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-344.png 827w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-344-300x84.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-344-768x215.png 768w\" sizes=\"auto, (max-width: 827px) 100vw, 827px\" \/><\/p>\n<p>In order to find the IP address of the DHCP server, issue the following command in Terminal:<\/p>\n<pre>$ sudo grep -R \u201cDHCPOFFER\u201d \/var\/sys\/log<\/pre>\n<p>From the output, you can verify the IP address is assigned by 192.168.9.1 which is the IP address of our DHCP server.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"898\" height=\"73\" class=\"wp-image-684\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-345.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-345.png 898w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-345-300x24.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-345-768x62.png 768w\" sizes=\"auto, (max-width: 898px) 100vw, 898px\" \/><\/p>\n<p>In case, there is no IP address obtained by the client machine from the DHCP server, then use the following commands to release\/renew the IP address:<\/p>\n<pre>$ sudo dhclient -r\u00a0<br \/><br \/>$ sudo dhclient -v\n<\/pre>\n<p>Now run the <strong>ifconfig<\/strong> command to verify the IP address of the client. This time hopefully the client will receive the IP address from the DHCP server.<\/p>\n<h3>Client 2- Obtain Static IP address from the DHCP server<\/h3>\n<p>For our second client machine, we have added the static\/fixed IP address entry in the DHCP server configuration file. On the client machine, first configure the interface to acquire the IP address from the DHCP server.<\/p>\n<p>Find your network interface name using the following command:<\/p>\n<pre>$ ifconfig<\/pre>\n<p>Once you have found the interface name, issue the following command in Terminal to configure the network interface:<\/p>\n<pre>$ sudo nano \/etc\/network\/interfaces<\/pre>\n<p>Insert the below lines in the file:<\/p>\n<pre>auto &lt;interface_name&gt;<\/pre>\n<pre>iface &lt;interface_name&gt;inet dhcp<\/pre>\n<p>In our case, it would be:<\/p>\n<pre>auto ens33<br \/><br \/>iface ens33 inet dhcp<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"260\" class=\"wp-image-685\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-346.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-346.png 645w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-346-300x121.png 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/p>\n<p>Now save and close the file and restart the network-manager service using the following command in Terminal:<\/p>\n<pre>$ sudo systemctl restart network-manager.service<\/pre>\n<p>Now run the ifconfig command to verify the IP address of your client.<\/p>\n<pre>$ ifconfig<\/pre>\n<p>You can see in the following output that the client has received the fixed IP address 192.168.9.13 from the DHCP server. This is the same fixed IP address we have added in the DHCP configuration file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"757\" height=\"217\" class=\"wp-image-686\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-347.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-347.png 757w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-347-300x86.png 300w\" sizes=\"auto, (max-width: 757px) 100vw, 757px\" \/><\/p>\n<p>In order to find the IP address of the DHCP server, issue the following command in Terminal:<\/p>\n<pre>$ sudo grep -R \u201cDHCPOFFER\u201d \/var\/sys\/log<\/pre>\n<p>From the output, you can verify the IP addresses are assigned by 192.168.9.1 which is our DHCP server.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"830\" height=\"83\" class=\"wp-image-687\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-348.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-348.png 830w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-348-300x30.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-348-768x77.png 768w\" sizes=\"auto, (max-width: 830px) 100vw, 830px\" \/><\/p>\n<p>In case, there is no IP address obtained by the client machine from the DHCP server, then use the following commands to release\/renew IP address:<\/p>\n<pre>$ sudo dhclient \u2013r -v<br \/><br \/>$ sudo dhclient -v<\/pre>\n<p>Now run the <strong>ifconfig<\/strong> command to verify the IP address of the client. This time hopefully the client will receive the IP address from the DHCP server.<\/p>\n<h2>View assigned IP addresses by DHCP server<\/h2>\n<p>From the DHCP server, you can also verify the dynamic addresses assigned by it to the clients.<\/p>\n<p>In the DHCP server machine, run the following command in Terminal to list the addresses assigned by the DHCP server:<\/p>\n<pre>$ dhcp-lease-list<\/pre>\n<p>In the following output, you will see only the IP addresses dynamically assigned by the DHCP server to a client. Remember, it will not list the static IP addresses that are issued by the DHCP server.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1263\" height=\"157\" class=\"wp-image-688\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-349.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-349.png 1263w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-349-300x37.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-349-1024x127.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-349-768x95.png 768w\" sizes=\"auto, (max-width: 1263px) 100vw, 1263px\" \/><\/p>\n<p>In this article, you have learned how to install and configure the DHCP server in the Linux system and assign dynamic and fixed IP addresses to the clients. It takes just a few simple steps to configure the DHCP server in a Linux machine. Once you have configured it, it will automatically assign the assigning IP addresses and related information to each machine on the network.<\/p>\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>DHCP (stands for dynamic host configuration protocol) dynamically assigns unique IP addresses and other information to client systems. Some of the information that is assigned by DHCP includes&hellip;<\/p>","protected":false},"author":1,"featured_media":689,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,2],"tags":[80,39],"class_list":["post-676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mint","category-ubuntu","tag-dhcp","tag-mint"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=676"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/689"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}