Dynamic Host Configuration Protocol

Dynamic Host Configuration Protocol (DHCP) is a network protocol that automatically assigns TCP/IP information to client machines. Each DHCP client connects to the centrally located DHCP server, which returns that client's network configuration (including the IP address, gateway, and DNS servers).

DHCP operational overview

As with most network services there is a server side and a client side to DHCP. The examples use the DHCP daemon on the server side, and the pump (is a client program) executable on the client side. There are other packages available, but these binaries are the ones installed with Red Hat by default.

  • Provides dynamic configuration and network information to hosts. IP address.

  • DNS servers.

  • Netbios name servers. Gateways.

  • Domain name.

  • Only one DHCP server per network segment. Uses broadcast packets to retrieve information. Superset of bootp.

  • Can answer requests from bootp clients.

Why Use DHCP?

  • DHCP is useful for automatic configuration of client network interfaces.

  • When configuring the client system, the administrator chooses DHCP instead of specifying an IP address, net mask, gateway, or DNS servers.

  • The client retrieves this information from the DHCP server.

  • DHCP is also useful if an administrator wants to change the IP addresses of a large number of systems. Instead of reconfiguring all the systems, he can just edit one DHCP configuration file on the server for the new set of IP addresses.

  • If the DNS servers for an organization changes, the changes are made on the DHCP server, not on the DHCP clients. When the administrator restarts the network or reboots the clients, the changes will go into effect.

Configuration File

The first step in configuring a DHCP server is to create the configuration file that stores the network information for the clients.

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 reffered 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.

DHCP Servers with Multiple NICs

DHCP servers with multiple interfaces pose two configuration challenges. The first is setting up the correct routing and the second is making sure only the required interfaces are listening to serve DHCP. . Routing

When a DHCP configured PC boots, it requests its IP address from the DHCP server. It does this by sending a standardized DHCP broadcast request packet to the DHCP server with a source IP address of 255.255.255.255.

If your DHCP server has more than one interface, you have to add a route for this 255.255.255.255 address so that it knows the interface on which to send the reply; if not, it sends it to the default gateway.

You can temporarily add a route to 255.255.255.255 using the route add
command as seen below. [root@wilshire~]# route add -host 255.255.255.255
dev eth0
Configuring DHCP
#yum install dhcp*
#cp /usr/share/doc/dhcp*/dhcpd.conf.sample /etc/dhcpd/dhcp.conf
#vi /etc/dhcpd/dhcp.conf
#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;
#default-lease-time 600;
#max-lease-time 7200;
authoritative;
log-facility local6;
#subnet 10.152.187.0 netmask 255.255.255.0 {
#}
# This is a very basic subnet declaration.
subnet 192.189.0.0 netmask 255.255.255.0
{
range192.189.0.180 192.189.0.220;
# option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
}
#subnet 10.254.239.32 netmask 255.255.255.224 {
# range dynamic-bootp 10.254.239.40 10.254.239.60;
#option broadcast-address 10.254.239.31;
#option routers rtr-239-32-1.example.org;
#}
# A slightly different configuration for an internal subnet.
#subnet 10.5.5.0 netmask 255.255.255.224 {
#  range 10.5.5.26 10.5.5.30;
#  option domain-name-servers ns1.internal.example.org;
#  option domain-name "internal.example.org";
#  option routers 10.5.5.1;
#  option broadcast-address 10.5.5.31;
#  default-lease-time 600;
#  max-lease-time 7200;
#}
Comment all the lines till end of the file
Now restart the dhcp daemon
#service named restart

Lease Database

  • On the DHCP server, the file /var/lib/dhcp/dhcpd.leases stores the DHCP client lease database. This file should not be modified by hand.

  • 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.

  • 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.

15.6 Starting the DHCP Server
[root@wilshire~]# chkconfig dhcpd on
To start, stop, and restart dhcpd after booting
[root@wilshire~]# service dhcpd start
[root@wilshire~]# service dhcpd stop
[root@wilshire~]# service dhcpd restart
[root@wilshire~]# pgrep dhcpd
[root@wilshire~]#service dhcpd status

Note: Remember to configure dhcpd to start automatically on your next reboot.

Configuring Linux clients to use DHCP

Linux NIC cards can be configured to dynamically get their IP addresses from a DHCP server by editing the interface scripts in the /etc/sysconfig/network-scripts directory.

Here is an example shows how to configure the DHCP client:

[root@wilshire~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
bootproto=dhcp
wq!

Here bootproto=dhcp tells the system to get an IP address from the DHCP server during the boot time# We can also get an IP address from DHCP server by using following command:

[root@wilshire~]# dhclient
To release dhcp ip from server to client
[root@wilshire~]#dhclient –r

Subscribe For More Content