Friday 25 November 2011

DHCP in Linux


Dynamic Host Configuration Protocol (DHCP)




1. Introduction

The Dynamic Host Configuration Protocol (DHCP) provides configuration parameters to the Internet hosts. On a typical LAN, a DHCP server allocates network addresses and delivers network configuration parameters such as the gateway, netmask, DNS, etc. DHCP supports three mechanisms to allocate IP addresses:

  • Automatic allocation: A permanent IP address is allocated to a client.
  • Dynamic allocation: An IP address is assigned for a limited period of time by the DHCP server.
  • Manual allocation: The network administrator explicitly assigns an IP address to a particular client and DHCP is used to convey the assigned address.

This guide will demonstrate how to set up a DHCP server and client. More information regarding the protocol can be found in the “References” section.

2. Installtion

Many distributions come with a dhcpd server and client. In case you would like to install it manually, however, you can use rpm -ivh dhcp-VERSION.rpm to install the rpm. The DHCP server can be downloaded from "http://people.redhat.com/~jvdias/DHCP/". DHCP reads from a configuration /etc/dhcpd.conf.  The rpm package does not install this file, but you can use the sample DHCP configuration file that can be found under /usr/share/doc/dhcp-<version-number>/dhcpd.conf.sample. Copy this file to /etc and rename it to dhcpd.conf.

            # cp /usr/share/doc/dhcp-3.0.1/dhcpd.conf.sample /etc/dhcpd.conf

2.1. Server Configuration

The sample dhcpd.conf file is shown below:
ddns-update-style interim;
ignore client-updates;

subnet 192.168.0.0 netmask 255.255.255.0 {

# --- default gateway
        option routers                  192.168.0.1;
        option subnet-mask              255.255.255.0;

        option nis-domain               "domain.org";
        option domain-name              "domain.org";
        option domain-name-servers      192.168.1.1;
        option time-offset              -18000; # Eastern Standard Time
#       option ntp-servers              192.168.1.1;
#       option netbios-name-servers     192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don't change this unless
# -- you understand Netbios very well
#       option netbios-node-type 2;

        range dynamic-bootp 192.168.0.128 192.168.0.254;
        default-lease-time 21600;
        max-lease-time 43200;

        # we want the nameserver to appear at a fixed address
        host ns {
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
        }
}

A DHCP configuration file contains the configuration parameters for the clients. The important section in a DHCP client is "subnet 192.168.0.0 netmask 255.255.255.0" that specifies the subnet and netmask for which the address will be allocated. Other important options are:
           
            option routers ---> specifies default gateway for clients
            option subnet-mask ---> netmask for clients
            option domain-name-servers ---> DNS server to be used by the clients
            range ---> Range of IP addresses that will be allocated to clients

The working DHCP configuration file is given below.

ddns-update-style interim;
ignore client-updates;

subnet 10.0.0.0 netmask 255.0.0.0 {

        option routers                  10.10.10.1;
        option subnet-mask              255.0.0.0;

        option broadcast-address        10.255.255.255;
        option domain-name-servers      10.10.10.10;

        range  10.10.10.10 10.10.10.50;
        default-lease-time 21600;
        max-lease-time 43200;

}

Make sure to include "option broadcast-address".

The above configuration file specifies that the clients will be given an IP address in the range 10.10.10.10 to 10.10.10.50. The default lease time, in case the client does not explicitly ask for it, will be 21600 seconds; otherwise the maximum allowed lease time is 43200 sconds. The server also specifies that the client should use the subnet mask 255.0.0.0, 10.255.255.255 as its broadcast address, 10.10.10.1 as the default gateway and 10.10.10.10 as the DNS server.

The network diagram for this scenario is illustrated below:



Once you have edited the dhcpd.conf file, start the server. If you get a failure message regarding lease file being missing, create the following file:

            # touch /var/lib/dhcp/dhcpd.leases
            # service dhcpd start

You can check whether DHCP is running by pgrep command

            # pgrep dhcpd

The above configuration file dynamically allocates an IP address. DHCP can also be configured as a mixed environment that can allocate IP addresses dynamically and statically. The "host" section in the sample configuration file allocates an IP, based on the hardware address. Please see the “References” section for more advanced DHCP configurations. While running, DHCP server creates the client entries in /var/lib/dhcp/dhcpd.leases.

2.2. Client Configuration

The client side requires very little configuration. Most distributions come with a pre-installed version of a DHCP client. As an alternative, you can install rpm package, dhclient from "http://people.redhat.com/~jvdias/DHCP/".

For RedHat, you can use netconfig to configure the DHCP host.

            # netconfig

This will ask for a couple of options. Click on "Use dynamic IP configuration (BOOTP/DHCP)". Select "OK" and restart the network services.

            # service network restart

If the DHCP server has been configured properly, the client should be able to get an IP address. Use ipconfig to check the IP.

            # ifconfig

No comments:

Post a Comment