If a connection to a network needs to be made, every computer needs a correct Internet Protocol (IP) configuration installed on their system to communicate. Assigning IP client configurations automatically from a central point using the Dynamic Host Control Protocol (DHCP) can make the administrator’s life easier and simplify the process of adding new machines to a network in comparison to the tedious work of manually setting up static IP information on each computer system in your network. In small home-based networks, people often use DHCP servers directly installed in silico on their Internet routers, but such devices often lack advanced features and have only a basic set of configuration options available. Most of the time, this is not sufficient for bigger networks or in the corporate environment, where you are more likely to find dedicated DCHP servers for more complex scenarios and better control. In this process, we will show you how to install and configure a DHCP server on a CentOS 7 system.
To Start With: What Do You Need?
To complete this process, you will require a working installation of the CentOS 7 operating system with root privileges, a console-based text editor of your choice, and a connection to the Internet in order to facilitate the download of additional packages. It is expected that your DHCP server will be using a static IP address; if you do not have one, refer to the process Building a static network connection, Configuring the System. If you plan to send DNS information to the clients through DHCP as well, you should have already applied the process Installing and configuring a simple nameserver, Working with FTP.
The Process
Here in this example, we will configure a DHCP server for a static network interface serving a single network with all its available IP addresses to all the computers connected directly to it (they are all in the same subnet).
- First, log in as root and type the following command in order to install the DHCP server packages:
yum install dhcp
- In our example, we will use a network interface with the name, ifcfg-enp5s0f1, to serve our DHCP requests. Next, we need to collect some very important network information, which we will use later for configuring the DHCP server (change the network interface name to fit your own needs):
cat /etc/sysconfig/network-scripts/ifcfg-enp5s0f1
- From this output, we need the following information, so please write it down (most likely, your output will be different):
BOOTPROTO="static"
IPADDR="192.168.1.8"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.254" - We also need the subnet network address, which can be calculated using the following line:
ipcalc -n 192.168.1.8/24
- This will print the following output (write it down for later):
NETWORK=192.168.1.0
- Now, we will open our main DHCP configuration file, after we make a backup of the original file:
cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.BAK vi /etc/dhcp/dhcpd.conf
- Append the following lines to the end of the file, taking into account your individual network interface’s configuration from the preceding steps (routers = GATEWAY, subnet = NETWORK):
authoriative;
default-lease-time 28800;
max-lease-time 86400;
shared-network MyNetwork {
option domain-name "example.com";
option domain-name-servers 8.8.8.8, 8.8.4.4;
option routers 192.168.1.254;
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.10 192.168.1.160;
} - Finally, start and enable the DHCP service:
systemctl start dhcpd
systemctl enable dhcpd
How Does It Work?
Here in this process, we showed you how easy it is to set up a DHCP server for a single network. With this, every time a new machine gets added to the network, the computer gets the correct IP information automatically, which it needs in order to connect to the network without any further human action.
So, what did we learn from this experience?
We started this process by installing the DHCP server package because it does not come with CentOS 7 out-of-the-box. Since our DHCP daemon communicates with its clients to assign IP information over a network interface, in the next step we had to choose a network device that would be used for the service. In our example, we selected a device named enp5s0f1. By default, the DHCP server can manage all available IP addresses from the same subnet as the associated network interface. Remember that your primary DHCP server’s network interface must be configured to get its own IP information statically and not through (another) DHCP server! Next, we used the cat command to print out all the interesting lines from our enp5s0f1 network interface configuration file, which we will need for configuring the DHCP server. Afterwards, we used the ipcalc tool to calculate the (subnet) network address for our DHCP server’s network interface. Then, we opened the main DHCP server configuration, started configuring some global settings, and defined a new shared network. In the global settings, we first set our DHCP server to be authoritative, which means it is the only and main responsible DHCP server in the network. Next, we defined default-lease-time to 28800 seconds, which is eight hours, and the max-lease-time to 86400, which is 24 hours. The lease time is the amount of time the DHCP server “rents out” an IP address to a client before it has to sign up again on the DHCP server asking for an extension of the IP. If it is not requesting a renewal of an existing lease at that time, the IP address will be released from the client and put into the pool of free IP addresses again, ready to be served to new machines that want to connect to the network. The client can define the amount of time it wants to lease an IP address by itself. If no time frame has been supplied from the client to the DHCP server, the default lease time will be used.
All subnets that share the same physical network interface should be defined within a shared-network declaration, so we defined this area using square brackets. This is also called a scope. In our example, we only have one network, so we only need one shared-network scope. Within it, we first defined a domain-name option, which will be sent and can be used by clients as their base domain name. Next, we added the domain name servers (DNS) to our configuration. Sending DNS information to the client is not mandatory for the DHCP server but can be useful. The more information a client gets for a given network, the better because fewer manual configuration steps have to be made.
Note
You can send out a lot of other useful information to the client (using DHCP) about the network he is connecting to: gateway, time, WINS, and so on.
Here in our example, we used the official Google DNS servers; if you have already set up your own DNS server, you could also use these addresses here. Next, we specified a routers option, which is another useful piece of information that will be sent out to the clients as well. Afterwards, we specified the most important part of any DHCP server: the subnet scope. Here, we defined our network ranges for assigning IP addresses for clients. We need to provide the subnet network address, its submask, and then the starting and ending IP address range that we want to allow to clients. In our example, we allow host IP addresses from 192.168.1.10, 192.168.1.11, 192.168.1.12 … to 192.168.1.160. If you have more than one subnet, you can use multiple subnet scope directives (called a multihomed DHCP server).
Next, we started the DHCP server and enabled it on boot. Your clients should now be able to get IP addresses dynamically from our new system.
In summary, we have only shown you some very basic DHCP server configuration options to get you started, and there are many more settings available, letting you build very complex DHCP server solutions. To get a better overview of its possibilities, please have a look at the example configuration file provided with the DHCP server documentation at less /usr/share/doc/dhcp-4*/dhcpd.conf.example.
There's more…
In the main process, we configured our basic DHCP server to be able to send complete IP network information to our clients so that they should be able to join our network. To use this server, you need to enable DHCP addressing on your client’s network interfaces. On CentOS clients, please do not forget to use BOOTPROTO=dhcp and remove all static entries such as IPADDR in the appropriate network-scripts ifcfg file (read the process, Building a static network connection, Configuring the System to get you started on network-scripts files). Then, to make a DHCP request, restart the network using systemctl restart network or try to do a reboot of the client system (with the ONBOOT=yes option). Confirm with ip addr list.