{"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
Begin by resynchronizing system packages to their latest versions.<\/p>\n\n\n\n
dnf update<\/code><\/pre>\n\n\n\nIn 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\ndnf install dhcp-server<\/code><\/pre>\n\n\n\nDependencies 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\nConfigure 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\nIt also creates a sample configuration file for ISC dhcpd under \/usr\/share\/doc\/dhcp-server\/dhcpd.conf.example<\/strong><\/code>.<\/p>\n\n\n\nNOTE: 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\nUse the sample configuration file, \/usr\/share\/doc\/dhcp-server\/dhcpd.conf.example<\/strong> to guide you when defining these options.<\/p>\n\n\n\nFirst 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\nFor example, to set DHCP server to listen on an interface, enp0s8<\/strong><\/code>, whose IP address details are shown below;<\/p>\n\n\n\nip add sh dev enp0s8<\/code><\/pre>\n\n\n\n3: 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\nNext, open the DHCP server configuration file for editing.<\/p>\n\n\n\n
vim \/etc\/dhcp\/dhcpd.conf<\/code><\/pre>\n\n\n\nConfigure 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\nDefining 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\nIf 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\nFor 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\nHence, since we configured to log to facility local7<\/code><\/strong>, simply add the line below to Rsyslog.conf.<\/p>\n\n\n\necho \"local7.debug \/var\/log\/dhcpd\/dhcpd.log\" >> \/etc\/rsyslog.conf<\/code><\/pre>\n\n\n\nCheck Rsyslog for syntax errors;<\/p>\n\n\n\n
rsyslogd -N1<\/code><\/pre>\n\n\n\nCreate the log directory.<\/p>\n\n\n\n
mkdir \/var\/log\/dhcpd<\/code><\/pre>\n\n\n\nRestart Rsyslog<\/p>\n\n\n\n
systemctl restart rsyslog<\/code><\/pre>\n\n\n\nRunning 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\nCheck 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\nAlso check the logs on, \/var\/log\/dhcpd\/dhcpd.log<\/code><\/strong>.<\/p>\n\n\n\ntail \/var\/log\/dhcpd\/dhcpd.log<\/code><\/pre>\n\n\n\nOpen 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\nReload FirewallD<\/p>\n\n\n\n
firewall-cmd --reload<\/code><\/pre>\n\n\n\nConfigure 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\nOnce 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\nNow 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\n3: 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\nOn 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\nGENERAL.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\nYou 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\nRelated guides<\/h3>\n\n\n\n