Raspberry Pi Authoritative DNS Server

From Chorke Wiki
Revision as of 07:57, 3 August 2019 by Shahed (talk | contribs) (→‎Client)
Jump to navigation Jump to search

Install

sudo su
apt update
apt upgrade
apt install bind9 bind9utils bind9-doc dnsutils
nano /etc/default/bind9
# run resolvconf?
RESOLVCONF=no

# startup options for the server
OPTIONS="-u bind -4"

Options

nano /etc/bind/named.conf.options

acl trusted {
        127.0.0.0/8;
        10.19.83.0/24;
};

options {
        directory "/var/cache/bind";
        forwarders {
                8.8.8.8;    # Google DNS
                8.8.4.4;    # Google DNS
                10.19.83.1; # Router DNS
        };
        auth-nxdomain no;
        dnssec-validation auto;
        listen-on-v6 { none; };
        listen-on port 53 {
                127.0.0.1;
                10.19.83.3;
        };
        allow-transfer { none; };
        allow-query { trusted; };
        allow-recursion { trusted; };
};

Keygen

dnssec-keygen -K /etc/bind/ddnskeys -C -a HMAC-MD5 -b 128 -n USER apn.dev.shahed.biz
dnssec-keygen -K /etc/bind/ddnskeys -C -a HMAC-MD5 -b 128 -n USER dns.dev.shahed.biz
dnssec-keygen -K /etc/bind/ddnskeys -C -a HMAC-MD5 -b 128 -n USER gtw.dev.shahed.biz

Zones

nano /etc/bind/named.conf.local

include "/etc/bind/rndc.key";
zone "dev.shahed.biz" {
    type master;
    file "/etc/bind/zones/db.dev.shahed.biz";     # zone file path
    allow-update { key rndc-key; };
};

zone "83.19.10.in-addr.arpa" {
    type master;
    file "/etc/bind/zones/db.83.19.10";           # 10.19.83.0/24 subnet
    allow-update { key rndc-key; };
};

zone "dns.dev.shahed.biz" {
        type master;
        file "/etc/bind/zones/db.dns.dev.shahed.biz";
        notify yes;
        allow-update { key dns.dev.shahed.biz.; };
        allow-query { any; };
};

key dns.dev.shahed.biz. {
        algorithm HMAC-MD5;
        secret "8+JJL3HnWswtBVzwAetEYg==";
};
// consider adding the 1918 zones here
// include "/etc/bind/zones.rfc1918";

Forward Lookup Zone

mkdir /etc/bind/zones
cp /etc/bind/db.local /etc/bind/zones/db.dev.shahed.biz
nano /etc/bind/zones/db.dev.shahed.biz
;
; BIND data file for local interface ;
$TTL     600    ;  10S
$ORIGIN dev.shahed.biz.

@       IN      SOA     dns.dev.shahed.biz. root.dns.dev.shahed.biz. (
                        201908011           ; Serial       YYYYmmddI
                             3600           ; Refresh            01H
                              600           ; Retry              10M
                            86400           ; Expire             01D
                              600 )         ; Negative Cache TTL 10S
; NS Records
@       IN      NS      dns.dev.shahed.biz.
@       IN      A       10.19.83.3
; CNAME
ftp             CNAME   dns
rpi             CNAME   dns
; A  Records
apn             A       10.19.83.2
dns             A       10.19.83.3
gtw             A       10.19.83.1

Reverse Lookup Zone

cp /etc/bind/db.127 /etc/bind/zones/db.83.19.10
nano /etc/bind/zones/db.10.19.83
;
; BIND reverse data file for local interface
;
$TTL     600    ; 10S
$ORIGIN 83.19.10.in-addr.arpa.

@       IN      SOA     dns.dev.shahed.biz. root.dns.dev.shahed.biz. (
                        201908011           ; Serial       YYYYmmddI
                             3600           ; Refresh            01H
                              600           ; Retry              10M
                            86400           ; Expire             01D
                              600 )         ; Negative Cache TTL 10S
; NS  Records
@       IN      NS      dns.dev.shahed.biz.
; PTR Records
1.83    IN      PTR     gtw.dev.shahed.biz.
2.83    IN      PTR     apn.dev.shahed.biz.
3.83    IN      PTR     dns.dev.shahed.biz.
; PTR CNAME
3.83    IN      PTR     ftp.dev.shahed.biz.
3.83    IN      PTR     rpi.dev.shahed.biz.
; PTR ORIGIN
3.83    IN      PTR     dev.shahed.biz.

DDNS Forward Zone

cp /etc/bind/db.local /etc/bind/zones/db.dns.dev.shahed.biz
nano /etc/bind/zones/db.dns.dev.shahed.biz
;
; BIND data file for dns.dev.shahed.biz
;
$ORIGIN .
$TTL 86400              ; 1D
dns.dev.shahed.biz      IN SOA  dns.dev.shahed.biz. root.dns.dev.shahed.biz. (
                                201908011           ; serial       YYYYmmddI
                                    28800           ; refresh            08H
                                     7200           ; retry              02H
                                   604800           ; expire             01W
                                    86400 )         ; Negative Cache TTL 01D
                        NS      dns.dev.shahed.biz.
                        A       10.19.83.3

Server

named-checkconf -z
named-checkzone dns.dev.shahed.biz /etc/bind/zones/db.dns.dev.shahed.biz
named-checkzone dev.shahed.biz /etc/bind/zones/db.dev.shahed.biz
named-checkzone 83.19.10.in-addr.arpa /etc/bind/zones/db.83.19.10
update-rc.d bind9 enable
/etc/init.d/bind9 restart
service bind9 restart
reboot

Client

nano /etc/resolv.conf

nameserver 10.19.83.3
search dev.shahed.biz

nano /etc/network/interfaces

dns-nameservers 10.19.83.3
dns-search dev.shahed.biz

DDNS Client

$HOME/ddnsclient

#!/bin/bash
: '
 @author   "Chorke Academia, Inc."<[email protected]>
 @vendor    Chorke Academia, Inc.
 @web       http://chorke.org
 @version   1.0.00.GA
 @since     1.0.00.GA
'

# http://zteo.com/posts/your-own-dynamic-dns-in-3-steps
# http://dynupdate.no-ip.com/ip.php
# http://www.antedes.com/getip.php
# http://checkip.dyndns.org

IPS='http://dynupdate.no-ip.com/ip.php'
DNSP='/etc/bind/ddnskeys'

while true; do
    # first, retrieve ipaddress
    CURIP=`curl -s $IPS | awk '{ print $1 }'`
    OLDIP=`cat $DNSP/oldip`

    # compare to previously saved ip
    [ "$CURIP" == "$OLDIP" ] && continue
    echo $CURIP > $DNSP/oldip

    # if different, tell dns
    echo "server dns.dev.shahed.biz" > $DNSP/zone
    echo "zone dns.dev.shahed.biz"  >> $DNSP/zone
    echo "update delete dns.dev.shahed.biz. A"  >> $DNSP/zone
    echo "update add dns.dev.shahed.biz. 86400 A $CURIP"  >> $DNSP/zone
    echo "show" >> $DNSP/zone
    echo "send" >> $DNSP/zone
    /usr/bin/nsupdate -k $DNSP/Kdns.dev.shahed.biz.+157+62881 $DNSP/zone
    sleep 300 # (5M, 30M) = (300, 1800)
done
nohup "$HOME/ddnsclient" &

Debug

@rem clear win x dns cache
ipconfig /flushdns
ipconfig /displaydns
# clear macos dns cache
sudo killall -HUP mDNSResponder
dig @10.19.83.3 shahed.biz
dig @10.19.83.3 dev.shahed.biz
dig @10.19.83.3 apn.dev.shahed.biz
dig @10.19.83.3 dns.dev.shahed.biz
dig @10.19.83.3 gtw.dev.shahed.biz
nslookup shahed.biz
nslookup dev.shahed.biz
nslookup apn.dev.shahed.biz
nslookup dns.dev.shahed.biz
nslookup gtw.dev.shahed.biz

References