Raspberry Pi DHCP Server

Raspberry Pi

A handy use for a Raspberry Pi is as a DHCP server for your home or small office network. A Raspberry Pi makes a great DHCP server because:

  • its small, credit-card size does not take much room in your home or office,
  • it uses very little power (<3W),
  • it is silent,
  • its Graphical User Interface (GUI) and Command Line Interface (CLI) capabilities, makes management easy,
  • it is relatively inexpensive ($35 as of this writing),
  • and it can be used for other purposes while providing DHCP services.

I recently replaced a CentOS 5.11 DHCP server with a Raspberry Pi. The CentOS system was a retired PC that burned about 65W of power 24 hours a day 7 days a week. Its hard drives and fans made it noisy. It was a conventional mini tower type, so it took up a fair amount of room too. While the CentOS system was reliable and robust, replacing it with a Raspberry Pi just made good sense on all fronts. The new system was easy to set up, uses very little power, takes up no room, and has the potential for other uses. Below are the steps I used to create my Raspberry Pi DHCP Server.

Set a Static IP Address

To begin, I started with a clean install of Raspbian. The first thing I needed was to set a static IP address for the Raspberry Pi. By default it is setup to be a DHCP client (not a server) on the local network through its eth0 port. I edited the interface by file /etc/network/interfaces.

sudo nano /etc/network/interfaces

I deleted the original code and replaced it with this:

auto lo

iface lo inet loopback
iface eth0 inet static
address 10.20.30.2
gateway 10.20.30.1
netmask 255.255.255.0

Set DNS Addresses

DNS servers should be listed in /etc/resolv.conf. I found my resolv.conf was empty so I edited the file.

sudo nano /etc/resolv.conf

I populated /etc/resolv.conf with some trusted DNS servers:

nameserver 208.67.222.222
nameserver 75.75.75.75
nameserver 208.67.220.220

I restarted my Raspberry Pi at this point to make sure the networking came up the way I wanted. I logged into it using the new, static address. This may not be a necessary step, but I wanted to check the network before installing the DHCP server.

Install and Configure the DHCP Server Application

DHCP server was not installed by default. I installed a DHCP server application with apt-get.

sudo apt-get install isc-dhcp-server

The next step wass to tell isc-dhcp-server what network interface to use for DHCP services.

sudo nano /etc/default/isc-dhcp-server

I found the following section near the end of the file. I put eth0 between the quotes so the end of the configuration file looks like this:

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="eth0"

Next I had to edit /etc/dhcp/dhcpd.conf to tailor the server to my own network’s needs.

sudo nano /etc/dhcp/dhcpd.conf

Just in case I wanted to come back to the original remarks or default setup, I kept the contents of dhcpd.conf and remarked out all lines. I went to the beginning of the configuration file and inserted the following settings right at the top of the file, before all the remarks.

authoritative;
max-lease-time 31449600;
default-lease-time 2419200;
option domain-name-servers 208.67.222.222 , 75.75.75.75 , 208.67.220.220;
option broadcast-address 10.20.30.255;
option subnet-mask 255.255.255.0;
option routers 10.20.30.1;
# Home Workstations
subnet 10.20.30.0 netmask 255.255.255.0 {
range 10.20.30.30 10.20.30.100;
}
ddns-update-style interim;

Leases

I already had a DHCP server and wanted to keep my current client addresses intact if possible. Before turning the service off on my CentOS machine, I saved the “leases” files into a tarball and copied it to the Raspberry Pi.

cd /var/lib/dhcpd
tar czvf leases dhcpd.l*
scp leases [email protected]:/home/kyle

From there, I moved the leases to the correct location for the Raspberry Pi and extracted the CentOS leases into Raspbian to keep my old leases intact.

mv leases /var/lib/dhcp
cd /var/lib/dhcp
tar xzvf leases

To make sure everything was ready, I manually started DHCP to check for any issues.

sudo service isc-dhcp-server start

There were no issues and the service was running as expected. So I configured it to start on boot, automatically.

sudo update-rc.d isc-dhcp-server start

Reboot and Check DHCP

The final step was to reboot the Raspberry Pi and make sure DHCP is really working. After rebooting I used this command:

sudo service isc-dhcp-server status

It told me the service was running. My old CentOS box was not the same IP as the new Raspberry Pi address, so the final check was easy. I just rebooted a Windows PC with a dynamic IP address. I opened a command prompt.

ipconfig /all

In one section, it revealed that the Raspberry Pi was indeed issuing dynamic addresses with the following information.

IPv4 Address. . . . . . . . . . . : 10.20.30.70(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Sunday, January 17, 2016 1:05:34 PM
Lease Expires . . . . . . . . . . : Sunday, February 14, 2016 1:05:33 PM
Default Gateway . . . . . . . . . : 10.20.30.1
DHCP Server . . . . . . . . . . . : 10.20.30.2
DNS Servers . . . . . . . . . . . : 208.67.222.222
75.75.75.75
208.67.220.220

From this I can see that it properly assigned my gateway, DNS, IP, and subnet mask. It also lists the DHCP server and I can see the address for it is correct. I can also see my dynamic IP address did not change from 10.20.30.70 because the leases files worked. This means all my dynamic devices remain the same even though I changed servers.

Here is the best part. I have the same service level from a new device that is quieter, smaller, and saves me 1488Wh or $0.18 per day. At that rate, the Raspberry Pi DHCP server pays for itself in less than a year.