DHCP Server configuration

February 02, 2005
Suppose you are in charge of a network of say 100 computers all in a single broadcast domain. There are two ways of configuring the IP addresses of these machines. One is the static method ; assigning an IP address to each of the 100 machines manually which can be quite tedious. The other easier and better method, is to use DHCP (Dynamic Host Configuration Protocol) to let a computer acting as the server assign the IP address to each of the 100 machines automatically. Infact, it is so easy that nowadays any computer which forms a part of a large network is assigned its IP address dynamically. This is also true when you connect to the internet via a dial-up or DSL modem - in which case, your computer is technically a part of the large network of your ISP and your ISP assigns your computer an IP address from its address pool automatically.
There are two popular methods of assigning IP addresses. They are BOOTP and DHCP. Both wait and hear for computers in the network to send broadcasts publishing their MAC addresses and requesting an IP address. The BOOTP or DHCP server, on recieving a broadcast, assign an IP address to the MAC address in the broadcast from its address pool. The client computers can query the server and find lots of information like default gateway, IP address, subnet mask, DNS etc.
Here I will explain how to convert your linux machine into a DHCP server. The power of DHCP is that if anything changes on your network such as the IP of a DNS server, you only need to edit one configuration file even if you have hundreds of clients.

If you do not have dhcp server installed on your machine, this is the right time to do so. In Redhat, you install the dhcp (rpm) package. The dhcp server runs as a daemon and has the name dhcpd and listens on ports 67 (bootp server) and 68 (bootp client). There are two main configuration files for the DHCP server. They are :
  • /etc/dhcpd.conf
  • /var/lib/dhcp/dhcpd.leases
The dhcp package installs without any configuration. The daemon will not start if a dhcpd.leases file does not exist. An empty file (commented) is installed with this package.
#File : /etc/dhcpd.conf

ddns-update-style none;

option domain-name "mydomain.com";

default-lease-time 21600;
max-lease-time 43200;

subnet 192.168.1.0 netmask 255.255.255.0
{
range 192.168.1.100 192.168.1.200;
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
option domain-name-servers 123.123.123.10, 123.123.123.20;
option routers 192.168.1.1;

host station1
{
hardware ethernet 00:a0:cc:3d:0b:39;
fixed-address 192.168.1.7;
}

host station2
{
hardware ethernet 00:06:CD:CD:CD:CD;
fixed-address 192.168.1.8;
}
}
Above I have shown the listing of my /etc/dhcpd.conf file. I will explain the meaning of each line below :
ddns-update-style none;
The first thing we need to do is set a Dynamic DNS update style. Here I have set it to the value none. But if it is something you want to do, you may read the man pages which has lots of information on this topic.
option domain-name "mydomain.com";
The above line specifies the domain name set on your server if you are running DNS.
default-lease-time 21600;
max-lease-time 43200;
Specifies the time in seconds after which the lease will expire and the maximum lease time also in seconds.
subnet 192.168.1.0 netmask 255.255.255.0
Next we must specify what subnet and netmask we will be working on. Note that you can have many subnet configurations within the single dhcpd.conf file. Each subnet group is bound together by curly braces { }

Note that every command from here on will only pertain to the subnet specified above. This will be true until we reach the closing curly brace } as noted above.

Now we will specify what range of IP addresses we want to be made available for clients using DHCP. This option is very handy when used in conjunction with a firewall because you know exactly what IP addresses came from a client using DHCP and you can exercise restrictions upon them as necessary.
range 192.168.1.100 192.168.1.200;
option subnet-mask 255.255.255.0;
The above option is redundant as it has been already set before the curly braces. But since it is given in the man pages, I have included it here.
option broadcast-address 192.168.1.255;
Specifying the broadcast address of our subnet.
option domain-name-servers 123.123.123.10, 123.123.123.20;
The above line tells all our clients what servers to use for DNS inorder to resolve hostnames to IP addresses.
option routers 192.168.1.1;
This line tells our clients what IP address to use for the default gateway. Usually the default gateway is our router.
Even though DHCP gives out IP addresses dynamically, it has the option of reserving a particular address for a certain computer. To do this, you have to specify the MAC address of your client machine for which you need to reserve an IP address. You can find the MAC address by running the following command on your client machine:
# ifconfig eth0 | grep HWaddr
..where eth0 is your ethernet interface. The MAC address is a 48 bit address burned into the NIC by its manufacturer. It is a unique number and no two NICs in the world will have the same number. It is obtained in hexadecimal format.
host station1
The first thing we must do is to specify a name for the computer as a helpful identifier as shown above. Note that similar to the subnet grouping, we are starting a sub-group which is represented by the curly braces. This allows us to have multiple host definitions within one subnet group.
hardware ethernet 00:a0:cc:3d:0b:39;
This is the client machine's MAC address for which we are going to reserve an IP address.
fixed-address 192.168.1.7;
This line tells the dhcpd server what IP address we always want to be assigned to this computer. Now the only thing remaining is to save your /etc/dhcpd.conf file and restart your DHCP daemon.
# service dhcpd restart
Also see How to assign an IP Address to understand DHCP client configuration.

1 comments:

  • Hi,

    nice posting.
    let me add two helpful commands for debuging purposes:

    dhcpdump
    tcpdump -lenx -s 1500 port bootps or port bootpc | dhcpdump

    takes what tcpdump delivers and displays
    in a very good readable format the dhcp conversion.





    dhcpcd-test
    dhcpcd-test eth0
    performs a test "dhcp discover / request" and shows the "dhcp ack" from the server.

    J