DHCP Server on Linux with Raspberry Pi

Published by Tobias Hofmann on

2 min read

My internet provider is Unitymedia. Their default router comes with a DHCP server. Honestly, it’s one of the worst products I ever had to work with. My private network is 192.168.0.x. The DHCP server of the Unitymedia box is distributing from time to time leases for 192.168.192.x. Changing my private network to 192.168.192.x one is not working, as then the DHCP server picks another address range. Advise from Unitymedia help desk was to reboot the box, which, of course, won’t solve the problem. Because of this error, some of my devices are in a different network: Chromecast won’t work, broken internet connection on smartphones, etc.

I do have a Raspberry Pi (RP) in 24/7 use. My idea is to run my own DHCP server on the RP. This not only solves the DHCP problem, but also gives me more control over the DHCP configuration.

Preparation

sudo apt-get update
sudo apt-get install isc-dhcp-server

This installs ISC DHCP server. As you can see in the output, starting the DHCP server failed.

sudo systemctl status isc-dhcp-server.service

The error is simply caused because the DHCP server is not configured. Let’s change that.

Configuration

Several parameters must be activated and configured.

sudo vim /etc/dhcp/dhcpd.conf

Lease time

default-lease-time 600;
max-lease-time 7200;

Activate DHCP server

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

Subnet

This configures what IP address are going to be distributed. My private network is 192.168.0.x with the router on 192.168.0.1. As DNS you can use whatever you want, as an example I am using Google DNS servers.

subnet 192.168.0.0 netmask 255.255.255.0 {
  range 192.168.0.150 192.168.0.240;
  option routers 192.168.0.1;
  option domain-name "itsfullofstars.de";
  option domain-name-servers 8.8.8.8, 8.8.4.4;
}

This will give DHCP clients an IP address between .150 and .240, with router .1, Google DNS and sets the domain name to my own.

Deactivate old DHCP server

To not have the DHCP server provided by Unitymedia box still issuing wrong IP address, I am going to deactivate the service via the web interface.

Start DHCP server

After installing and configuring the new DHCP server on RP and deactivating the one from the router box, it’s time to start the new DHCP server.

Result

To see if a IP address is assigned, use this command:

sudo systemctl status isc-dhcp-server.service

Android

Putting my Android device into flight mode and back makes it connect to Wifi again and obtain a new IP address via DHCP. In the DHCP status log, I can see the DHCPDISCOVER from the Android device and that it got the IP address 192.168.0.150 assigned.

Mac

As my Mac always got the wrong IP assigned, I changed it to manual configuration. Change the mode to DHCP, apply and deactivate / activating Wifi.

Soundbar

And my soundbar that got a strange IP address assigned by the Unitymedia router box? Works too!

Chromcast streaming shows the SoundBar is now in the same network.

Let the world know

Tobias Hofmann

Doing stuff with SAP since 1998. Open, web, UX, cloud. I am not a Basis guy, but very knowledgeable about Basis stuff, as it's the foundation of everything I do (DevOps). Performance is king, and unit tests is something I actually do. Developing HTML5 apps when HTML5 wasn't around. HCP/SCP user since 2012, NetWeaver since 2002, ABAP since 1998.

13 Comments

Sergio · December 28, 2019 at 00:27

Thanks for the tutorial, just a comment: you have to declare the interface in the config file ‘/etc/default/isc-dhcp-server’, otherwise it won’t start. It took me an hour finding it out.

Thanks again.

    Tobias Hofmann · January 2, 2020 at 19:01

    Hi Sergio,

    thanks for pointing this out. My my case, isc-dhcp-server contains INTERFACES=””. By default, ISC DHCP should listen on eth0. My RP also does have an additional network interface, wlan0, but that is not activated.

      kevin reid · October 7, 2020 at 22:05

      INTERFACES=””” didn’t work for me – I also had to put INTERFACES=”eth0″ into the file ‘/etc/default/isc-dhcp-server’ before it would work.

Chris Parker · June 5, 2020 at 12:53

Hi Tobias,

Doesn’t the DHCP server need a static IP address as well?

    Tobias Hofmann · June 6, 2020 at 14:03

    Hi Chris,

    my RP has a static IP address. As the clients are discovering a DHCP server via broadcast, a fixed address should not be needed.

Peter Vine · June 18, 2020 at 15:04

If you don’t have the luxury of having a domain name, what do you use instead?

    Tobias Hofmann · June 19, 2020 at 17:24

    Hi Peter,

    the domain name is only user internally. You are free to use any domain name you like. It could be vine.com or myprivatedomain.org.

Dillon · July 17, 2020 at 23:26

I’m slowly migrating off my Google Fiber server and this is one of the useful tools O still need to setup.

Kvas · January 3, 2021 at 19:10

I also needed to run a dchp server on my raspberry and I used this article, but after I lost the ability to connect to raspberry via ssh

    kvas · January 3, 2021 at 22:09

    and android devices can not connect wifi with router
    sorry my English

      Toto · March 24, 2021 at 11:42

      Same issue as kvas here. Nice tutorial but this is not working. The result of all this is:
      – wlan0 no longer recognized
      – eth0 down but can be put up with ifup

      Maybe it was working with some older OS version…

        Tobias Hofmann · March 24, 2021 at 12:12

        If your wlon0 or eth0 devices are not working, you have to check why these are not activated. Are these the ones on the DHCP server? Or the ones of a DHCP client? If they are from the server, DHCP needs them to be up and running. You won DHCP server has a static IP. For the clients, you have to check the logs to see what goes wrong.

Assign a static IP to DHCP client | It`s full of stars! · February 14, 2019 at 10:01

[…] setting up a DHCP server on a Raspberry Pi running Linux I get working leases for my clients. However, these are not static. It can happen […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.