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”.
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.
Before commissioning, it is recommended to test the configuration.
dnsmasq --test -C /etc/dnsmasq.conf
The syntax check should succeed with “OK”.
sudo systemctl restart dnsmasq
Show DNSMASQ status:
sudo systemctl status dnsmasq
Start DNSMASQ at system startup:
sudo systemctl enable dnsmasq
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: