CentOS 7 Server Deployment Cookbook
上QQ阅读APP看书,第一时间看更新

Installing a DHCP server

This recipe will show you how to set up your own DHCP server on CentOS. DHCP is used to assign IP addresses and other network configuration details on demand to a client. While a system configured with a static IP address will already know all the necessary networking details, a system configured to use DHCP broadcasts a request on the network and waits to receive a response from the DHCP server.

Getting ready

This recipe requires a CentOS system with a working network connection. You'll also need administrative privileges provided by logging in with the root account.

Note

Only one DHCP server should be running on the network to prevent clients from receiving conflicting responses that can result in network instability. Many routers already have a DHCP service running on them, so check for this on your own network before proceeding.

How to do it...

Follow these steps to set up a DHCP server:

  1. Install the dhcp package:
    yum install dhcp
    
  2. Copy the example configuration file provided by the package to serve as the starting point of your server's configuration:
    cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
    
  3. Open the configuration file using your text editor:
    vi /etc/dhcp/dhcpd.conf
    
  4. Modify the configuration with values that make sense for your environment. In particular, you'll want to address the following options: domain-name and domain-name-servers, subnet, the dynamic-bootp range, broadcast-address, and routers. Here is an example configuration for a network of two subnets:
    # option definitions common to all supported networks
    option domain-name localdomain;
    option domain-name-servers ns1.localdomain;
    default-lease-time 600;
    max-lease-time 7200;
    # This DHCP server is the official DHCP server for the
    # local network
    authoritative;
    # No service will be given on this subnet, but declaring
    # it helps the server to understand the network topology.
    subnet 192.168.56.0 netmask 255.255.255.0 {
    }
    # This is a basic subnet declaration
    subnet 192.168.56.0 netmask 255.255.255.128 {
     range 192.168.56.110 192.168.56.120;
     option domain-name-servers ns1.localdomain;
     option domain-name "localdomain";
     option routers 192.168.56.1;
     option broadcast-address 192.168.56.127;
    }
    # This is the second subnet
    subnet 192.168.56.128 netmask 255.255.255.128 {
     range 192.168.56.200 192.168.56.210;
     option domain-name-servers ns2.sub.localdomain;
     option domain-name "sub.localdomain";
     option routers 192.168.56.129;
     option broadcast-address 192.168.56.255;
    }
    
  5. Save your changes and close the file.
  6. Start the dhcp service and enable it to start at system boot:
    systemctl start dhcpd
    systemctl enable dhcpd
    
  7. Open ports 67 and 68 in the system's firewall to allow traffic:
    firewall-cmd --zone=public --permanent --add-service=dhcp
    firewall-cmd --reload
    

How it works...

A system configured to use DHCP will broadcast a request and wait to receive a response from the DHCP server. The server's response lets the client know which IP address, netmask, gateway information, and so on to use on the network. DHCP-provisioned addresses are usually leased, which means that after a set amount of time they expire and the client needs to send another request. The DHCP server, in addition to handing out connection details, must keep track of the addresses that have already been leased so that a client doesn't receive an address that's already in use by another system.

We began by installing the dhcpd package, which contains the server and example configuration files. Copying the example configuration to use as a starting point for our own saves us from having to draft the entire configuration from scratch:

cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf

In the configuration file, there are several places where you need to provide values that make sense for your network. The minimal configuration file provided as an illustration in the recipe reflects a network divided into two subnets. The first subnet is 192.168.56.0/25 and the second is 192.168.56.128/25. Each subnet has its own declaration.

Examining the first subnet declaration, the subnet's ID is 192.168.56.0 with a netmask of 255.255.255.128. The range option will restrict the DHCP server in assigning IP addresses in the range of 192.168.56.110 to 120 (the other addresses are still valid and are available for static assignment). Subsequent option entries provide the subnet's broadcast-address and gateway, and override the domain name and nameservers defined globally:

# This is a basic subnet declaration
subnet 192.168.56.0 netmask 255.255.255.128 {
 range 192.168.56.110 192.168.56.120;
 option domain-name-servers ns1.localdomain;
 option domain-name "localdomain";
 option routers 192.168.56.1;
 option broadcast-address 192.168.56.127;
}

Configuring a DHCP server properly requires an understanding of computer networking. It is a complex topic and, as such, we can't discuss every option in detail. I advise you to read the manual page for dhcpd.conf for extra guidance. The page can be accessed using the man command:

man 5 dhcpd.conf

The configuration file for dhcpd is documented in a manual page

Once the DHCP server was configured and running, we then needed to poke a hole in the firewall to allow requests and responses to flow freely. DHCP requests occur using UDP and ports 57 and 58 (you can allow them using the service defined for FirewallD):

firewall-cmd --zone=public --permanent --add-service=dhcp
firewall-cmd --reload

See also

For more information on setting up a DHCP server, refer to the following resources: