3 reasons to turn your Raspberry Pi into a DNS server with dnsmasq

Spread the love

I love my little Raspberry Pi. The $35 computer has a ton of uses, from the utilitarian to the hobby project. But in practice, how many of us are going to build a homebrewed Amazon Echo? My guess is not many. But there is one use for a Pi that I’ve been a big fan of for almost a year: a simple DNS server.

DNS and dnsmasq

By making DNS requests from a local Raspberry Pi instead of a remote server, you can realize a few advantages. Fetching any kind of data from a local area network will always be faster than fetching something from the Internet.

If you’re not familiar with how DNS works, I recommend reading Marco Chiappetta’s article about how to speed up your DNS. If it still sounds complex, there’s a comic series that explains how DNS works using cute cartoon servers.

The Linux program dnsmasq is a lightweight DNS and DHCP server that can be found in router operating systems like DD-WRT. While the Raspberry Pi may be a little underpowered for other routing and firewall functions, I’ve noticed that my Raspberry Pi 2 offers more than enough power to run dnsmasq. On top of that, the dnsmasq configuration files are much easier to understand than some other DNS servers.

Setting up dnsmasq requires setting up a server on a Raspberry Pi, which is its own task. The Arch Linux Wiki has a pretty good article on how to set up dnsmasq, and the main configuration file (/etc/dnsmasq.conf) is well documented. If you’re new to setting up servers, I recommend running Ubuntu on your Pi.

1. You can cache DNS lookups for decreased load times

One of the main advantages to running a local DNS server is that the server can cache DNS lookups for future use. While this seems trivial—you’re really only trying to get IP addresses for domains, after all—it can add up.

CNN resourcesAlex Campbell

The list on the left of the window shows all of the domain requests made when opening CNN.com’s homepage. Note that some browser plugins (like Buffer) make their own requests.

When you load a web page, your device may perform a dozen or more DNS lookups. Obviously, there’s the website itself. But today’s websites may be loaded with Javascript plugins for everything from animations to analytics gathering. Each service requires its own DNS lookup. If the website uses a content delivery network (CDN) to serve images or videos, that’s another lookup. Then there are the advertising elements and social media buttons. And so on.

By caching these IP addresses, the Raspberry Pi cuts down on network latency because it’s on your local network. Granted, we’re talking about fractions of a second here, but those fractions of a second are what people pay for when they buy high-bandwidth internet plans from their ISPs.

Leave a Reply

Your email address will not be published. Required fields are marked *