A Raspberry Pi is a tiny computer running Linux, that can be used in many home projects, including being used as a critical node on your network, like 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 for quick progress on Raspberry Pi, you can check out my e-book here. It’s a 30-day challenge, where you learn one new thing everyday 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 doesn’t have a network port to do this, and the 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 this network concept or for real use with specific network requirements. For example, 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 (you can find the article 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 forward, but it also brings DHCP features for small networks.
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 to fix the Raspberry Pi IP address.
At this step, 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 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 define 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 for 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:
static domain_name_servers=192.168.1.1 188.8.131.52
- Save and exit Nano (CTRL+W, CTRL+X).
- Now, you can restart your Raspberry Pi to apply the changes:
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.
Install the prerequisites
We can now continue with the Raspberry Pi OS initial configuration: configure the network, 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
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 forward 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 depends 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.
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 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||Which interface will be use for your DHCP server.|
eth0 for Ethernet, wlan0 for Wi-Fi
|bind-dynamic||Avoid issues with network outages|
|domain-needed||DNS option: never forward plain names (without dots)|
|bogus-priv||DNS option: never forward bad addresses|
|dhcp-range||Enable DHCP and set the range, subnet mask and lease time|
DNSMasq is a service, to apply changes you need to restart it:
sudo service dnsmasq restart
Once done, any device your 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 use 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.
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 using 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 in 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 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 answers.
Once installed, you’ll have access to a web interface with many configurations 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 for 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.
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.
Raspberry Pi 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 multiples projects with step-by-step guides.
Download the e-book
If you just want to hang out with me and show your support, you can also join the Patreon community. I share behind-the-scenes content there and give you early access to my content. You’ll also get a shoutout when you join.
More details here
You can also find all my recommendations for tools and hardware on this page.