Joseph's Blog

Getting systemd-resolved and dnsmasq to cooperate


Why run dnsmasq and systemd-resolved on the same server? Xe Iaso and David Anderson make a compelling argument for systemd-resolved on the Tailscale blog as it handles the widest variety of DNS configurations and seems to play well with other software. Importantly, Tailscale knows how to use systemd-resolved to add itself to the list of DNS resolvers for the system.

My goal with this setup is to have resolved manage the server’s DNS resolution—determining which queries are sent where. With Tailscale, I can publish some DNS records for various devices on my network that then resolve as 10.x.y.z addresses which are reachable via Tailscale. This works well for all devices running Tailscale, but there are some limitations here:

  1. If a device doesn’t have Tailscale installed/ set up properly, these 10.x.y.z addresses are unreachable
  2. Even if the device can be reached via Tailscale, while on the same LAN it’s usually quicker to use the standard LAN IP (192.168.x.y) and bypass Tailscale.

Thus, the goal of DNS resolution on my network is to resolve certain hostnames—, for example—to local IPs for all devices on the LAN. Devices on WAN are given the usual Tailscale IPs (which are only reachable by devices on my tailnet).

Long story short

Queries for MagicDNS (ending in should be send on to Tailscale, while queries for the rest of the internet (, for example) should be sent to the usual resolvers ( or similar), and queries for devices on the LAN (, for example) should be answered with the device’s LAN address, rather than the WAN/Tailscale address.

systemd-resolved and dnsmasq

resolved is easily enabled on NixOS with services.resolved.enable = true. Similarly, dnsmasq may be enabled with services.dnsmasq.enable = true. However, the two will conflict as both try to bind to port 53 by default.

To address this, dnsmasq can be told to bind/listen to only a single IP on a single interface. dnsmasq is given the enp6s18 (ethernet) interface, while resolved listens to the loopback interface.

}: let
  fqdn = config.networking.fqdn;
  address = [
  ]; # etc
in {
  services.dnsmasq = {
    enable = true;
    resolveLocalQueries = false;
    settings = {
      listen-address = [ "" ];
      interface = "enp6s18";
      bind-interfaces = true;
      no-resolv = true; # use specific upstreams, as the /etc/resolv.conf file just points to systemd-resolved (which points back to dnsmasq...)
      no-hosts = true; # ignore hosts file, which keeps telling other devices that is at
      cache-size = 500;
      server = [ "" "" "" "" "2606:4700:4700::1111" "2606:4700:4700::1001"];
      address = address;
      dnssec = true;
  networking.firewall.allowedTCPPorts = [53];
  networking.firewall.allowedUDPPorts = [53];

No additional configuration is necessary for resolved.


Probably best to reboot after all this. Now, all devices on the LAN which are using this server for DNS will be given the LAN IP addresses of various devices when they query dnsmasq. Meanwhile, the server itself will continue to use systemd-resolved, which sends all queries to Tailscale, and remaining queries are sent to dnsmasq (assuming either that upstream DNS has been manually set for resolved, or that the DHCP server on the network provides the server’s address as the DNS server).