I have recently set up a Pi-hole, which serves as both a DNS server and an ad-blocker for my home network. Since then, I've started deploying various web applications on my cluster of three Raspberry Pis. For those who have dealt with clusters before, you might remember the challenge of managing various port numbers. This is elegantly solved by reverse proxy tools like Traefik. However, I encountered another challenge I wanted to address: enabling wildcard DNS using Pi-hole, akin to the feature offered by Cloudflare, to streamline domain management for my applications.
Cloudflare offers a handy feature known as wildcard DNS. It allows for resolving all the names attached to a given domain with just one line, like example.com
. Implementing this in Pi-hole isn't straightforward, but it's possible because Pi-hole relies on the dnsmasq
tool. Let's log in to the Pi-hole and create a new file under /etc/dnsmasq.d
, naming it 99-wildcard.conf
. Now, let's define our domain and a wildcard à la Cloudflare:
Since we're implementing this within Pi-hole, we need to restart the Dnsmasq service. In Pi-hole v4 or higher, Dnsmasq doesn't run as a separate service; it runs as part of the Pi-hole-FTL. To restart that service, log in to the Pi-hole and run:
or
Now, if you open the terminal on one of the computers using the Pi-hole DNS and type:
You'll see that the output of the dig
command points to the IP address we defined in the Dnsmasq config file in the Pi-hole. It doesn't only resolve the domain name (casa.dev
) but also any service attached to that domain (example: service.casa.dev
).
Great, isn't it? Whatever I put in front of .casa.dev
, the DNS resolves the address immediately and points towards the IP address.