DHCP daemon on Raspberry Pi

DHCP stands for “Dynamic Host Configuration Protocol”.    The DHCP daemon is the process which assigns IP addresses to computers when they join a network, and gives them other important information about the network, including DNS server addresses. A local network used for a wireless Access Point usually has a DHCP server associated with it. It’s important to note that this is a DHCP server for the local network, but the AP may also act as a DHCP client to get it’s own network configuration information from the upstream link. These are two different things.

There are several packages that you can use to run a DHCP server. I chose the ISC DHCP server package (isc-dhcp-server), but I later learned that one can also use the dnsmasq package as a DHCP server.1 Choose one or the other.

Here is how I set up and configured dhcpd on Raspbian Stretch, using the isc-dhcp-server. This page was originally written in 2018, but I recently updated it in December 2020.

  1. Install: Install the package:
    sudo apt-get install isc-dhcp-server
    When you install this package it is configured to run automatically at boot time (
  2. Configure: There are two files to edit or check:
    1. Move the existing file /etc/dhcp/dhcpd.conf out of the way so you can replace it with your own:
      sudo mv /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.ORIG
      (You should keep it because it contains good documentation and examples).
      Then create and edit a new file /etc/dhcp/dhcpd.conf containing:2

      #                                                                               
      # Configuration file for DHCP server on Rasberry Pi                             
      #                                                                               
      ddns-update-style none;
      option domain-name "wifipi.local";
      option domain-name-servers 8.8.8.8, 1.1.1.1, 192.168.1.1;
      default-lease-time 3600;
      max-lease-time 86400;
      authoritative;
      log-facility local7;
      
      # Configure service for local network 192.168.47.0 (the wireless AP)                    
      subnet 192.168.47.0  netmask 255.255.255.0 {
          range 192.168.47.50  192.168.47.250;
          option routers 192.168.47.1;
      }
      ##
    2. Also edit the file /etc/default/isc-dhcp-server to add the line:
      INTERFACESv4="wlan0"

      Change the name of the interface to the local network if you are using something else.

  3. Add a Delay: While I could start the service “by hand” once the Pi had booted, I found that the ISC DHCP server would sometimes not start at boot time, even though it is configured at installation to do so. When that happened I found a complaint in the log file (viewed with `grep dhcp /var/log/syslog`) like this:
    dhcpd[345]: Not configured to listen on any interfaces!

    The problem appears to be that the server is brought up at the same time the interface is being configured, and sometimes the interface is not ready yet. A simple solution3 is to add a slight delay to the init script that brings up the DHCP server. I edited the file /etc/init.d/isc-dhcp-server and found the line (in the start_daemon() function) which actually starts the daemon. I then added a sleep of a few seconds (at least 4 seemed to be needed) right before it. The code should look something like

            sleep 4
            start-stop-daemon --start --quiet --pidfile $PIDFILE \
                    --exec /usr/sbin/dhcpd -- $VERSION -q -cf $CONF $INTERFACES
            sleep 2

    As you can see, there is already a sleep of 2 seconds right afterwards to let the daemon get started.

    A more elegant solution would be to create a systemd service file for this daemon. If that’s not been done in a newer release of the Raspberry Pi OS (I will check at some point) then I may do that and report the result. Another solution, of course, is to use dnsmasq instead.

Notes and References

  1. See Setting up a Raspberry Pi as a routed wireless access point at https://www.raspberrypi.org/documentation/configuration/wireless/access-point-routed.md
  2. “How to: Make a Raspberry Pi Powered Wifi Repeater” by Dryfire117, https://pastebin.com/A4jUp2Nq
  3. Found on StackExchange, of course: https://askubuntu.com/questions/58032/dhcp-server-doesnt-start-at-boot-because-of-wrong-startup-order
Print Friendly, PDF & Email

24 thoughts on “DHCP daemon on Raspberry Pi

  • 27 Feb 2023 at 16:13
    Permalink

    Hello,
    I’m using isc-dhcp-serverand I created a wifi net. I have a D-link DWA-131 802.11n connected to the raspberry.

    From different WIFI arduinos I connect to this network, but it only supports 8 at a time, I think there must be some configuration that I’m missing,

    Can only 8 components be connected or am I doing something wrong?

    Thanks a lot

    Reply
    • 28 Feb 2023 at 14:11
      Permalink

      There is a limit based on the number of available IP addresses. In the example here the subnet has a range (of IP addresses) from 192.168.47.50 to 192.168.47.250, so a limit of 200 addresses. Check that. I can’t think of any other way there could be a limit.

      Reply
  • 17 Sep 2021 at 11:25
    Permalink

    I have an issue with isc server too, as my raspberry pi keep on sending DHCP requests to it’s server and getting new session very often, Any suggestion for this issue is much appreciated.

    Reply
    • 18 Oct 2021 at 08:18
      Permalink

      Is it possible to give it a fixed IP address on the upstream network? Otherwise, you might see if you can change the lease time on the upstream DHCP server.

      Reply
      • 28 Feb 2023 at 13:14
        Permalink

        Actually the network I am creating is an internal network, it has no output to the internet (and I don’t want it either), but I am surprised that this network supports 8 components but not one more.

        Reply
  • 26 Dec 2020 at 12:33
    Permalink

    I finally had some time to look at this and I found the problem with isc-dhcp-server not starting at boot time. It seems that in the boot order it’s started at about the time the wlan0 interface is being brought up, and sometimes (but not always, for me) the isc-dhcp-server starts fast enough that wlan0 isn’t ready yet. The solution I found, which I added to this page, is to add a `sleep 4` to the init script that starts up isc-dhcp-server. There may be other ways to deal with it as well, but that solved it for me.

    Reply
  • 26 Oct 2020 at 09:16
    Permalink

    I was following the instruction to set up ISC DHCP server, but the service failed to start.

    However following the Raspbery Pi documentation on setting up a wireless access point, hostapd, working well.

    I have a suspicions that the isc dhcp server fails to start because dhcpd service is already running by default.

    Reply
    • 26 Oct 2020 at 09:18
      Permalink

      Thank you for the report. Can you confirm that another dhcpd service is running, and which one? I had to add this to my setup because there was nothing else doing that job.

      Reply
    • 26 Dec 2020 at 12:34
      Permalink

      The other dhcp service that may be running is the _client_ for the upstream connection, not the _server_, and the two are different. At least that’s what I found when the server wasn’t starting. I found the true cause of the server not starting (sometimes?) at boot, and added it to the page.

      Reply
    • 26 Dec 2020 at 12:30
      Permalink

      Thanks for making me aware of that page. They use dnsmasq instead of isc-dhcp-server, and that might be better, given the problems with starting the latter at boot time. I’ve added a link to that page and the suggestion of using it instead.

      Reply
  • 6 Jun 2020 at 08:20
    Permalink

    Hi,

    I’ve installed this on a RPi 3B running the latest version of Buster. The DHCP service is running as I can see in leases file:

    lease 192.168.1.145 {
    starts 6 2020/06/06 11:54:41;
    ends 6 2020/06/06 12:04:41;
    tstp 6 2020/06/06 12:04:41;
    cltt 6 2020/06/06 11:54:41;
    binding state free;
    hardware ethernet f8:2f:a8:48:e5:3a;
    uid “\001\370/\250H\345:”;
    set vendor-class-identifier = “PS Vita\000”;
    }

    However, when I run:

    sudo systemctl status isc-dhcp-server.service

    I get:
    * isc-dhcp-server.service – LSB: DHCP server
    Loaded: loaded (/etc/init.d/isc-dhcp-server; generated)
    Active: failed (Result: exit-code) since Sat 2020-06-06 13:18:59 CEST; 58min ago
    Docs: man:systemd-sysv-generator(8)

    Jun 06 13:18:59 rpi-dhcp systemd[1]: Starting LSB: DHCP server…
    Jun 06 13:18:59 rpi-dhcp isc-dhcp-server[2694]: Launching IPv4 server only.
    Jun 06 13:18:59 rpi-dhcp isc-dhcp-server[2694]: Starting ISC DHCPv4 server: dhcpddhcpd service already running (pid f
    Jun 06 13:18:59 rpi-dhcp systemd[1]: isc-dhcp-server.service: Control process exited, code=exited, status=1/FAILURE
    Jun 06 13:18:59 rpi-dhcp systemd[1]: isc-dhcp-server.service: Failed with result ‘exit-code’.
    Jun 06 13:18:59 rpi-dhcp systemd[1]: Failed to start LSB: DHCP server.
    lines 1-11/11 (END)

    Can anybody help me understand what’s happening?

    Thanks in advance!

    Reply
    • 13 Sep 2020 at 08:50
      Permalink

      Looks like you already had it running and it tried to start it again:
      Starting ISC DHCPv4 server: dhcpddhcpd service already running (pid f

      It’s the second instance that is terminating, since the service is already running. You can check active services to ensure the DHCP service is running:
      systemctl –type=service

      If so you just need to figure out why something is trying to start it second time.

      Reply
  • 21 May 2020 at 13:15
    Permalink

    There are two “d”s in dns in the example. This will keep it from starting for anyone that copies the example. This got it working for me.

    Reply
  • 6 Nov 2019 at 17:36
    Permalink

    My dhcp server doesn’t start again after reboot. Tried everything. Someone please help.

    Reply
  • 8 Sep 2019 at 09:12
    Permalink

    do you know if this dhcp server supports multiple subnets and multiple listening interfaces?

    Reply
    • 8 Sep 2019 at 10:08
      Permalink

      I’m afraid I do not know if this particular dhcp server supports those.

      Reply
    • 23 Feb 2022 at 19:49
      Permalink

      Yes. Your shared network (in your config) will default to the device that has a statically assigned IP within that subnet.

      Reply
  • 8 Jul 2019 at 22:12
    Permalink

    Looking over this page again I see a potential problem, in that I run the DHCP daemon on wlan1 (my pi has two wifi interfaces). If you only have one wifi interface then you should change to “wlan0” . I’ve changed these instructions to use wlan0 and added a comment about using a different interface.

    Also, even if there is a problem with the restart, the service might still start up after a reboot. It should be configured automatically to start up at boot time.

    Reply
  • 1 Jul 2019 at 05:34
    Permalink

    Yep this configuration is giving error.

    Reply
  • 26 Apr 2019 at 13:25
    Permalink

    I did all the instructions you mentioned, but it prints this error in terminal when I execute

    “sudo systemctl restart isc-dhcp-server”

    the error is the following.

    job for isc-dhcp-server.service failed because the control process exited with error code.

    Reply

Leave a Reply to Peter Merchant Cancel reply

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

Skip to toolbar