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

1. Install DNSMasq

The first step is to install DNSMasq on your Raspberry Pi. This is a lightweight DNS and DHCP server that is perfect for small networks. To install it, open a terminal window and type:

sudo apt-get install dnsmasq

2. Configure DNSMasq

Once DNSMasq is installed, you need to configure it. To do this, open the configuration file with a text editor:

sudo nano /etc/dnsmasq.conf

In the configuration file, you need to set the following parameters:

interface=eth0

dhcp-range=192.168.1.50,192.168.1.150,12h

dhcp-option=3,192.168.1.1

dhcp-option=6,192.168.1.1

These settings will tell DNSMasq to assign IP addresses in the range of 192.168.1.50 to 192.168.1.150, with a lease time of 12 hours. It will also tell clients to use the Raspberry Pi as their DNS server (192.168.1.1).

3. Start DNSMasq

Once you have saved the configuration file, you can start DNSMasq with the following command:

sudo service dnsmasq start

4. Configure the Network

The next step is to configure the network so that clients can connect to the Raspberry Pi. To do this, open the network configuration file with a text editor:

sudo nano /etc/network/interfaces

In the configuration file, you need to set the following parameters:

iface eth0 inet static

address 192.168.1.1

netmask 255.255.255.0

network 192.168.1.0

broadcast 192.168.1.255

These settings will tell the Raspberry Pi to use the IP address 192.168.1.1 on the network.

5. Restart the Network

Once you have saved the configuration file, you need to restart the network for the changes to take effect. To do this, type the following command:

sudo service networking restart

6. Test the DHCP Server

Once the network has been restarted, you can test the DHCP server by connecting a client to the network. If everything is working correctly, the client should be assigned an IP address in the range that you specified in the configuration file.

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’re 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.

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

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 option Description
interface Which interface will be used 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 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.

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

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.

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.

How to Use Raspberry Pi as a DHCP Server

The Raspberry Pi is a small, affordable, and incredibly capable, credit card sized computer. It can be used for a wide variety of projects, from basic computing and programming to running complex networks. One of the most popular uses of the Raspberry Pi is as a DHCP server, allowing you to easily manage and configure your network.

What is a DHCP Server?

A DHCP (Dynamic Host Configuration Protocol) server is a network service that automatically provides and assigns IP addresses, default gateways, and other network configuration parameters to client devices. It is the most common way of connecting devices to a network, and is used in both home and business networks.

Using Raspberry Pi as a DHCP Server

Using the Raspberry Pi as a DHCP server is a great way to have a network up and running quickly. The Raspberry Pi can be configured to act as a DHCP server, allowing it to assign IP addresses to devices on the network. This can be done using either the DNSMasq or ISC DHCP server software.

DNSMasq

DNSMasq is a lightweight, easy to configure DNS forwarder and DHCP server. It is designed to provide DNS and, optionally, DHCP, to a small network. It can be used to serve as a DHCP server for a small network, and can be configured to assign static IP addresses to specific devices.

ISC DHCP Server

The ISC DHCP server is a full-featured, open source DHCP server that can be used to serve a small to medium-sized network. It is more complex to configure than DNSMasq, but provides more features and flexibility. It can be used to assign static IP addresses to specific devices, as well as to provide dynamic IP addresses to other devices.

Conclusion

Using the Raspberry Pi as a DHCP server is a great way to quickly and easily set up a network. Whether you choose to use DNSMasq or the ISC DHCP server, the Raspberry Pi can be configured to provide DHCP services to your network. With a few simple steps, you can have a fully functioning network up and running in no time.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?