Objectives
The purpose and functions of DHCP
The functions of and the differences between MAC and IP addresses
Several of the many network configuration items that DHCP can serve
How to assign and manage static IP addresses for specific hosts based on the MAC address
Overview of DHCP
The Dynamic Host Configuration Protocol (DHCP) provides a centralized and automated method for configuring hosts when they connect to the network. This reduces the need to configure each network host individually. It is useful for portable devices such as laptops which might connect as unknown guests. DHCP offers even more advantages when used to manage static IP address assignments for known hosts using the central DHCP database.
The DHCP server uses a database of information created by the SysAdmin. This database is entirely contained in the /etc/dhcp/dhcpd.conf configuration file. Like all well-designed Linux configuration files, it is a simple ASCII plain text file. This means that it is open and knowable and that it can be examined by standard, simple text manipulation tools like cat and grep and modified by any text editor such as Emacs or Vim, or a stream editor such as sed.
In addition to assigning IP addresses to client hosts, DHCP can also provide host configuration information such as DNS servers, the domain name used for DNS searches, the default gateway, an NTP (Network Time Protocol) server, a server from which a network boot can be performed, and more.
The DHCP client is always installed on Linux clients – certainly at least Red Hat–based distros and all the other distros I have tried – because of the very high probability that they will be connected to a network using DHCP and not with a static configuration.
When a host configured for DHCP is booted, or its NIC is turned up (activated), it sends a broadcast request to the network asking for a DHCP server to respond. The client and the server engage in a bit of conversation and the server sends the configuration data to the client which uses it to self-configure its network connection. Hosts may have multiple NICs connected to different networks and any or all may be configured using DHCP, or one or more of the NICs may be configured using DHCP and one or more NICS may be configured using static configuration.
Installing the DHCP server
The DHCP server is not installed by default and, like the other servers we will install during this course, we must install it ourselves.
Experiment 3-1
- 1.
Start StudentVM2 if it is not already running.
- 2.
After StudentVM2 has finished boot and startup, login as the student1 user, open a terminal session and su – to root.
- 3.Check to see which DHCP packages are already installed.[root@studentvm2 ~]# dnf list installed dhcp∗Installed Packagesdhcp-client.x86_64 12:4.3.6-28.fc29 @anacondadhcp-common.noarch 12:4.3.6-28.fc29 @anacondadhcp-libs.x86_64 12:4.3.6-28.fc29 @anaconda[root@studentvm2 ~]#
This shows the DHCP client has been installed along with libraries and supporting files common to the client, server, and possibly the DHCP development packages.
- 4.The DHCP server is not installed, so we need to install it. a[root@studentvm2 ~]# dnf install -y dhcp-serverLast metadata expiration check: 2:39:06 ago on Wed 26 Dec 2018 12:19:46 PM EST.Dependencies resolved.======================================================================Package Arch Version Repository Size======================================================================Installing:dhcp-server x86_64 12:4.3.6-28.fc29 fedora 431 kTransaction Summary======================================================================Install 1 PackageTotal download size: 431 kInstalled size: 1.2 MDownloading Packages:dhcp-server-4.3.6-28.fc29.x86_64.rpm 120 kB/s | 431 kB 00:03----------------------------------------------------------------------Total 78 kB/s | 431 kB 00:05Running transaction checkTransaction check succeeded.Running transaction testTransaction test succeeded.Running transactionPreparing : 1/1Running scriptlet: dhcp-server-12:4.3.6-28.fc29.x86_64 1/1Installing : dhcp-server-12:4.3.6-28.fc29.x86_64 1/1Running scriptlet: dhcp-server-12:4.3.6-28.fc29.x86_64 1/1Verifying : dhcp-server-12:4.3.6-28.fc29.x86_64 1/1Installed:dhcp-server-12:4.3.6-28.fc29.x86_64Complete![root@studentvm2 ~]#
That was easy and no reboot of the server, StudentVM2, is required.
Configuring the DHCP server
With the DHCP server installed, the next step is to configure the server. Having more than one DHCP server on the same network can cause problems because one would never know which DHCP server is providing the network configuration data to the client. However, a single DHCP server on one host can listen to multiple networks and provide configuration data to clients on more than one network.
It is possible for DHCP to provide DNS names for the gateway and other servers. For example, the NTP server could use the hostname of that server, such as NTP1 instead of the IP address. Most of the time this would work well, but this configuration might cause problems if the DNS name services server were to be disabled or if our own server does not exist, such as right at the moment.

