Friday, November 3, 2017

Easy way to configure BIND 9 DNS server.

"I heard sometimes people need DNS" - anonymous

Now, there are two ways to configure DNS: easy way and hard way.
If you choose to do it hard, you have to dig manuals, search examples and go by trial and error, because neither of it is perfect.
OR
You can see example below. 

Example was made on Ubuntu, so on any other Linux some path may differ.

  1.  Go to /etc/bind/ and edit file named.conf.local
    add following in the end of file
    include "/srv/www/p0rc0-r0ss0.com/dns/zone.conf"
    /!\ In this example I assume, that we have virtual hosting on our server, and every website has it's own DNS zone in website folder. You can make your location as it is convenient to you. 
  2. Now go to your DNS zone folder (in my case it's /srv/www/p0rc0-r0ss0.com/dns/), and create file zone.conf
    /!\ Note, that rule allow-query restricts DNS usage to certain IP subnets.
    File should contain following:
    zone "p0rc0-r0ss0.zn" IN {
            type master;
            file "/srv/www/p0rc0-r0ss0.com/dns/p0rc0-r0ss0.zn";
            allow-query { 192.168.0.0/24; 127.0.0.1; };
            notify no;
    };

    zone "0.168.192.in-addr.arpa" IN {
            type master;
            file "/srv/www/p0rc0-r0ss0.com/dns/p0rc0-r0ss0.rzn";
            allow-query { 192.168.0.0/24; 127.0.0.1; };
            notify no;
    };
  3. Now create file p0rc0-r0ss0.zn and p0rc0-r0ss0.rzn here we place our DNS zones.
    Forward zone looks like this:$TTL 1W
    @               1D IN SOA       p0rc0-r0ss0. root.p0rc0-r0ss0. (
                                    2012090501      ; serial
                                    3H              ; refresh
                                    15M             ; retry
                                    1W              ; expiry
                                    1D )            ; minimum

                            IN A            192.168.0.254
                            1D IN NS        ns.p0rc0-r0ss0.
                            1D IN MX        10 mail.p0rc0-r0ss0.

    ns                       A               192.168.0.254
    mail                     A               192.168.0.254
    web                      A               192.168.0.254
    host1                    A               192.168.0.8
    host2                    A               192.168.0.15
  4. /!\ Notice, that you can only reverse lookup one DNS name per IP or names will be look up in round robin style, which is bad practice.
    and reverse zone is like this:

    $ORIGIN 0.168.192.in-addr.arpa.
    $TTL 1W
    ; /!\ Warning, trailing dots are mandatory!
    ;                           our domain  admin eMail
    @          1D IN SOA       p0rc0-r0ss0. root.p0rc0-r0ss0. (
                               2013080901      ; serial
                               3H              ; refresh
                               15M             ; retry
                               1W              ; expiry
                               1D )            ; minimum

                              1D IN NS        dns.p0rc0-r0ss0.

    ;IP host name
    8                       PTR             host1.p0rc0-r0ss0.
    15                      PTR             host2.p0rc0-r0ss0.
    254                     PTR             web.p0rc0-r0ss0.
Don't forget to check out /var/log/syslog for any late news ;)

Well, that's pretty much it. Of course you should remember, that if you want your DNS to work, client should use it for name resolve. This can be achieved either by setting your DNS as primary server in OS config, or by making delegation via global DNS registration company, but that's another story.

p.s.
It would be good idea to set our new DNS as primary for our server. 
Go to /etc/network/interfaces and set:
iface eno1 inet static
        address         192.168.0.250
        netmask         255.255.255.0
        gateway         192.168.0.1
        broadcast       192.168.0.255

        dns-nameservers 127.0.0.1


VIM cheat sheet

Basic basics :) i - start editing, current symbol a - start editing, next symbol Esc - stop editing :w - write to disk :w <filename> -...