I’ve got numerous headless and virtualised machines up and running at my home. Accessing them by IP address is cumbersome (I tend to remember words better than numbers) and editing your /etc/hosts file on each and every machine is also not very convenient in this case. Sure, there are solutions like mDNS/Bonjour but this also involves installing a mDNS responder on each machine and does not work great when using multiple aliases for a single machine. Instead, I just want to have a single place where I can maintain my hostnames and be able to access them across the network from every machine running a local name server. In a small environment like at my home, running a name server does not need much resources and as such a Raspberry Pi is sufficiently equipped for this task.
As distribution for my RPi, I settled for Arch Linux, even though I prefer Debian on my other servers. It appears to be very lightweight and doesn’t install lots of cruft that I do not use anyways. Following the installation instructions provides you directly with a bare minimum installation from where you can go on and install the essential missing packages (I added: vim, screen, wpa_supplicant, dnsutils and dnsmasq). Once done, we need to assign it a static IP address (the only one you should remember ;)) for which I used the default systemd-networkd service and changes the /etc/systemd/network file to something like:
[Match] Name=eth0 [Network] DNS=126.96.36.199 Address=10.0.1.3/24 Gateway=10.0.1.1
(Note: I use the public DNS server from Google as upstream name server, but you can use the one from your own ISP if you do not trust Google).
Next stop is the actual DNS server. I’m only interested in running a forwarding name server with the authority over a couple of hostnames. As such, running and maintaining a BIND installation is a bit of overkill for my situation. Instead, I went for Dnsmasq, which is small and easy to administer: upstream DNS servers go into /etc/resolv.conf (which is automatically maintained by systemd-networkd for you) and the hostnames go into /etc/hosts. The only thing we need is configure it a bit: create /etc/dnsmasq.d/ and create a file names dns.conf with the following content:
# DNS configuration port=53 expand-host domain=mydomain.com local=/mydomain.com/
This is all there is to it! After restarting dnsmasq with systemctl restart dnsmasq, your nameserver is up and running and able to respond to all names mentioned in your /etc/hosts (you might want to replace the mydomain.com with your own domain). To test it, ask it a question like:
$ dig @localhost www.raspberrypi.org⏎ ; <<>> DiG 9.9.2-P2 <<>> @localhost www.raspberrypi.org ; (2 servers found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53177 ;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 2, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;www.raspberrypi.org. IN A ;; ANSWER SECTION: www.raspberrypi.org. 178 IN CNAME lb.raspberrypi.org. lb.raspberrypi.org. 178 IN A 188.8.131.52 lb.raspberrypi.org. 178 IN A 184.108.40.206 lb.raspberrypi.org. 178 IN A 220.127.116.11 lb.raspberrypi.org. 178 IN A 18.104.22.168 ;; AUTHORITY SECTION: raspberrypi.org. 1268 IN NS ns1.mythic-beasts.com. raspberrypi.org. 1268 IN NS ns2.mythic-beasts.com. ;; Query time: 31 msec ;; SERVER: ::1#53(::1) ;; WHEN: Wed Mar 11 14:02:31 2015 ;; MSG SIZE rcvd: 182
All left to do is update all of your other machines to make use of this DNS server.