Contents[Hide]

1. What is DHCP?

Anyone with a basic knowledge of computer networking knows that in order for two hosts communicate on the same network using TCP/IP model, both hosts need to have an unique IP address. There are two ways on how a network host can obtain an IP address.

One way is to manually configure network interface and assign an IP address by hand. This is called static configuration which means that host's IP address will never change until changed manually again by user or system administrator. If company's network includes more than 1000 hosts this job of setting up each individual host with static IP address will become tiresome and more importantly inefficient.

Another way how to set host's IP address and this is also a solution to the above issue would be to have some way to automatically assign an IP address to any host on the network upon the request. This is where DHCP ( Dynamic Host Configuration Protocol ) comes handy.

The DHCP protocol lets a DHCP client to lease network configuration parameters such as an IP address. In fact parameters to lease are not limited to IP address only and they also include:

  • IP addresses and network masks
  • Domain Names servers ( DNS )
  • Default Gateways
  • WINS servers
  • Syslog hosts
  • Proxy servers
  • NTP servers
  • X Font servers
  • Syslog hosts

If you still have some questions after reading this article please try our new LinuxCareer Forum.

Each host set to obtain an IP address dynamically will upon boot send a DHCP request over the network ( by definition this is a broadcast of all 1's ) to discover whether there is a DHCP server available on the network and consequently ask for an network configuration. DHCP client is then obligated to maintain a communication with DHCP server and renew its IP address regularly as dictated by IP address's lease time expiry. In case that DHCP client fails to renew its IP address ( disconnection, client is turned off and etc. ) its IP address expires and DHCP server is free to lease this IP address to another DHCP client.

DHCP server keeps a record of all leased IP addresses and stores them into a file called dhcpd.leases which can be found in /var/lib/dhcp directory ( location of this file may vary depending on Linux system in use ). Having such a file allows DHCP server to keep track of all IP address leases even after its reboot or power failure.

Here are some advantages of having a DHCP server connected to network:

  • No IP address conflicts. DHCP can guarantee that all hosts on the network will have unique IP address. DHCP server keeps a record of all IP addresses assigned and cross reference them with host's MAC addresses.
  • Based on the MAC address DHCP allows for a fixed parameter configuration for a specific host
  • Efficiency with minimum local client configuration

2. DHCP Server Installation

Standard DHCP server implementation available in various Linux distributions is an Open source version maintained by ISC ( Internet System Consortium ). There are currently 3 major versions 2, 3, 4 where version 3 supports backup servers, and version 4 supports IPv6. This article deals only with ISC DHCP v3.

Use the following commands to install DHCP on your Linux server:

Debian and Ubuntu:

# apt-get install dhcp3-server

Redhat & Fedora:

# yum install dhcp

3. Basic DHCP Configuration

By default DHCP server configuration does not include any subnets on which DHCP server should lease IP addresses. Therefore, depends on your Linux system you may get a following error message when you attempt to start DHCP with default dhcpd.conf configuration file.

Starting ISC DHCP server: dhcpdcheck syslog for diagnostics. ... failed!

Checking a log files and particularly /var/log/syslog reveals this problem in more detail:

No subnet declaration for eth0 (some IP address). 

As it happens very often your server may be connected to multiple network subnets. In order to start DHCP server at least one subnet must be defined in DHCP configuration file /etc/dhcp/dhcpd.conf.

NOTE: if your server has access to more than one subnet, DHCP requires all subnets to be defined even though there isn't immediate intention to enable DHCP service on that subnet.

Below is the simplest example of DHCP configuration file:

subnet 10.1.1.0 netmask 255.255.255.0 {
  range 10.1.1.3 10.1.1.254;
}

subnet 192.168.0.0 netmask 255.255.0.0 {
}

This configuration file instructs DHCP server to listen for DHCP client requests on subnet 10.1.1.0 with netmask 255.255.255.0. Furthermore, it will assign IP addresses in range 10.1.1.3 - 10.1.1.254. It also defines an empty definition of subnet with network ID 192.168.0.0.

Alter above code with your subnet and insert it into /etc/dhcp/dhcpd.conf. When ready restart your DHCP server with ( restart command may vary ) :

# /etc/init.d/isc-dhcp-server restart

4. DHCP default and max lease time

At this point we can add to our DHCP configuration another setting and that is to set default and max lease time expiry.

  • default-lease-time is a value in seconds in which a leased IP address expiry will be set to if DHCP client does not ask for any other specific expiry lease time
  • max-lease-time is a value in seconds which defines a maximum expiry time for an IP address leased by DHCP server
default-lease-time 600;
max-lease-time 7200;

subnet 10.1.1.0 netmask 255.255.255.0 {
  range 10.1.1.3 10.1.1.254;
}

subnet 192.168.0.0 netmask 255.255.0.0 {
}

5. Define DNS server

Another configuration parameter possible to be set by DHCP server to its client is a definition of DNS server. If you want your clients to use DNS server with an IP address 8.8.8.8 and 10.1.1.1 you can do it by including an option "domain-name-servers" to DHCP's configuration file.

default-lease-time 600;
max-lease-time 7200;

subnet 10.1.1.0 netmask 255.255.255.0 {
  range 10.1.1.3 10.1.1.254;
  option domain-name-servers 10.1.1.1, 8.8.8.8;
}

subnet 192.168.0.0 netmask 255.255.0.0 {
}

subnet 10.1.1.0 netmask 255.255.255.0 {
  range 10.1.1.3 10.1.1.254;
  option routers 10.1.1.1;
}

6. Set default gateway

DHCP also allows for client's gateway configuration.To set any client on the local network to use default gateway 10.1.1.1, add line "option routers 10.1.1.1" into dhcpd.conf file as demonstrated below:

default-lease-time 600;
max-lease-time 7200;

subnet 10.1.1.0 netmask 255.255.255.0 {
  range 10.1.1.3 10.1.1.254;
  option domain-name-servers 10.1.1.1, 8.8.8.8;
  option routers 10.1.1.1;
}

subnet 192.168.0.0 netmask 255.255.0.0 {
}

subnet 10.1.1.0 netmask 255.255.255.0 {
  range 10.1.1.3 10.1.1.254;
  option routers 10.1.1.1;
}

DHCP will now set DHCP client with gateway 10.1.1.1.

7. Host specific configuration

There maybe a need to set static IP address to a particular host on the network such as printer, web server and etc. In this case it is posible to amend DHCP server configuration to lease a choosen IP address to a specific host defined by its MAC address.

default-lease-time 600;
max-lease-time 7200;

subnet 10.1.1.0 netmask 255.255.255.0 {
  range 10.1.1.3 10.1.1.254;
  option domain-name-servers 10.1.1.1, 8.8.8.8;
  option routers 10.1.1.1;
}

subnet 192.168.0.0 netmask 255.255.0.0 {
}

host printer {
  hardware ethernet 00:16:d3:b7:8f:86;
  fixed-address 10.1.1.100;
}

host web-server {
  hardware ethernet 00:17:a4:c2:44:22;
  fixed-address 10.1.1.200;
}

The above DHCP configuration file will permanently assign the IP address 10.1.1.100 to a host "printer" with a MAC address 00:16:d3:b7:8f:86 and IP address 10.1.1.200 to host "web-server" with MAC address 00:17:a4:c2:44:22.

8. Appendix

8.1. Ubuntu client network settings to use DHCP

To configure your client to use a DHCP on a network interface eth0 on Ubuntu or Debian linux systems enter a following lines in your /etc/network/interfaces file:

auto eth0
iface eth0 inet dhcp

8.2. Configuring DHCP relay agent

If your DHCP server does not have an access to some particular subnet it does not mean that it cannot provide its services there. To make this work a DHCP relay agent needs to be configured on a remote subnet which forwards all request to a specified DHCP server and distant subnet. First install DHCP relay agent:

# apt-get install isc-dhcp-relay

Then create a configuration file /etc/default/isc-dhcp-relay with two lines:

SERVERS="192.168.5.5"
INTERFACES="eth1"

The above config file will instruct DHCP relay agent to listen on eth0 interface for a DHCP client requests and forward them to a DHCP server on an IP address 192.168.5.5.

8.3. BOOTP Support

ISC DHCP server is backward compatible with BOOTP. The following is a BOOTP client declaration to be defined in DHCP's main configuration dhcpd.conf file:

host bootp {
              hardware ethernet 00:00:2e:55:12:09;
              fixed-address 123.123.1.3;
              filename "/path/to/tftpboot/bootp.boot";
            }


Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top