How to Configure DNS for Developing Webapp with Dnsmaqs once for all

When you install a new project, you often need to edit your /etc/hosts file. It isn’t really painful but wouldn’t it be better if we could avoid it?

I have used Dnsmasq for a decade to solve these kinds of problems, and I’m really happy with it. I asked around me, and it does not seem to be widely used! In this blog post I want to show you how simple it is to install and setup so you never have to edit your /etc/hosts by hand 😀

Section intitulée dnsmasq-a-light-dns-serverDnsmasq, a light DNS server

From its documentation:

Dnsmasq provides network infrastructure for small networks: DNS, DHCP, router advertisement and network boot. It is designed to be lightweight and have a small footprint

With Dnsmasq, you’ll be able to resolve any TLD to a specific IP address. For example, all the following domain names will resolve to

  • project.test
  • another-project.test
  • it.works.with.subdomain.too.test

Section intitulée installation-and-configurationInstallation and configuration

On Ubuntu based distribution, you’ll need to run:

sudo apt update
sudo apt install dnsmasq

Now, to resolve all test TLD to

echo 'address=/test/' | sudo tee /etc/dnsmasq.d/test

Since we changed the configuration, we need to restart the daemon

systemctl restart dnsmasq

On some distributions, systemd adds a custom resolver. So you’ll need to disable it.

echo '[Resolve]
DNSStubListener=no' | sudo tee /etc/systemd/resolved.conf
systemctl restart systemd-resolved

And that’s it! Ubuntu takes care of adding Dnsmasq server to the /etc/resolv.conf file for you!

Now, you’ll never have to edit your /etc/hosts file anymore!

By the way, did you know we already talked about PHP, DNS, and DNSmasq 🥖?

Commentaires et discussions