In Ubuntu Bionic, I found that the dnsmasq package no longer creates a service for dnsmasq that you can control with
systemctl. After a fair amount of experimenting and some help from the friendly folk at
#systemd on irc.freenode.net , I ended up with a dnsmasq service file that does the right things, namely:
- wait for the LAN interface to be online (since my dnsmasq listens only on LAN), and then start the dnsmasq service.
Here goes the systemd unit file which you can place in
[Unit] Description = Self-created DNSMasq service unit file After=sys-subsystem-net-devices-enp4s0.device [Service] Type=forking ExecStartPre=/lib/systemd/systemd-networkd-wait-online -i enp4s0 ExecStart=/usr/sbin/dnsmasq Restart=on-failure RestartSec=15 [Install] WantedBy=sys-subsystem-net-devices-enp4s0.device
Once you have created the service file, you must enable it with
sudo systemctl enable dnsmasq.service . You of course need to make sure to use the correct device names for your system (my network device is listed by systemctl as sys-subsystem-net-devices-enp4s0.device). You can list all the devices systemd knows on your machine using
systemctl -t device. Use grep to filter for your specific device (interface) name if you know what it’s called. Mine was called “enp4s0”.
The short summary of the above systemd unit file is that:
- It is wanted by my LAN ethernet device, so it is launched when the device has been registered by udevd (or whatever subsystem handles this).
- It’s of type “forking” because dnsmasq is a daemon which forks itself and you need this configuration for systemd to track it correctly.
- In order to wait until the LAN is actually routable, I had to use the
ExecStartPre(thanks #systemd) to use the systemd-networkd-wait-online application.
- ExecStartPre just executes specified binary or script before it actually launches your desired process.
- this application basically blocks until the specified interface is routable (which means it has an IP address).
- You must use the full path to the executable.
- Once it’s routable, then dnsmasq is executed (ExecStart), and dnsmasq by default will load the config file in /etc/dnsmasq.conf