Temporary DHCP Server With Dnsmasq
Occasionally I need a temporary DHCP server in a lab/test setting, serving leases on a specific network interface and log relevant information to stdout. Setting up a RaspberryPi or messing with access points are examples for such occasions. While one could simply connect the device to the home network, I find it tedious checking the network DHCP server’s leases table and filter out the right device. Furthermore I seek for a method which is available in the field (e.g. installed per default1) and works in isolated environments. I like to use Dnsmasq for this purpose.
First, let’s setup the interface, assuming eth1 is our target.
$ ip l set dev eth1 up
$ ip a add 192.168.42.1/24 dev eth1
Next, the DHCP server:
$ dnsmasq -p 0 -d -C '' -z -F 192.168.42.10,192.168.42.100 -i eth1
Here are the options explained:
-p 0 disables DNS Server functionality of dnsmasq. “Start DNS on port 0 => Don’t start DNS”
-d enables debug mode.
-C '' provides the empty string as the configuration file name, ignoring the default configuration.
-z ensures that only the later specified interface is bound to. Dnsmasq defaults to some dynamic binding functionality otherwise. 2
-F <start>,<end> enables the DHCP Server with a pool of ip addresses.
-i <iface> specifies the network interface we want to bind to.
A successful DHCP transaction would look like the following.
$ dnsmasq -p 0 -d -C '' -z -F 192.168.42.10,192.168.42.100 -i eth1
dnsmasq: started, version 2.93 DNS disabled
dnsmasq: compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset nftset auth DNSSEC loop-detect inotify dumpfile
dnsmasq-dhcp: DHCP, IP range 192.168.42.10 -- 192.168.42.100, lease time 1h
dnsmasq-dhcp: DHCP, sockets bound exclusively to interface eth1
dnsmasq-dhcp: DHCPDISCOVER(eth1) 50:c7:bf:b5:f8:3e
dnsmasq-dhcp: DHCPOFFER(eth1) 192.168.42.92 50:c7:bf:b5:f8:3e
dnsmasq-dhcp: DHCPDISCOVER(eth1) 50:c7:bf:b5:f8:3e
dnsmasq-dhcp: DHCPOFFER(eth1) 192.168.42.92 50:c7:bf:b5:f8:3e
dnsmasq-dhcp: DHCPREQUEST(eth1) 192.168.42.92 50:c7:bf:b5:f8:3e
dnsmasq-dhcp: DHCPACK(eth1) 192.168.42.92 50:c7:bf:b5:f8:3e TL-WA901ND