Configuring a DHCP Server

Configuring a DHCP Server

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

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-<version_number>/dhcpd.conf.sample /etc/dhcp/dhcpd.conf

… where <version_number> is the DHCP version number.

 

Configuration File

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.

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.

 Subnet Declaration

default-lease-time 600;

max-lease-time 7200;

subnet 192.168.1.0 netmask 255.255.255.0 {

        option routers                  192.168.1.254;

        option subnet-mask              255.255.255.0;

        option domain-search              “syadminslab.com”;

        option domain-name-servers       192.168.1.1;

        option time-offset              -18000;     # Eastern Standard Time

        range 192.168.1.10 192.168.1.100;

        range 192.168.1.150 192.168.1.200;

}

 

This will result in DHCP server giving a client an IP address from the range 192.168.1.10-192.168.1.100 or 192.168.1.150-192.168.1.200. It will lease an IP address for 600 seconds if the client doesn’t ask for specific time frame. Otherwise the maximum (allowed) lease will be 7200 seconds. The server will also “advise” the client that it should use 255.255.255.0 as its subnet mask, 192.168.1.255 as its broadcast address, 192.168.1.254 as the router/gateway and 192.168.1.1 as its DNS servers.

Note that you can also use the optional parameter host-name to assign a host name to the client.

Static IP Address Using DHCP

host host0 {

   option host-name “host0.sysadminslab.com”;

   hardware ethernet 04:4B:82:82:82:02;

   fixed-address 192.168.1.24;

}

 

 Lease Database

There is only one thing to do before starting the server. In most cases DHCP installation doesn’t create a dhcpd.leases files. This file is used by DHCPd to store information about current leases. It is in the plain text form so you can view it during the operation of DHCPd. To create dhcpd.leases type:

touch /var/state/dhcp/dhcpd.leases

This will create an empty file (file size = 0). Some of the older version of dhcpd 2.0 placed the file in /etc/dhcpd.leases. You do not need to make any changes to the leases file it will be manipulated by the dhcpd. If you get a message saying that file exists simply ignore it and go to the next step.

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.

You can now invoke the DHCP server. Simply type (or include in the bootup scripts)

/usr/sbin/dhcpd

This will invoke dhcpd on eth0 device. If you want to invoke it on another device simply supply it on the command line e.g.

/usr/sbin/dhcpd eth1

To verify that everything is working fine you should first turn on the debugging mode and put the server in foreground. You can do this by typing

/usr/sbin/dhcpd -d -f

 

Then boot up one of your clients and check out the console of your server. You will see a number of debugging messages come up. If everything works out fine you are done :-). Quit dhcpd and start it without the -d -f and arguments.

Below are advanced and not tested:-

All subnets that share the same physical network should be declared within a 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.

Shared-network Declaration

shared-network name {

    option domain-search            “test.sysadminslab.com”;

    option domain-name-servers      ns1.sysadminslab.com, ns2.sysadminslab.com;

    option routers                  192.168.0.254;

    #more parameters for TEST-LAB shared-network

    subnet 192.168.1.0 netmask 255.255.252.0 {

        #parameters for subnet

        range 192.168.1.1 192.168.1.254;

    }

    subnet 192.168.2.0 netmask 255.255.252.0 {

        #parameters for subnet

        range 192.168.2.1 192.168.2.254;

    }

}

 

The group declaration is used to apply global parameters to a group of declarations. For sysadminslab, shared networks, subnets, and hosts can be grouped.

 

group {

   option routers                  192.168.1.254;

   option subnet-mask              255.255.255.0;

   option domain-search              “sysadminslab.com”;

   option domain-name-servers       192.168.1.1;

   option time-offset              -18000;     # Indian Standard Time

   host host0 {

      option host-name “host0.sysadminslab.com”;

      hardware ethernet 04:4B:82:82:82:02;

      fixed-address 192.168.1.4;

   }

   host host1 {

      option host-name “host1.sysadminslab.com”;

      hardware ethernet 04:4B:82:82:82:03;

      fixed-address 192.168.1.6;

   }

}

 

For a complete list of option statements and what they do, see the dhcp-options man page.