Setting Up A Raspberry Pi As A DHCP Server

A popular DHCP server for the Pi is ISC's dhcpd. There is a package for the Pi and so you can install it in the usual way using apt-get.

Step 1

At the Pi command line use apt-get update to download any package updates and then write the changes to the local package cache.

sudo apt-get update

Step 2

Now install the ISC DHCP server software. At the end of the installation process, the DHCP server daemon will be started and it will fail! We'll fix that presently.

sudo apt-get install isc-dhcp-server

.

.

[FAIL] Starting ISC DHCP server: dhcpd[....] check syslog for diagnostics. ... failed!

invoke-rc.d: initscript isc-dhcp-server, action "start" failed.

Step 3

The DHCP server requires a static IP address and, by default, the Pi will have a DHCP one. Edit /etc/network/interfaces using nano or your preferred editor

sudo nano /etc/network/interfaces

and modify the records that pertain to eth0 using the network parameters for your network.

iface eth0 inet static

address <the-IP-address-of-your-Pi-that-will-be-the-DHCP-server>

netmask <the-subnet-mask-of-your-LAN>

gateway <the-IP-address-of-your-LAN-gateway>

Save the changes.

Step 4

The next step is to modify the DHCP configuration file to suit your requirements. Edit /etc/dhcp/dhcpd.conf. All of the main configuration items are commented out and this is why the startup failed previously:

sudo nano /etc/dhcp/dhcpd.conf

Save the changes.

Step 5

You need to read the documentation to fully understand the parameters, however, for a basic network you need at least the following:

Exclamation markNote that the braces and semi-colons are required syntax and need to be in the positions shown. For example, you cannot just concatenate all the "braced" parameters in a single statement.

subnet <starting-IP-address-of-your-network> netmask <starting-IP-address-of-your-network> {

     range <first-IP-address-of-your-DHCP-address-range> <last-IP-address-of-your-DHCP-address-range>;

     option routers <the-IP-address-of-your-gateway-or-router>;

     option broadcast-address <the-broadcast-IP-address-for-your-network>;

}

Step 6

A final configuration step is to set up the DHCP "defaults" file. Unless you do this, your DHCP server will have problems starting when the Pi boots. Edit the file /etc/default/isc-dhcp-server and uncomment the following records, inserting your network interface name as appropriate and changing the filepaths if you have chosen to put the files elsewhere. Unless you need to set any default options, you can leave the OPTION keyword commented out:

DHCPD_CONF=/etc/dhcp/dhcpd.conf

DHCPD_PID=/var/run/dhcpd.pid

INTERFACES="eth0"

Save the changes.

Step 7

Restart the DHCP daemon process using the service command:

sudo service isc-dhcp-server restart

It's likely that you have errors in your configuration, so these need to be corrected before trying to restart the service. The main configuration file is a bit pernickety about braces and commas but the messages are helpful and point accurately to the offending item.

USEFUL INFORMATION

The DHCP server keeps its current set of leases in a text file in /var/lib/dhcp/dhcpd.leases.

Online documentation for the DHCP server can be viewed on the Pi using man dhcpd.