Configuring a DHCPv4 Server

DHCPv4Server

Release date : March 4, 2017
Categories : Network
Configuring a DHCPv4 Server,dezhafzarnet(dezhafzar)

The dhcp package contains an Internet Systems Consortium (ISC) DHCP server. First, install the package as the superuser:

~]# yum install dhcp

Installing the dhcp package creates a file, /etc/dhcp/dhcpd.conf, which is merely an empty configuration file:

~]# cat /etc/dhcp/dhcpd.conf #DHCP Server Configuration file.# see /usr/share/doc/dhcp*/dhcpd.conf.sample

The sample configuration file can be found at /usr/share/doc/dhcp-V/dhcpd.conf.sample.

You should use this file to help you configure /etc/dhcp/dhcpd.conf, which is explained in detail below. DHCP also uses the file /var/lib/dhcpd/dhcpd.leases to store the client lease database. See Section 16.2.2, “Lease Database” for more information.

⁠16.2.1. Configuration File

The first step in configuring a DHCP server is to create the configuration file that stores the network information for the clients. Use this file to declare options and global options for client systems. The configuration file can contain extra tabs or blank lines for easier formatting. Keywords are case-insensitive and lines beginning with a hash sign (#) are considered comments. There are two types of statements in the configuration file:

  • Parameters — State how to perform a task, whether to perform a task, or what network configuration options to send to the client.
  • Declarations — Describe the topology of the network, describe the clients, provide addresses for the clients, or apply a group of parameters to a group of declarations.

The parameters that start with the keyword option are referred to as options. These options control DHCP options; whereas, parameters configure values that are not optional or control how the DHCP server behaves. Parameters (including options) declared before a section enclosed in curly brackets ({ }) are considered global parameters. Global parameters apply to all the sections below it.

Important

If the configuration file is changed, the changes do not take effect until the DHCP daemon is restarted with the command service dhcpd restart.

Note

Instead of changing a DHCP configuration file and restarting the service each time, using the omshellcommand provides an interactive way to connect to, query, and change the configuration of a DHCP server. By using omshell, all changes can be made while the server is running. For more information on omshell, see the omshell man page. In Example 16.1, “Subnet Declaration”, the routers, subnet-mask, domain-search, domain-name-servers, and time-offset options are used for any host statements declared below it. For every subnet which will be served, and for every subnet to which the DHCP server is connected, there must be one subnet declaration, which tells the DHCP daemon how to recognize that an address is on that subnet. A subnet declaration is required for each subnet even if no addresses will be dynamically allocated to that subnet. In this example, there are global options for every DHCP client in the subnet and a range is declared. Clients are assigned an IP address within the range.

Example 16.1. Subnet Declaration

Configuring a DHCPv4 Server,dezhafzarnet(dezhafzar)

To configure a DHCP server that leases a dynamic IP address to a system within a subnet, modify Example 16.2, “Range Parameter” with your values. It declares a default lease time, maximum lease time, and network configuration values for the clients. This example assigns IP addresses in the range 192.168.1.10 and 192.168.1.100 to client systems.

Example 16.2. Range Parameter

Configuring a DHCPv4 Server,dezhafzarnet(dezhafzar)

To assign an IP address to a client based on the MAC address of the network interface card, use the hardware ethernet parameter within a host declaration. As demonstrated in Example 16.3, “Static IP Address Using DHCP”, the host apex declaration specifies that the network interface card with the MAC address 00:A0:78:8E:9E:AA always receives the IP address 192.168.1.4. Note that you can also use the optional parameter host-name to assign a hostname to the client.

Example 16.3. Static IP Address Using DHCP

Configuring a DHCPv4 Server,dezhafzarnet(dezhafzar)

All subnets that share the same physical network should be declared within a shared-network declaration as shown in Example 16.4, “Shared-network Declaration”. Parameters within the shared-network, but outside the enclosed subnet declarations, are considered to be global parameters. The name of the shared-network must be a descriptive title for the network, such as using the title ‘test-lab’ to describe all the subnets in a test lab environment.

Example 16.4. Shared-network Declaration

Configuring a DHCPv4 Server,dezhafzarnet(dezhafzar)

As demonstrated in Example 16.5, “Group Declaration”, the group declaration is used to apply global parameters to a group of declarations. For example, shared networks, subnets, and hosts can be grouped.

Example 16.5. Group Declaration

Configuring a DHCPv4 Server,dezhafzarnet(dezhafzar)

using the sample configuration file

You can use the provided sample configuration file as a starting point and add custom configuration options to it. To copy this file to the proper location, use the following command as root: ~]# cp /usr/share/doc/dhcp-/dhcpd.conf.sample /etc/dhcp/dhcpd.conf … where is the DHCP version number. For a complete list of option statements and what they do, see the dhcp-options man page.

Lease Database

