DNS server with Raspberry Pi

Make sure that Raspberry Pi is assigned a static IP address within the local network. Do this by assign it a static ip address using your DHCP server (on your router) or assign Raspberry Pi a unique IP address:

sudo nano /etc/network/interfaces

Now you can install and configure BIND. Use the following command:

sudo apt-get install bind9 bind9utils dnsutils
sudo nano /etc/bind/named.conf.local

Now set up two zones there: One for the forward lookup, where the domain’s IP address is searched, and a reverse lookup for the inverse query. This example assumes that your local network address begins with 192.168.178. If this isn’t the case, then you need to enter the correct address. 

zone "home.lan" IN {
        type master;
        file "/etc/bind/db.home.lan";
  };
zone "178.168.192.in-addr.arpa" {
        type master;
        file "/etc/bind/db.rev.1.168.192.in-addr.arpa";
  };
zone "mydomain.com" {
    type master;
    file "/etc/bind/db.mydomain.com";
};

The last two entries in the file have to be customized. Enter the static IP address of your Raspberry Pi and of your router. Make sure that the domain names always end with a period. At the beginning of the file, after the serial number, set how much time there should be in between regular actions. The two declarations NS and MX specify that both the name server and the mail server are provided by the Raspberry Pi.

sudo nano /etc/bind/db.home.lan
 
$TTL 86400
home.lan. IN SOA raspberry.home.lan. hostmaster.home.lan. (
       2019072801 ; serial
        8H ; refresh
        4H ; retry
        4W ; expire
        1D ; minimum
    )
home.lan. IN NS raspberry.home.lan.
localhost      IN A 127.0.0.1
raspberry    IN A 192.168.178.28
router           IN A 192.168.178.1
 
sudo nano /etc/bind/db.mydomain.com
$TTL 86400
mydomain.com. IN SOA ns1.mydomain.com. hostmaster.mydomain.com. (
       2019072802 ; serial
        8H ; refresh
        4H ; retry
        4W ; expire
        1D ; minimum
    )
mydomain.com. IN NS ns1.mydomain.com.
localhost      IN A 127.0.0.1
ns1    IN A 192.168.178.28
router           IN A 192.168.178.1
 
sudo nano /etc/bind/db.rev.178.168.192.in-addr.arpa
@ IN SOA raspberry.home.lan. hostmaster.home.lan. (
        2019072801 ; serial
        8H ; refresh
        4H ; retry
        4W ; expire
        1D ; minimum
    )
               IN NS raspberry.home.lan.
    1         IN PTR router.home.lan.
    31       IN PTR raspberry.home.lan.

If you install a DNS server on your Raspberry Pi, then this functions as a cache of DNS queries. This means that as soon as you’ve queried a name resolution, the entry remains saved in your DNS server. For now, DNS queries are still forwarded to another server. The location of which can be set in /etc/bind/named.conf.options. For example, you can enter the IP address of your internet provider’s DNS server here or of an open system. The Google server (8.8.8.8 and 8.8.4.4) is a popular choice. Open the file and change the IP address in the “Forwarders” entry:

sudo nano /etc/bind/named.conf.options
 
forwarders {
      8.8.8.8;
      8.8.4.4;
    };
 
sudo service bind9 restart

Now you just have to enter your new DNS server into your router’s settings, so that requests for name resolution run through your Raspberry Pi. In the device settings (usually accessed via the web interface), enter the IP address of the Raspberry Pi.

In order to identify clients dns queries, bind query log needs to enabled. For BIND 9, turn on query logging with:

sudo rndc querylog
#  Use the same command used to disable it
sudo rndc querylog

The Queries will be logged to /var/log/syslog file. The name server will log a one-line message each time it receives a query. For BIND 9, the messages look like this:

Feb  5 09:12:11 ns1 named[80090]: client 192.168.10.12#3261: query: www.server.example IN A