Home Setup Raspberry pi as a dhcp server
Post
Cancel

Setup Raspberry pi as a dhcp server

This post is going to show you the basic steps of creating a DHCP server using a Raspberry pi running the latest version of Raspbian, configure the Raspberry (Linux) to use a fixed IP Address and configure clients to get a static IP address using their MAC address. The software used is isc-dhcp-server available from the Ubuntu software repository. I was fed-up of my router giving random IP addresses to servers when it was meant to hand out the same address.

Install Software

The first step is to install the server software.

sudo apt-get install isc-dhcp-server

Set Static IP Address

The second step is to set a static IP address on the Raspberry pi as this wont be able to start the DHCP service without it. I don't need to configure NetworkManager as I am not running the Raspberry pi with any GUI. Edit the following file.

sudo nano /etc/network/interfaces
auto lo

iface lo inet loopback
#iface eth0 inet dhcp //This should be commented out as we do not want to use DHCP to get an address.

iface eth0 inet static #// We want a static address
address 172.16.20.21
gateway 172.16.20.62 #//My router as this points to the internet
netmask 255.255.255.192

#allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp

The bottom section of the file has been commented out as this is not relevant to the ethernet port I am using.

To get the DNS working we need to edit the resolv.conf file.

sudo nano /etc/resolv.conf
domain home //In File already
search home //In File already
nameserver 172.16.20.62 //Router which handles DNS
nameserver 8.8.8.8 //Google DNS server

Add the above nameservers into the file. You will need to modify to your network. You can use Googles DNS servers instead if you prefer.

Configure the DHCP Server

Now we need to tell the DHCP service the interface to hand out addresses on. Edit the following file:

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

Find this section, it should be at the bottom of the file.

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

Change the INTERFACES="eth0" to the interface you want the DHCP service to send requests, the Raspberry Pi only has one interface so this is Eth0.

Now we need to edit the actual DHCP configuration file. Edit the following:

sudo nano /etc/dhcp/dhcpd.conf

Most of the file is commented out and contains helpful examples, find the following:

# option definitions common to all supported networks...
option domain-name "home"; #//Randomly chosen the name home as this is my home network.
option domain-name-servers 172.16.20.62; #//This is the router in my network that handles DNS

This Raspberry pi will be acting as the DHCP server in my home LAN. I therefore needed to un-comment the authoritative line.

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be un-commented.
authoritative;

This section will configure the addresses handed out, the gateway to give to the clients and the DNS servers to give to clients etc.

subnet 172.16.20.0 netmask 255.255.255.192 {
  range 172.16.20.11 172.16.20.19; #//Hands out an address in this range to clients
  range 172.16.20.31 172.16.20.49; #//Hands out an address in this range to clients
  option domain-name-servers 172.16.20.62; #//Router handles the DNS requests so send them to it
  option domain-name "home"; #//Same as set previously (Not sure it matters)
  option routers 172.16.20.62; #//The router in the network/Gateway the clients should use
  option broadcast-address 172.16.20.63; #//Broadcast of the subnet used
  default-lease-time 600; 
  max-lease-time 7200;
}

I have two ranges in my configuration. This is because I don't want the DHCP server handing out some of these addresses as they will be fixed to certain clients later in the configuration. You can define as many ranges as needed.

Configure Fixed IP's for clients

Further down the configuration file should be a section for adding fixed IP Addresses. Find something similar to this example:

#host fantasia {
#  hardware ethernet 08:00:07:26:c0:a5;
#  fixed-address fantasia.fugue.com;
#}

You can either un-comment this and edit it or create as many as you like after it. For example.

host machine1 {
   hardware ethernet XX:XX:XX:XX:XX:XX;
   fixed-address 172.16.20.1;
}
host machine2 {
   hardware ethernet XX:XX:XX:XX:XX:XX;
   fixed-address 172.16.20.2;
}
host machine3 {
   hardware ethernet XX:XX:XX:XX:XX:XX;
   fixed-address 172.16.20.20;
}
host machine4 {
   hardware ethernet XX:XX:XX:XX:XX:XX;
   fixed-address 172.16.20.3;
}
host machine5{
   hardware ethernet XX:XX:XX:XX:XX:XX;
   fixed-address 172.16.20.4;
}
host machine6 {
   hardware ethernet XX:XX:XX:XX:XX:XX;
   fixed-address 172.16.20.10;
}

If any of the machines listed above with those MAC addresses then they will get the given fixed IP Address. These are addresses are excluded from the range of addresses handed out by the DHCP server (See above configuration).

Start the DHCP service upon boot

Run this command to make the service start on boot otherwise when the machine is rebooted then any clients in the local network won't be given an address.

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

Now you can either start the service by running the following command or reboot the server.

sudo service isc-dhcp-server start

Checking the currently leased addresses

Run this command to check the currently assigned addresses. Please note this does not include the clients given a fixed address. You should see something like this:

cat /var/lib/dhcp/dhcpd.leases
lease 172.16.20.11 {
  starts 4 2014/10/09 18:33:57;
  ends 4 2014/10/09 18:43:57;
  cltt 4 2014/10/09 18:33:57;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet XX:XX:XX:XX:XX:XX;
  client-hostname "phone";
}

Run this command to check the currently leased addresses to the fixed clients. You should see something like this:

cat /var/lib/dhcp/dhclient.eth0.leases
lease {
  interface "eth0";
  fixed-address 172.16.20.10;
  option subnet-mask 255.255.255.224;
  option dhcp-lease-time 1814400;
  option routers 172.16.20.30;
  option dhcp-message-type 5;
  option dhcp-server-identifier 172.16.20.30;
  option domain-name-servers 172.16.20.30,172.16.20.30;
  option dhcp-renewal-time 907200;
  option dhcp-rebinding-time 1587600;
  option domain-name "home";
  renew 5 2014/10/10 01:15:17;
  rebind 6 2014/10/18 07:13:58;
  expire 1 2014/10/20 22:13:58;
}

References

Ubuntu Forums
How to set a Static IP in Ubuntu – The Proper Way! | Sudo Juice
Setup DHCP Server On Ubuntu 14.04 LTS Server | Unix Men

This post is licensed under CC BY 4.0 by the author.

If you have found this site useful, please consider buying me a coffee :)

Proud supporter of the Gnome Foundation

Become a Friend of GNOME

Contents

Wordpress permalinks not working apache

Setting up mail server dns records

Comments powered by Disqus.