As always, the first thing you have to ask yourself is that why do you want to set up a DNS server? Here are a few of mine:
Access web services via custom domains instead of IP addresses
Update multiple devices (including mobile devices) is time consuming and inconvenient
Router does not provide a DNS server
I run many web services in the local network, for example, GitLab for code repository and Ghost for blogging. Instead of typing IP address for each service, it will be much easier to use custom domain names, for example, accessing my blog via http://ghost/, not dot com nor localhost.
This can be easily done if there is just one machine. You can update the host configuration file in the local machine such as /etc/hosts in Linux or C:\Windows\System32\drivers\etc\hosts in Windows. But I have multiple devices: a laptop, a tablet, and of course a smartphone. Updating multiple devices is a pain. And for many mobile devices running systems such as iOS or Android, it is not easy to edit the host configuration file without rooting the devices. Therefore, we will opt to update a single DNS configuration file.
DNS configuration can be inherited from the DHCP server from the local network router. Unfortunately, the router I have does not provide a built-in DNS server. I have to setup my own DNS server. Once the DNS server has been setup, the router will use the server as the primary DNS server, and falls back to default gateway IP as the secondary DNS server, or we can use either Google’s public DNS server with IP address 22.214.171.124 or any other alternatives.
Primary DNS server: 192.168.0.100
Secondary DNS server: 192.168.0.1, 126.96.36.199 or others
I am going to setup a DNS server in a Debian Jessie machine. Here is a summary of steps:
Add custom zone
BIND (Berkley Internet Naming Daemon) is the most common program used for maintaining a name server on Linux. In Debian, we use the bind9 package:
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;ghost. IN A
;; ANSWER SECTION:
ghost. 60 IN A 192.168.0.100
;; AUTHORITY SECTION:
ghost. 60 IN NS ghost.
;; Query time: 3 msec
;; SERVER: 192.168.0.100#53(192.168.0.100)
;; WHEN: Thu Nov 2612:00:00 GMT 2015
;; MSG SIZE rcvd: 64
Reverse check will return the custom domain ghost:
$ dig -x 192.168.0.100
Now it’s time to update the router.
Router usually provides a DHCP server. We just need to provide a custom DNS servers instead of obtaining from upstream:
The primary DNS server is the one we just setup, and the secondary is a 114DNS, a DNS server from China. Give some time or just clear the cache in each of devices, it will start using the DNS server we just setup, for example, from the iOS:
Now just type in the following URL in your browser from any of your devices: