Setup Raspberry Pi DNSMASQ as a DHCP server

“dnsmasq” refers to itself as “A lightweight DHCP and caching DNS server”. “Dnsmasq” is a very versatile daemon. In addition to being a DHCP server, it can also be used to provide DNS services (DNS forwarders) and IPv6 router advertisements.
Preparing to operate a DHCP server
- Set IPv4 address range
- Set subnet mask and default gateway
- Set DNS server
- Set lease time
- Alternative: Setup ISC DHCP server
Note: Name of the network interfaces
Since Raspbian Stretch, the Ethernet and WLAN network interfaces have different names. So no longer “eth0” and “wlan0”, but “enx …” and “wlx …”. This concerns USB-connected network adapters whose names differ from the designations mentioned here. This means that one must first determine the individual name or change the naming to the old method.
Important: Static IPv4 configuration
It makes sense that the relevant interface with the DNSMASQ should serve the IP network with a static IPv4 configuration.
It is important that the IPv4 configuration is bound to the relevant interface, even if there is no physical connection to the network.
Note: It is recommended to make the IPv4 configuration via the file “/etc/network/interfaces”.
Setup DNSMASQ
First we install the “dnsmasq” daemon.
sudo apt-get install dnsmasq
Then we continue with the configuration. Before we save the default configuration file and create a new one.
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf_old
sudo nano /etc/dnsmasq.conf
There we enter the following lines as minimal configuration:
# DHCP server active for WLAN interface
interface = wlan0
# DHCP server not active for existing network
no-dhcp-interface = eth0
# IPv4 address range and lease time
dhcp-range = 192.168.1.100,192.168.1.150,24h
# DNS
dhcp-option = option: dns-server 192.168.1.1
Here the DHCP server and the DNS forwarding for the network interface “wlan0” is activated and for the interface “eth0” only DNS, but no DHCP is served. In addition, it is specified that in the WLAN, the IPv4 addresses are assigned from 192.168.1.100 to 192.168.1.150 for the clients and the address of the DNS server is specified.
Test configuration
Before commissioning, it is recommended to test the configuration.
dnsmasq --test -C /etc/dnsmasq.conf
The syntax check should succeed with “OK”.
Enable DNSMASQ
Restart DNSMASQ:
sudo systemctl restart dnsmasq
Show DNSMASQ status:
sudo systemctl status dnsmasq
Start DNSMASQ at system startup:
sudo systemctl enable dnsmasq
Troubleshooting
If it is not quite clear if and how “dnsmasq” distributes the IPv4 addresses, you can show the clients’ communication with the DHCP server:
sudo dnsmasq -dd -C /etc/dnsmasq.conf
You can see the lease file here:
cat /var/lib/misc/dnsmasq.leases
Just used your config file with minor mods. It produced a syntax error for the DNS option. There should be no space between “option:” and “dns-server” and “dns-server” should be followed by a comma. So the line should read:
dhcp-option = option:dns-server, 192.168.1.1
Just used your config file with minor mods. It produced a syntax error for the DNS option. There should be no space between “option:” and “dns-server” and “dns-server” should be followed by a comma. So the line should read:
dhcp-option = option:dns-server, 192.168.1.1
You’ve also missed out the netmask from the dhcp-range. It should be:
dhcp-range = 192.168.1.100, 192.168.1.150, 255.255.255.0, 24h
dnsmasq doesn’t start its dhcp server without it.