How to Use Raspberry Pi as a DHCP Server (DNSMasq & more)


A Raspberry Pi is a tiny computer running Linux that can be used in many home projects. One example of a home project a Raspberry Pi can be used for is as a critical node on your network, such as a DHCP server.

A Raspberry Pi can be used as a DHCP server by installing Raspberry Pi OS and the package DNSMasq. Then it’s required to edit the configuration to set a static IP address for the Raspberry Pi and configure the DHCP range for the other devices on the network.

In this article, I’ll show you the easiest way to do this with Raspberry Pi OS, and then discuss other solutions you can try.

If you are looking to quickly progress on Raspberry Pi, you can check out my e-book here. It’s a 30-day challenge, where you learn one new thing every day until you become a Raspberry Pi expert. The first third of the book teaches you the basics, but the following chapters include projects you can try on your own.

Can a Raspberry Pi be a DHCP Server?

As a general rule, any device running Linux with network capabilities can be a DHCP Server, so Raspberry Pi models with an Ethernet port can do this. The packages aren’t installed by default, but they are available in the official repository.

However, take notes as it comes with limitations. Some older Raspberry Pi models don’t have a network port to do this, and others only have one. So, if you need to handle several subnets, Raspberry Pi might not be the best option.

At work, I use a mini PC like this one on Amazon with Pfsense on small sites, it works fine, and it’s probably more stable than a Raspberry Pi.

Generally, you have an Internet router at home that’s already acting as a DHCP server, so most of the time there’s no need to use anything else for this.

Anyway, in some cases, the Raspberry Pi can do a fantastic job as a DHCP server, either to experiment with this network concept or for a real use with specific network requirements. I’ve tried this in the past with a wireless router. The DHCP server was on the Wi-Fi network and the Raspberry Pi routed the traffic to the Ethernet network (learn more about this here).

How to Use Raspberry Pi as a DHCP Server

The easiest way to use a Raspberry Pi as a DHCP server is to install and configure DNSMasq. Its main purpose is to be a lightweight DNS forwarder, but it also brings DHCP features for small networks.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

Install Raspberry Pi OS

I’m doing this tutorial on Raspberry Pi OS, so I recommend doing the same. It should work on other Linux distributions, but this will allow you to use the same commands and configuration options as me.

So, the first step is to install Raspberry Pi OS. If you need help with this, click on this link to read my step-by-step tutorial.

Set a static IP address to your Raspberry Pi

Once installed, the first thing to do is set a static IP address on your Raspberry Pi. If you don’t have a DHCP server currently, you probably don’t have network access yet. And even if you have a DHCP server, you need to set it to fix the Raspberry Pi IP address.

I have a step-by-step tutorial on this website where I explain how to set a static IP on a Raspberry Pi (click the link to read it now), but I’ll give you the short version here.

Firstly, you might need to define the subnet you’ll use for this setup. In general, I would recommend using the same network as your Internet router, but you can choose something different and change the router configuration later.

For example, let’s say your main subnet is 192.168.1.0/24, and your router is 192.168.1.1. You can then choose to use 192.168.1.2 for the Raspberry Pi, and define a DHCP range between 192.168.1.100 and 192.168.1.200.

When you decide this, it’s better to avoid IP addresses and ranges already used if you currently have an active DHCP server. On my network, all IPs (except the router) are between 1.20 and 1.50, so it shouldn’t cause an issue.

Anyway, follow these steps to set a static IP address:

  • Open this configuration file with Nano:
    sudo nano /etc/dhcpcd.conf
  • You should find an example of a static IP configuration at the end of the file:
  • Add your configuration the same way (or uncomment this paragraph). In my example, it can be something like:
    interface eth0
    static ip_address=192.168.1.2
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.1 8.8.8.8

  • Save and exit Nano (CTRL+W, CTRL+X).
  • Now, you can restart your Raspberry Pi to apply the changes:
    sudo reboot

After the restart, your Raspberry Pi should now be connected to the Internet, with the new IP address set, and you can continue with the basic installation.

If you have difficulties with Nano, feel free to check out my guide with all the shortcuts and commands, you can find it here.

Are you a bit lost in the Linux command line? Check this article first, for the most important commands to remember, and a free downloadable cheat sheet so you can have the commands at your fingertips.

Install the prerequisites

We can now continue with the Raspberry Pi OS initial configuration: configure the network, and update the system and packages. Enabling SSH is also a good idea, so you can copy and paste the commands I give you directly from your computer.

Once done, the only thing we need for now is to install DNSMasq, you can do this with the “Add/Remove software” tool on the Desktop version, or simply use:
sudo apt install dnsmasq

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

DNSMasq will start automatically. It’s configured as a DNS forwarder by default, so it shouldn’t be an issue as none of your devices are using the Raspberry Pi as a DNS server for now.

A DNS forwarder is a service that forwards DNS queries to your main DNS server on the network. It can be useful to add custom hosts or to keep a cache of your DNS requests. Any new requests will be forwarded to another server.

Disable your old DHCP server

Most of the time, your Internet router already acts as a DHCP server. So, you first need to disable it to avoid conflict. If you keep both enabled, your computer and other devices will get an IP address for one of them randomly (the first to answer) and it won’t work as expected.

The steps to disable it will depend on the router you have, so I can’t give you more details about this. Check the documentation for your router model or your ISP website.

Configure DNSMasq

Configuring DNSMasq is not complicated. Everything is in one configuration file, and there is only a few lines to add:

  • Open the DNSMasq configuration file with Nano:
    sudo nano /etc/dnsmasq.conf
  • Almost everything is commented on here, and it’s a pretty long file, so the easiest way is to copy and paste these lines at the end (CTRL+_ and CTRL+V):
    interface=eth0
    bind-dynamic
    domain-needed
    bogus-priv
    dhcp-range=192.168.1.100,192.168.1.200,255.255.255.0,12h


Configuration optionDescription
interfaceWhich interface will be used for your DHCP server.
eth0 for Ethernet, wlan0 for Wi-Fi
bind-dynamicAvoid issues with network outages
domain-neededDNS option: never forward plain names (without dots)
bogus-privDNS option: never forward bad addresses
dhcp-rangeEnable DHCP and set the range, subnet mask and lease time
DNSMasq configuration options

DNSMasq is a service, to apply changes you need to restart it:
sudo service dnsmasq restart

Once done, any device you plug into your network will get an IP address in the defined range. Your Raspberry Pi will act as a DHCP server (which was the goal of this tutorial) but also as a DNS forwarder. So, any host added in the /etc/hosts file can be used on other devices on your network.

The DHCP server doesn’t require any maintenance over time, but you can use tools like Webmin if you want a web interface to manage DNSMasq. It will be particularly useful to see the IP range usage and configure the DNS server.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.

Alternatives to DNSMasq as DHCP Server on Raspberry Pi

As explained at the beginning, DNSMasq is the easiest way to get a basic DHCP server running on Raspberry Pi, but it’s not the only option. You might want something more complex, with more features or a better interface. I got you covered below with other solutions you can try.

Wireless Router with Firewall

My first suggestion is to do something similar but use the two interfaces (if you have a recent model). For example, you can use your Raspberry Pi 3B+ or 4 as a wireless access point, set up the DHCP server on this interface and even add some security features with a firewall. The Ethernet card will be used to access the Internet.

That could be a great project to add a guest Wi-Fi network in a small business, but whether you’re doing this for a valid reason or just for fun, it’s an interesting project to learn a few network skills.

I have an in-depth article explaining all the steps to put this into practice, from the conception to the installation and configuration of everything. I highly recommend checking it out, even if you just need some clarification about this article.
How to use Raspberry Pi as a Wireless Router with Firewall.

Pi-Hole

Pi-Hole is a well-known ad-blocker, free and open-source that can be installed on Raspberry Pi.
Most of the time, it will be only used as your DNS server, and each request will be filtered to check if it’s an ad or not.

The installation is pretty easy: one command line and a few questions to answer.
Once installed, you’ll have access to a web interface with many configuration options.
One of them allows you to enable a DHCP server in a few clicks.

If having a web interface to configure everything is important to you, that’s probably the best option.
You can configure everything here, and you can see and set DHCP leases just below.

Want to give it a try? Check out my tutorial on how to set up Pi-Hole in 5 minutes on your Raspberry Pi.
And I also have a tutorial here on how to install Pi-Hole on Ubuntu, if you prefer using Ubuntu on a Raspberry Pi or another computer.

Recently, AdGuard is becoming more and more popular and do similar things. You can read more about it here: Getting Started With AdGuard Home On Raspberry Pi (vs Pi-Hole).

ISC-DHCP-Server

Another option is to use ISC-DHCP-Server, eventually coupled with Bind for the DNS server. It should work on Raspberry Pi, but it’s a bit more complicated to configure these services, so I don’t recommend using them.

If you have an important network where you need a more advanced solution, the Raspberry Pi is probably not the best option for you anyway. So, even if ISC-DHCP-Server is often used on Linux, I don’t think we need it on Raspberry Pi, DNSMasq is perfect in this case.

Also, for more important networks, Pfsense is not (yet?) available on Raspberry Pi, but as explained in my article here, there are still great firewall options that you can use on Raspberry Pi.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Create, understand and improve any Python script for your Raspberry Pi.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.

Patrick Fromaget

I'm the lead author and owner of RaspberryTips.com. My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

Recent Posts