On the DHCP server, the file /var/lib/dhcpd/dhcpd.leases stores the DHCP client lease database. Do not change this file. DHCP lease information for each recently assigned IP address is automatically stored in the lease database. The information includes the length of the lease, to whom the IP address has been assigned, the start and end dates for the lease, and the MAC address of the network interface card that was used to retrieve the lease. All times in the lease database are in Coordinated Universal Time (UTC), not local time. The lease database is recreated from time to time so that it is not too large. First, all known leases are saved in a temporary lease database. The dhcpd.leases file is renamed dhcpd.leases~ and the temporary lease database is written to dhcpd.leases. The DHCP daemon could be killed or the system could crash after the lease database has been renamed to the backup file but before the new file has been written. If this happens, the dhcpd.leases file does not exist, but it is required to start the service. Do not create a new lease file. If you do, all old leases are lost which causes many problems. The correct solution is to rename the dhcpd.leases~ backup file to dhcpd.leases and then start the daemon.

Starting and Stopping the Server

When the DHCP server is started for the first time, it fails unless the dhcpd.leases file exists. Use the command touch /var/lib/dhcpd/dhcpd.leases to create the file if it does not exist. If the same server is also running BIND as a DNS server, this step is not necessary, as starting the namedservice automatically checks for a dhcpd.leases file. To start the DHCP service, use the command /sbin/service dhcpd start. To stop the DHCP server, use the command /sbin/service dhcpd stop. By default, the DHCP service does not start at boot time. For information on how to configure the daemon to start automatically at boot time, see Chapter 12, Services and Daemons. If more than one network interface is attached to the system, but the DHCP server should only be started on one of the interfaces, configure the DHCP server to start only on that device. In /etc/sysconfig/dhcpd, add the name of the interface to the list of DHCPDARGS: # Command line options here DHCPDARGS=eth0. This is useful for a firewall machine with two network cards. One network card can be configured as a DHCP client to retrieve an IP address to the Internet. The other network card can be used as a DHCP server for the internal network behind the firewall. Specifying only the network card connected to the internal network makes the system more secure because users can not connect to the daemon via the Internet.

Other command-line options that can be specified in /etc/sysconfig/dhcpd include:

  • -p — Specifies the UDP port number on which dhcpd should listen. The default is port 67. The DHCP server transmits responses to the DHCP clients at a port number one greater than the UDP port specified. For example, if the default port 67 is used, the server listens to port 67 for requests and responds to the client on port 68. If a port is specified here and the DHCP relay agent is used, the same port on which the DHCP relay agent should listen must be specified. See Section 16.2.4, “DHCP Relay Agent” for details.
  • -f — Runs the daemon as a foreground process. This is mostly used for debugging
  • -d — Logs the DHCP server daemon to the standard error descriptor. This is mostly used for debugging. If this is not specified, the log is written to /var/log/messages.
  • -cf — Specifies the location of the configuration file. The default location is /etc/dhcp/dhcpd.conf.
  • -lf — Specifies the location of the lease database file. If a lease database file already exists, it is very important that the same file be used every time the DHCP server is started. It is strongly recommended that this option only be used for debugging purposes on non-production machines. The default location is /var/lib/dhcpd/dhcpd.leases.
  • -q — Do not print the entire copyright message when starting the daemon.

DHCP Relay Agent

The DHCP Relay Agent (dhcrelay) allows for the relay of DHCP and BOOTP requests from a subnet with no DHCP server on it to one or more DHCP servers on other subnets. When a DHCP client requests information, the DHCP Relay Agent forwards the request to the list of DHCP servers specified when the DHCP Relay Agent is started. When a DHCP server returns a reply, the reply is broadcast or unicast on the network that sent the original request. The DHCP Relay Agent listens for DHCP requests on all interfaces unless the interfaces are specified in /etc/sysconfig/dhcrelay with the INTERFACES directive. To start the DHCP Relay Agent, use the command service dhcrelay start.

Configuring a DHCPv4 Client

To configure a DHCP client manually, modify the /etc/sysconfig/network file to enable networking and the configuration file for each network device in the /etc/sysconfig/network-scripts directory. In this directory, each device should have a configuration file named ifcfg-eth0, where eth0 is the network device name. Make sure that the /etc/sysconfig/network-scripts/ifcfg-eth0 file contains the following lines:

Configuring a DHCPv4 Server,dezhafzarnet(dezhafzar)

To use DHCP, set a configuration file for each device. Other options for the network script include:

  • DHCP_HOSTNAME — Only use this option if the DHCP server requires the client to specify a hostname before receiving an IP address.

PEERDNS=, where is one of the following:

  • yes — Modify /etc/resolv.conf with information from the server. This is the default.
  • no — Do not modify /etc/resolv.conf.

If you prefer using a graphical interface, see Chapter 10, NetworkManager for instructions on using NetworkManager to configure a network interface to use DHCP. Note For advanced configurations of client DHCP options such as protocol timing, lease requirements and requests, dynamic DNS support, aliases, as well as a wide variety of values to override, prepend, or append to client-side configurations, see the dhclient and dhclient.conf man pages.

DHCPv4Server

Collection and compilation: Dezhafzarnet Company

Latest Articles

The Latest Articles are available

ContactUs

Dezhfarenet SocialNetworks

You can contact us through the following networks, I will respond as soon as possible