The specific IP Address map for the server and the workstation on our network
Before configuring and starting the DHCP server on StudentVM2, we will turn off the DHCP server that is part of the virtual router.
Experiment 3-2
In this experiment, we create a fairly simple configuration file, start the DHCP server, and then test it by determining that StudentVM1 receives the correct network configuration information. Be absolutely certain to use the MAC addresses for the specific hosts on your virtual network. As the non-root user on your physical host, turn off the virtual network DHCP server to prevent conflict. You need to use the command line to do this. Use the VirtualBox Manager and click the menu bar, File ➤ Preferences to open the preferences dialog box.
Click the Network tab on the left side of the Preferences dialog. Then click StudentNetwork.
Remove the check mark from the Supports DHCP check box. Note that the existing IP address leases will remain in effect until they expire, the client host NICs are turned down and then up again, or the systems are rebooted. We are not turning off the StudentNetwork, just its DHCP server. Click OK and then again OK in the Preferences dialog.
Now we can configure the DHCP server on StudentVM2.
As root, let’s look at the existing dhcpd.conf file. Make /etc/dhcp the PWD and then cat the dhcpd.conf file to view the content. There is not much there, but it does point to an example file, /usr/share/doc/dhcp-server/dhcpd.conf.example, which you can read in order to understand the main components and syntax of the dhcpd.conf file.
The dhcpd.conf(5) man page also has some excellent descriptions of the various configuration statements that we are likely to need.
Add the following statements to the bottom of the dhcpd.conf file. This first section contains configuration items that are global – common to all subnets that DHCP is configured to provide for. In our case, we have only a single subnet, but we still place these statements in the global section because they are likely to be the same for all subnets. If they were to differ for a given subnet, placing a statement with different values in the subnet declaration will override the global declaration.
Note that the host declaration has curly braces, {}, around the configuration declarations. The curly braces for the subnet declaration also surround the host declaration because the host declarations need to be inside the subnet declaration.
You should see no errors from the status command but you will see a number of statements indicating the DHCP daemon is listening on a specific NIC and the MAC address of the NIC. If this information is not correct, verify that the dhcpd.conf file is correct and try to restart. If there are syntactical errors in your configuration, they will show up in the status report.
If the StudentVM1 host is not already running, start it now. Nothing further will need to be done to obtain an IP address from the newly configured DHCP server.
Login to StudentVM1, open a terminal session, and su – to root.
If the dhcpd.conf file is still open in your editor, close it now.
A reboot is not required for either the DHCP client or the server. No configuration changes were required on the client, StudentVM1, and simply turning down the NIC and bringing it back up again enabled it to obtain the configuration data from the new DHCP server.
Configuring guest hosts
Configuring the network settings for guest hosts such as laptops and other mobile devices is also possible with DHCP. This implies that we have no information such as the MAC address for these computers and that we must assign an IP address anyway.
In most cases, this usage for DHCP, despite the fact that it was the original intention for DHCP, requires a good bit of trust be afforded to the guest hosts. I personally dislike having guests on my own network, so I usually find a way to set up a second network subnet to which I relegate all guest hosts. This protects my own network and improves security because the guest hosts have no access to it.
There are times when it becomes necessary to include guest computers in a network. The use of DHCP makes that not just possible but also easy. All we need to do is introduce a short stanza into our subnet configuration.
Experiment 3-3
This experiment must be performed as root.
Verify that there were no errors during the restart.
To test this DHCP guest allocation, create a brand new VM, StudentVM3 with a dynamically allocated 120GB hard drive and 4GB of RAM. Be sure the VM is in the StudentNetwork. Do not clone this VM from one of the other VMs we have already created. Boot to the Live USB image ISO file from which StudentVM1 was created. After the Live image boots, open a terminal session and verify that the network configuration is correct and that the IP address falls within the range specified in the declaration.
I have found that it is sometimes necessary to power off all open VMs and VirtualBox itself before restarting VirtualBox, then StudentVM2, and then the rest of the Student VMs in order to reset the VirtualBox DHCP server to off. This behavior is inconsistent and I have no current explanation for it. Just be aware that it can happen.
Perform some additional testing to ensure that network communication to the Internet is working properly.
After you have finished testing, you can power off the StudentVM3 virtual machine. Do not delete the VM you created for this test.
The dhcpd.conf file

The completed dhcpd.conf file
Chapter summary
DHCP is designed to provide network configuration data to client hosts on a network. This allows for centralization of network configuration management.
You now have a working DHCP server on StudentVM2. You have turned off the DHCP server embedded in the virtual router created for our virtual network to ensure that there are no conflicts between servers. You have tested the new DHCP server by using the StudentVM1 as a DHCP client to obtain the revised network configuration data from the new DHCP server. You have used typical Linux command line networking tools to verify that the new configuration is working properly.
A DHCP server can provide many configuration options to clients, including many required for Windows hosts that might connect to the network. This configuration data includes gateway routers, NTP servers, DNS servers, PXE boot servers, and much more. As we continue through this course, we will add configuration items for DNS and NTP servers to our DHCP configuration.
Exercises
- 1.
What is the function of DHCP?
- 2.
What five common configuration items are provided by DHCP to Linux hosts?
- 3.
How many name servers can be specified?
- 4.
Can servers and routers be specified by name as well as by IP address in the dhcpd.conf file? If so, what problems might arise?
- 5.
Based on the content of the DHCP configuration database we have created in this chapter, what IP address would likely be served to a new VM that booted up on the network?