{"id":4579,"date":"2019-10-20T12:30:20","date_gmt":"2019-10-20T09:30:20","guid":{"rendered":"https:\/\/kifarunix.com\/?p=4579"},"modified":"2024-03-12T22:10:48","modified_gmt":"2024-03-12T19:10:48","slug":"install-and-setup-dhcp-server-on-centos-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-and-setup-dhcp-server-on-centos-8\/","title":{"rendered":"Install and Setup DHCP Server on CentOS 8"},"content":{"rendered":"\n

Do you want to automatically assign IP addresses to the network devices in your LAN? Well, then you need to install and setup a Dynamic Host Configuration Protocol (DHCP) server. Follow through this guide to learn how to install and setup DHCP server on CentOS 8.<\/p>\n\n\n\n

Installing DHCP Server on CentOS 8<\/h2>\n\n\n
\n
\"install<\/a>
Our Deployment Architecture<\/figcaption><\/figure><\/div>\n\n\n

Run system update<\/h3>\n\n\n\n

Begin by resynchronizing system packages to their latest versions.<\/p>\n\n\n\n

dnf update<\/code><\/pre>\n\n\n\n

In CentOS 8, the DHCP server is provided by the dhcp-server<\/code> package. This package can be installed by running the command below;<\/p>\n\n\n\n

dnf install dhcp-server<\/code><\/pre>\n\n\n\n
Dependencies resolved.\n=======================================================================================================================================================\n Package                              Arch                            Version                                    Repository                       Size\n=======================================================================================================================================================\nInstalling:\n dhcp-server                          x86_64                          12:4.3.6-30.el8                            BaseOS                          529 k\n\nTransaction Summary\n=======================================================================================================================================================\nInstall  1 Package\n\nTotal download size: 529 k\n...<\/code><\/pre>\n\n\n\n

Configure DHCP Server on CentOS 8<\/h3>\n\n\n\n

When installed, the dhcp-server package creates an empty configuration file \/etc\/dhcp\/dhcpd.conf<\/code><\/strong>.<\/p>\n\n\n\n

It also creates a sample configuration file for ISC dhcpd under \/usr\/share\/doc\/dhcp-server\/dhcpd.conf.example<\/strong><\/code>.<\/p>\n\n\n\n

NOTE: This guide only provides DHCP server configuration for IPv4 addresses only.<\/p>\n\n\n\n

To begin with, you need to define the parameters and declarations options that stores the network information for the DHCP clients. These parameters and declarations should be made on the DHCPd configuration file, \/etc\/dhcp\/dhcpd.conf<\/code><\/strong>.<\/p>\n\n\n\n

Use the sample configuration file, \/usr\/share\/doc\/dhcp-server\/dhcpd.conf.example<\/strong> to guide you when defining these options.<\/p>\n\n\n\n

First off, be aware that dhcpd listens only on interfaces for which it finds subnet declaration in dhcpd.conf.<\/p>\n\n\n\n

If you declare a subnet that is not associated with the interface on which DHCP server is listening on, DHCP server daemon will fail to start with the error;<\/p>\n\n\n\n

No subnet declaration for enp0sX (x.x.x.x).<\/strong>\n** Ignoring requests on enp0sX.  If this is not what\nyou want, please write a subnet declaration\nin your dhcpd.conf file for the network segment\nto which interface enp0s3 is attached. **\n...<\/code><\/pre>\n\n\n\n

For example, to set DHCP server to listen on an interface, enp0s8<\/strong><\/code>, whose IP address details are shown below;<\/p>\n\n\n\n

ip add sh dev enp0s8<\/code><\/pre>\n\n\n\n
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000\n    link\/ether 08:00:27:36:48:ae brd ff:ff:ff:ff:ff:ff\n    inet 192.168.10.13\/24 brd 192.168.10.255 scope global dynamic enp0s8\n       valid_lft 1032sec preferred_lft 1032sec<\/code><\/pre>\n\n\n\n

Next, open the DHCP server configuration file for editing.<\/p>\n\n\n\n

vim \/etc\/dhcp\/dhcpd.conf<\/code><\/pre>\n\n\n\n

Configure your DHCP server such that your configuration may look like;<\/p>\n\n\n\n

#\n# DHCP Server Configuration file.\n#   see \/usr\/share\/doc\/dhcp-server\/dhcpd.conf.example\n#   see dhcpd.conf(5) man page\n#\n######################################################\n#Define the domain name of your DHCP server<\/strong>\noption domain-name \"kifarunix-demo.com\";\n\n# Define the IP address or Hostname of your DNS server<\/strong>\noption domain-name-servers ns1.kifarunix-demo.com;\n\n#If you have multiple DNS servers, specify them as;<\/strong>\n# option domain-name-servers ns1.kifarunix-demo.com, ns2.kifarunix-demo.com;\n\n# Define how long should an endpoint use the assigned IP address<\/strong>\ndefault-lease-time 3600;    # One hour\n\n# Define the maximum lease time<\/strong>\nmax-lease-time 7200; # Two hours\n\n# Other values you can use are 86400 (one day), 604800 (one week) and 2592000 (30 days).<\/strong>\n# \n# Declare the DHCP server official for the local network<\/strong>\nauthoritative;\n\n# Change the default log file for DHCP server.<\/strong>\nlog-facility local7;\n\n#### Subnet Declaration ####<\/strong>\n# The subnet declaration includes a range of IP addresses that a DHCP server can assign to clients.\n# and global options to apply for every DHCP client.\n#\n# Define the subnet of the DHCP server interface (Network address and subnet mask)<\/strong>\nsubnet 192.168.10.0 netmask 255.255.255.0 {\n    # Define the broadcast address<\/strong>\n    option broadcast-address 192.168.10.255;\n    # Define the gateway<\/strong>\n    option routers 192.168.10.1;\n    # Define the range of IP addresses to be leased<\/strong>\n    range 192.168.10.110 192.168.10.120;\n}<\/code><\/pre>\n\n\n\n

Defining Static IP Address Using DHCP<\/h3>\n\n\n\n

If you need to define static IP addresses using DHCP for specific hosts in your network, you need to get the MAC address of the host. After which, you can add the host declaration configuration into DHCP server configuration file.<\/p>\n\n\n\n

For example, to assign a static IP address to the host, webdev01.kifarunix-demo.com<\/strong> with MAC address, 01:23:45:67:89:ab<\/strong>, create the host declaration as shown below;<\/p>\n\n\n\n

# Fixed Host IP Address assigning<\/strong>\nhost webdev01 {\n    # Define the MAC address<\/strong>\n    hardware ethernet 08:00:27:57:57:ad<\/strong>;\n    # Define Fixed IP address<\/strong>\n    fixed-address 192.168.10.125;\n}<\/code><\/pre>\n\n\n\n

If you configured DHCP to log to a specific facility instead of the using the default, \/var\/log\/messages<\/code><\/strong>, you need to create the log directory. This can be done by modifying the Rsyslog configuration.<\/p>\n\n\n\n

For example, to configure DHCP server to log to \/var\/log\/dhcpd\/dhcpd.log<\/strong><\/code>, you need to create the directory \/var\/log\/dhcpd<\/strong><\/code>.<\/p>\n\n\n\n

Hence, since we configured to log to facility local7<\/code><\/strong>, simply add the line below to Rsyslog.conf.<\/p>\n\n\n\n

echo \"local7.debug \/var\/log\/dhcpd\/dhcpd.log\" >> \/etc\/rsyslog.conf<\/code><\/pre>\n\n\n\n

Check Rsyslog for syntax errors;<\/p>\n\n\n\n

rsyslogd -N1<\/code><\/pre>\n\n\n\n

Create the log directory.<\/p>\n\n\n\n

mkdir \/var\/log\/dhcpd<\/code><\/pre>\n\n\n\n

Restart Rsyslog<\/p>\n\n\n\n

systemctl restart rsyslog<\/code><\/pre>\n\n\n\n

Running DHCP Daemon<\/h3>\n\n\n\n

Once the configuration is done, you can start and enable DHCPd by executing the command;<\/p>\n\n\n\n

systemctl enable --now dhcpd<\/code><\/pre>\n\n\n\n

Check the status;<\/p>\n\n\n\n

systemctl status dhcpd<\/code><\/pre>\n\n\n\n
\u25cf dhcpd.service - DHCPv4 Server Daemon\n   Loaded: loaded (\/usr\/lib\/systemd\/system\/dhcpd.service; enabled; vendor preset: disabled)\n   Active: active (running) since Sat 2019-10-20 10:01:59 EAT; 2min ago\n     Docs: man:dhcpd(8)\n           man:dhcpd.conf(5)\n Main PID: 6598 (dhcpd)\n   Status: \"Dispatching packets...\"\n    Tasks: 1 (limit: 5072)\n   Memory: 5.3M\n   CGroup: \/system.slice\/dhcpd.service\n           \u2514\u25006598 \/usr\/sbin\/dhcpd -f -cf \/etc\/dhcp\/dhcpd.conf -user dhcpd -group dhcpd --no-pid\n...<\/code><\/pre>\n\n\n\n

Also check the logs on, \/var\/log\/dhcpd\/dhcpd.log<\/code><\/strong>.<\/p>\n\n\n\n

tail \/var\/log\/dhcpd\/dhcpd.log<\/code><\/pre>\n\n\n\n

Open DHCP UDP Port on Firewall<\/h3>\n\n\n\n

To allow remote clients to query your DHCP server for IP addresses, you need to open DHCP UDP port 67 on firewalld if it is running.<\/p>\n\n\n\n

firewall-cmd --add-port=67\/udp --permanent<\/code><\/pre>\n\n\n\n

Reload FirewallD<\/p>\n\n\n\n

firewall-cmd --reload<\/code><\/pre>\n\n\n\n

Configure DHCP Client on CentOS 8<\/h3>\n\n\n\n

To verify that your DHCP server is working, you need to configure DHCP client. In this guide, we are using another CentOS 8 server as DHCP client.<\/p>\n\n\n\n

Install DHCP client package by running the command below if it is not already installed.<\/p>\n\n\n\n

dnf install dhcp-client<\/code><\/pre>\n\n\n\n

Once the DHCP client is installed, simply run dhclient command to get the DHCP IP.<\/p>\n\n\n\n

dhclient<\/code><\/pre>\n\n\n\n

Now check the assigned IP addresses.<\/p>\n\n\n\n

On the host with DHCP static IP mapping (webdev01) for our case;<\/p>\n\n\n\n

ip add<\/code><\/pre>\n\n\n\n
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000\n    link\/ether 08:00:27:57:57:ad brd ff:ff:ff:ff:ff:ff\n    inet 192.168.10.125\/24 brd 192.168.56.255 scope global dynamic enp0s8\n       valid_lft 3059sec preferred_lft 3059sec\n    inet6 fe80::76d2:60c9:4cea:3ec8\/64 scope link noprefixroute \n       valid_lft forever preferred_lft forever<\/code><\/pre>\n\n\n\n

On the other host, the addressed assigned should be within the defined range.<\/p>\n\n\n\n

nmcli dev show enp0s8<\/code><\/pre>\n\n\n\n
GENERAL.DEVICE:                         enp0s8\nGENERAL.TYPE:                           ethernet\nGENERAL.HWADDR:                         08:00:27:5A:86:69\nGENERAL.MTU:                            1500\nGENERAL.STATE:                          100 (connected)\nGENERAL.CONNECTION:                     Wired connection 1\nGENERAL.CON-PATH:                       \/org\/freedesktop\/NetworkManager\/ActiveConnection\/1\nWIRED-PROPERTIES.CARRIER:               on\nIP4.ADDRESS[1]:                         192.168.10.113\/24\nIP4.GATEWAY:                            192.168.10.5\nIP4.ROUTE[1]:                           dst = 192.168.10.0\/24, nh = 0.0.0.0, mt = 0\nIP4.ROUTE[2]:                           dst = 0.0.0.0\/0, nh = 192.168.56.2, mt = 0\nIP4.DOMAIN[1]:                          kifarunix-demo.com\n...<\/code><\/pre>\n\n\n\n

You have successfully setup DHCP server and client. That marks the end of our guide.<\/p>\n\n\n\n

Read more about DHCP parameters and options on ISC DHCP manual pages.<\/p>\n\n\n\n

ISC DHCP Manual pages<\/a><\/p>\n\n\n\n

Related guides<\/h3>\n\n\n\n

How to Install and Setup DHCP Server on Fedora 29\/Fedora 28\/CentOS 7<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

Do you want to automatically assign IP addresses to the network devices in your LAN? Well, then you need to install and setup a Dynamic<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,258],"tags":[1142,1185,259,1186],"class_list":["post-4579","post","type-post","status-publish","format-standard","hentry","category-howtos","category-dhcp-server","tag-centos-8","tag-dhcp-client","tag-dhcp-server","tag-dhcp-server-on-centos-8","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4579"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=4579"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4579\/revisions"}],"predecessor-version":[{"id":21220,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4579\/revisions\/21220"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=4579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=4579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=4579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}