Temporary DHCP server

Start a temporary DHCP server using dnsmasq

Published on updated on

A temporary DHCP server may be needed, for example, to configure a device that, by default, gets its IP address using DHCP. When a DHCP server is not available, or the new device should not be connected to the main network, a temporary DHCP server is a solution.

Starting a DHCP server with dnsmasq is fast and simple.

Note that, amongst others, a DHCP can be started with systemd-networkd, see the Arch Linux wiki for systemd-networkd or Kea.

Install

Install dnsmasq, if needed:

if ! systemctl list-unit-files --type=service | grep -q "^dnsmasq\.service"; then
  sudo apt update && sudo apt install -y dnsmasq
  sudo systemctl disable dnsmasq
fi

sudo systemctl stop dnsmasq

Use

Make sure you have a firewall rule to allow the traffic. Example for nftables:

iif "enp0s31f6" udp dport 67-68 accept

Start dnsmasq without DNS functionality (--port=0). Example:

sudo dnsmasq --no-daemon --port=0 \
  --log-queries=extra \
  --interface=enp0s31f6 \
  --dhcp-range=192.168.1.5,192.168.1.9,1h

(optional) add the --log-dhcp and --log-debug flags for extended logging.

(optional) to pass DHCP options, add flags like:

# default gateway
  --dhcp-option=option:router,192.168.1.1
# DNS server
  --dhcp-option=option:dns-server,8.8.8.8,1.1.1.1
# NTP server
  --dhcp-option=option:ntp-server,192.168.1.1,10.10.0.5

dnsmasq --help dhcp and dnsmasq --help dhcp6 will display known DHCP options

If you need to check some parameters, the configuration file is /etc/dnsmasq.conf.

Clean

Do not just remove / purge dnsmasq. Many packages list dnsmasq and dnsmasq-base as dependency. Check with:

apt-cache rdepends --installed dnsmasq

and make a purge simulation with:

apt-get purge --simulate dnsmasq