Home » Debian, Featured, Headline, How-to, Linux, Shell, Ubuntu

Linux ISC DHCP Server under Debian and Ubuntu

6 April 2009 One Comment

Right Tools

Right Tools

Managing big networks can become an issue if you don’t use the right tools. DHCP (Dynamic Host Configuration Protocol) appears in early 1993 and was defined in RFC 1531. This protocol was created to reduce system administration workload, allowing administrators to add new devices in a IPV4 network easy as possible (in some cases no intervention required). Dynamic Host Configuration Protocol is used by DHCP clients to automatically obtain the necessaryly information (network parameters assigment) to connect to a IPV4 network. Also DHCP was extended for IPV6 protocol in RFC 3315. Some devices should never use DHCP: routers, firewalls, any kind of production servers and they should have static IP address.

Also DHCP can be used for the mobile devices and not only on large networks. Using it for mobile devices is a bless when you need to switch networks all the time (working on your laptop from home, work or any other networks). So even if you have a small network using DHCP is “required”.

In this article I will try to explain you how to configure your Linux ISC DHCP server. This article will be splited in 3 parts:

1. Configuring ISC DHCP under Ubuntu / Debian
2. Configuring failover on ISC DHCP
3. Configuring Dynamic DNS with ISC DHCP updates

The configuration parameters should match any existent Linux distribution, but the paths, and other paramters, scripts are just tested on Debian Lenny 5.0 and Ubuntu 8.10. Anyway with minimum skills the content of this article can be used on any Linux distribution. Today we will talk about Configuring ISC DHCP under Ubuntu / Debian and in the next days I will come with the next articles.

1. Configuring ISC DHCP under Ubuntu / Debian

ISC DHCP servers is the default DHCP server on almost all Linux distributions. Installing it under Ubuntu / Debian is just a simple command:

Ubuntu as your default user

[root@randombugs]$ sudo apt-get install dhcp3-server

Debian as root

[root@randombugs]# apt-get install dhcp3-server

Now we need to know on what network we need to serve DHCP requests. For that you will need to run ifconfig to see how many network cards do you have or you need to plan on what network card do you want to serve this requests. I will presume you already know that, because is hard to understand the rest of article if you don’t have minimum skills under Linux networking.

Planing is not such a complicate thing. Let’s assume the following:

1. we have IP network 10.1.0.0/24 where we want to server DHCP requests.
2. our DNS server 10.1.0.12
3. our gateway address is 10.1.0.1
4. first 16 IP addresses are reserved only for servers and our DHCP pool need to be from 10.1.0.16 to 10.1.0.254
5. our network 10.1.0.0/24 is on eth1

First we need to enable dhcp to server requests on eth1. For that just open /etc/default/dhcp3 and add or modify INTERFACES parameter.

1
2
3
4
5
6
7
8
9
10
11
# Defaults for dhcp initscript
# sourced by /etc/init.d/dhcp
# installed at /etc/default/dhcp3-server by the maintainer scripts
 
#
# This is a POSIX shell fragment
#
 
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="eth1"

Now we need to add/modify the configuration file from /etc/dhcp3/dhcpd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
authoritative;
 
ddns-update-style none;
 
option domain-name "domain.com";
option broadcast-address 10.1.0.255;
 
default-lease-time 86400;
max-lease-time 86400;
 
log-facility local7;
 
subnet 10.1.0.0 netmask 255.255.255.0 {
  option domain-name-servers 10.1.0.12;
  range 10.1.0.16 10.1.0.254;       
  option routers 10.1.0.1;             
 
    host hostname {
      hardware ethernet 00:01:13:e1:d1:11;
      fixed-address 10.1.0.17;         
    }
 
    host print_server {
      hardware ethernet 00:01:23:e2:d1:22;
      fixed-address 10.1.0.18;         
    }                  
 
}

Now let’s explain what we configured here.

authoritative – if is not specified (by default is not) then DHCP assume the configuration for network segment what we used is NOT correct and is not authoritative. Because,DHCP, is not by default configured with authoritative option, if a user what configure a DHCP server (and probably doesn’t know what is doing) in the same network as your servers, for example, the server configured without authoritative it will NOT send DHCPNAK messages to DHCP clients that already obtained a valid IP configuration from your valid DHCP server. This option should be the first option in your configuration file.

ddns-update-style none – refers to update style of the dynamic dns, but because we don’t use it on this configuration this options is none.

option domain-name “domain.com” – domain name what will be served to your clients.

option broadcast-address 10.1.0.255 – broadcast address where dhcp server will send the responses for the clients.

default-lease-time 86400; – if a client doesn’t request the lease time for configuration, then this value will be used as default. 86400 represents the seconds until the client configuration is expiring (1 day).

max-lease-time 86400; – the maximum lease time what will be served to client.

log-facility local7; -by default dhcp is logging as daemon facility, but we can change the log-facility to whatever value what syslog allow us. Adding local7 as log facility it will require to modify syslog.conf to accept that

local7.debug /var/log/dhcpd.log

After doing this modification just restart the syslog daemon

/etc/init.d/sysklogd restart

Now we will configure the pool subnet for dhcp.

subnet 10.1.0.0 netmask 255.255.255.0 – represents the subnet where dhcp will serve the request for it’s clients

option domain-name-servers 10.1.0.12; – DNS what will be assigned to the client

range 10.1.0.16 10.1.0.254; – what ip should use to lease for the clients.

option routers 10.1.0.1; – default gateway for the client

Also we can reserve a certain IP for a host. Host identification is made based on Physical / MAC Address.

host [hostname] – the name of the host for who will do the reservation.
hardware etherenet [address] – Physical / MAC Address of the client
fixed-address [ip] – reserved ip address

Also we have a lot of other options what we can send them to the dhcp client:
option netbios-name-servers 10.1.0.1; – if you have a wins server in your network (PDC, samba etc)
option ntp-servers 10.1.0.1; – if you have a NTP server (time sync)
option ip-forwarding off; – Don’t forward dhcp requests to another network interface.

For more just RTFM (read the fine manual) of dhcpd.conf:

[root@randombugs]# man dhcpd.conf

Now you can restart the DHCP server and verify if is working corectly. (use root to run this command)

[root@randombugs]# /etc/init.d/dhcp3-server restart

Now from a linux workstation run this command (use root to run this command)

[root@randombugs]# dhclient eth1
[sudo] password for rb:
Internet Systems Consortium DHCP Client V3.1.1
Copyright 2004-2008 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/

Listening on LPF/eth1/00:e1:21:12:a8:4d
Sending on LPF/eth1/00:e1:21:12:a8:4d
Sending on Socket/fallback
DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 4
DHCPOFFER of 10.1.0.17 from 10.1.0.1
DHCPREQUEST of 10.1.0.17 on eth1 to 255.255.255.255 port 67
DHCPREQUEST of 10.1.0.17 on eth1 to 255.255.255.255 port 67
DHCPACK of 10.1.0.17 from 10.1.0.1
bound to 10.1.0.17 — renewal in 86300 seconds.

From a windows workstation just try

ipconfig /renew

You need to have administrative privileges to run this command

After a while probably you will want to assign/reserve an address for each client. Doing that for the new clients will not be hard, but what we can do for the old clients from the network what already have some stable addresses? ISC DHCP use a lease file (/var/lib/dhcp3/dhcpd.lease) to remember the old clients IPs so I write a script what will extract the hosts from lease file and convert them to a host like configuration.

Copy this script in a file (I used lease2host.sh)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash                                      
# Autor: Stanila Constantin Adrian               
# Date: 06/04/2009                               
# Description: Convert from a ISC dhcp lease file to a standard host configuration
# http://www.randombugs.com  
 
START=false
 
cat /var/lib/dhcp3/dhcpd.leases | while read line
do
    #Bash pattern
    if [[ "$line" == lease* ]]
    then
        START=true
        IP=`echo $line | awk '{print $2}'`
    fi
 
    if [[ "$line" == "}" ]]
    then
        if [ "$NAME" ]
        then
            NAME=${NAME//\"/}
            NAME=${NAME//;/}
            echo "host "$NAME" {"
            echo "fixed-address "$IP";"
            echo "hardware ethernet "$HW
            echo "}"
        fi
        IP=""
        HW=""
        NAME=""
        continue
    fi
 
    if [ $START = true ]
    then
        if [[ "$line" == *ethernet* ]]
        then
            HW=`echo $line | awk '{print $3}'`
        fi
 
        if [[ "$line" == client-hostname* ]]
        then
            NAME=`echo $line | awk '{print $2}'`
        fi
 
    fi
 
 
done

Don’t forget to set executable attribute

[root@randombugs]# chmod +x lease2host.sh

Now run it:

[root@randombugs]# ./lease2host.sh > /etc/dhcpd-hosts.conf

and in dhcpd.conf file don’t forget to include this file with

include “/etc/dhcpd-hosts.conf”;

or just copy the resulted file in your dhcpd.conf

The script is far to be perfect and if you find any bug just send me an email or write here on the comment section. I hope this article will be useful for you.

Good luck!
[ad#ad-2]


One Comment »

  • Walyonaire said:

    Perfect

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.