dnsmasq
is an amazing tool that solves one big headache for developers. It's a small DNS server that allows you to develop locally, using wildcard DNS. This means you can point *.local.test
to localhost
and work on any project without additional configuration. This is incredibly useful since, without it, you'd have to set up a bunch of DNS entries for api.local.test
, site.local.test
, and so on, which takes up a lot of time.
This sparktree post covers setting dnsmasq
up on Mac, and one would think that in Linux you could just use systemd-resolved
to set up resolution for custom local hosts like a more powerful /etc/hosts
file. However, the authors of this great system claim that this is a non-starter. This means that if you want to use this solution in Linux, you need the following workaround:
dnsmasq
and systemd-resolved
togeher
If you just attempt to install dnsmasq
, it will complain, when it goes to start the service, that there is already something running on port 53. This is due to the systemd-resolved
daemon is running locally for you to use.
The solution to this involves binding dnsmasq
to another IP so it can run in parallel and respond for local wildcard development domain.
Using the dnsmasq.conf
from the Mac example above, you can add the following to it to bind to another IP and allow it to run:
listen-address=127.0.0.2
bind-interfaces
and you will need to uncomment the following line in /etc/default/dnsmasq
so that we don’t create a lookup loop:
IGNORE_RESOLVCONF=yes
Start the dnsmasq
service and it should stay running now.
$ sudo systemctl start dnsmasq
At this point, dnsmasq
will respond for your local dev wildcard domain only and your system resolver is resolving everything else, but isn’t aware of your wildcard domain.
dnsmasq
Now you need to configure systemd-resolved
to send DNS queries for our wildcard domain to dnsmasq
.
In the /etc/systemd/resolved.conf
uncomment and configure the following:
[Resolve]
DNS=127.0.0.2
Domains=~local.test
Now restart the service for it to take effect:
$ sudo systemctl restart systemd-resolved
Now we get the best of both worlds, queries for our wildcard dev domain go to dnsmasq and queries for everything else are looked up according to /etc/resolv.conf
:
$ host foo.local.test
foo.local.test has address 127.0.0.1
And there you have it: enjoy developing locally with wildcard DNS on your Linux machine!