Ubuntu 16.04 - Lokalen DNS-Resolver (cashing-only) mit Bind installieren

Aus Twilight-Networks Wiki
Wechseln zu:Navigation, Suche


Outdated.png


Diese Dokumentation ist nicht mehr aktuell und wurde ersetzt.

Verwenden Sie die Dokumentation nur noch bei gerechtfertigten Anwendungsfällen.

Neue Version: Ubuntu 20.04 - Lokalen DNS-Resolver (cashing-only) mit Bind installieren




Vorwort

Diese Dokumentation beschreibt den Installationsvorgang von Bind als Cashing-Only-DNS-Resolver.

Bitte vergewissern Sie sich, dass Ihr System über die nötigen Voraussetzungen verfügt, wie in der Infobox beschrieben. Weiterhin sind Grundkenntnisse im Umgang mit Linux erforderlich, da die Vorgehensweise, wie sie hier beschrieben ist, in manchen Teilen abweichen kann. Kein System ist wie das Andere. Die Dokumentation wurde sorgfältig von mir geprüft. Dennoch kann ich keinerlei Haftung für Schäden an Ihrem System oder eine Gewährleistung übernehmen. Bitte verwenden Sie diese Dokumentation auf eigene Gefahr.


Ubuntu 16.04 - Lokalen DNS-Resolver (cashing-only) mit Bind installieren
Stand: Stand 02/2019


Bind 9 ISC Blue.png


Verwendete Software:


Voraussetzungen:

  • Ubuntu 16.04 LTS


Weitere, empfohlene Dokumentationen:











Server vorbereiten

Root-Rechte einräumen

Während der gesamten Dokumentation werden Root-Rechte benötigt. Um auf das Voranstellen von sudo zu verzichten, verschaffen Sie sich für die gesamte Sitzung erhöhte Rechte mit:

# sudo -i


Update durchführen

Bringen Sie den Server auf den aktuellen Patchstand:

# apt update && apt upgrade



Software herunterladen und installieren

Laden Sie Bind aus dem offiziellen Ubuntu-Repository herunter:

# apt-get install bind9



Bind einrichten

Bind9 konfigurieren

Benennen Sie zunächst die /etc/bind/named.conf.options um, damit Sie eine Sicherheitskopie der Original-Datei haben:

# mv /etc/bind/named.conf.options /etc/bind/named.conf.options.bak


Erstellen Sie eine neue Datei/etc/bind/named.conf.options:

# nano /etc/bind/named.conf.options
named.conf.options
options {
    directory "/var/cache/bind";

	dnssec-validation auto;
	auth-nxdomain no;
	recursion yes;

	listen-on { 
		127.0.0.1;
	};

    allow-query { 
		127.0.0.1;
	};

    allow-transfer { none; };
    allow-update { none; };
   
    version none;
    hostname none;
    server-id none;
};

controls {
	inet 127.0.0.1 port 953 allow { 127.0.0.1; };
};


Hinweis!

Wenn Sie den DNS-Resolver an einem Internet-Anschluss ohne Dual-Stack (ohne IPv6) betreiben, muss IPv6 in Bind deaktiviert werden, da Bind ansonsten versucht, die Namensauflösung über die IPv6-Adressen der Root-Server durchzuführen.

Editieren Sie die dazu die Datei /etc/default/bind9 und ändern Sie folgenden Parameter:

[...]
OPTIONS="-u bind -4"


Fügen Sie der Datei /etc/bind/named.conf.options folgendes hinzu, damit IPv6-Adressen bei der DNS-Abfrage ausgeblendet werden:

options {
    [...]
    filter-aaaa-on-v4 yes;
    [...]
};


Starten Sie Bind neu, damit die Konfiguration angewendet wird:

# service bind9 restart



Root-Hints und DNSSEC-Keys aktualisieren

Laden Sie die Liste der Root-DNS-Server /etc/bind/db.root herunter:

# dig +bufsize=1200 +norec NS . @a.root-servers.net > /etc/bind/db.root


Laden Sie die Liste der dazugehörigen DNSSEC-Keys /etc/bind/bind.keys herunter:

# wget "https://ftp.isc.org/isc/bind9/keys/9.11/bind.keys.v9_11" -q -O /etc/bind/bind.keys


Starten Sie Bind neu, damit die Konfiguration angewendet wird:

# service bind9 restart



Cron-Job einrichten

Die Liste der Root-Nameserver und die dazugehörigen DNSSEC-Keys können sich in unregelmäßigen Abständen ändern. Damit unser DNS-Resolver reibungslos DNS-Abfragen durchführen kann, müssen die Listen immer auf dem neuesten Stand gehalten werden.

Dieser Cron-Job aktualisiert wöchentlich beide Listen und versendet einen Status-Report per E-Mail.


Erstellen Sie die Datei/etc/cron.weekly/bind-update-root-hints mit folgendem Inhalt:

# nano /etc/cron.weekly/bind-update-root-hints
bind-update-root-hints
#!/bin/sh
#
#########################################################################
# Script Name   : bind-update-root-hints                                #
# Description   : Updates the Bind9 root-hint and the dnssec-key file.  #
#                 Creates a Backup of the existing Files and sends a    #
#                 Status-Report by Email.                               #
#                 Runs automatically by a Cron-Job                      #
#                 -based on a Script from Al Longyear                   #
# Args          :                                                       #
#                                                                       #
# Version       : 1.00                                                  #
# Last Update   : 28.02.2019                                            #
# Author        : Twilight                                              #
# Email         : twilight@twlnet.com                                   #
# Web:          : https://www.twilight-networks.com                     #
#########################################################################
# Tested with   : Ubuntu 16.04 LTS                                      #
#                 Bind 9.10.3                                           #
#########################################################################

# Bind-Dir
BINDDIR='/etc/bind'

# Root-Hints-Filename
ROOTHINTS='db.root'

# Keys-Filename
ROOTHINTSKEYS='bind.keys'

# IP-Address to check the Internet connection (for example google-dns)
CHECKONLINE='8.8.8.8'

# Uncomment this to disable Email-Reports (use SYSLOG instead)
#EMAILREPORT=false

# Reports are sent to
MAILTO='root@example.com'

# Reports are sent by
MAILFROM='dns@example.com'



# Normaly there is no need to change anything below this comment line!
#########################################################################
if [ "$EMAILREPORT" = false ] ; then

    (
      # Check Bind-Status
     case `rndc status 2>&1` in
        *refused*)
            # Bind down
            echo "bind-update: error [Update FAILED] Root-Hints and DNSSEC-Keys NOT updated. Service Bind started?"
            exit 0
            ;;
     esac

     PATH=/sbin:/usr/sbin:/bin:/usr/bin:
     export PATH
     # NOTE: Bind-Dir must be writable only by trusted users or this script 
     # will cause root compromise/denial of service opportunities.
     cd ${BINDDIR} 2>/dev/null || {
        # Bind-Dir error
        echo "bind-update: error [Update FAILED] Root-Hints and DNSSEC-Keys NOT updated. No access to ${BINDDIR}"
        exit 1
     }

     # Online-Check
     case `ping -qnc 1 ${CHECKONLINE} 2>&1` in
       *'100% packet loss'*)
            # Network down
            echo "bind-update: error [Update FAILED] Root-Hints and DNSSEC-Keys NOT updated. Check Internet-Connection!"
            exit 1
            ;;
     esac

     #  Download Root-Hints
     dig +bufsize=1200 +norec NS . @a.root-servers.net >${ROOTHINTS}.new 2> errors
     # Download DNSSEC-Keys
     wget "https://ftp.isc.org/isc/bind9/keys/9.11/bind.keys.v9_11" -q -O ${ROOTHINTSKEYS}.new 2> errors


     # No errors in Root-Hints
     case `cat ${ROOTHINTS}.new` in
       *NOERROR*)
            :;;
       *)   
            # Download failed
            echo "bind-update: error [Update FAILED] Root-Hints and DNSSEC-Keys NOT updated. Download ${ROOTHINTS} failed."
            exit 1
            ;;
     esac

	 # No errors in DNSEC-Keys
     case `cat ${ROOTHINTSKEYS}.new` in
       *managed-keys*)
            :;;
       *)   
            # Download failed
            echo "bind-update: error [Update FAILED] Root-Hints and DNSSEC-Keys NOT updated. Download ${ROOTHINTSKEYS} failed."
            exit 1
            ;;
     esac

     # Update successful
     echo "bind-update: [Update SUCCESSFUL] Root-Hints and DNSSEC-Keys updated."
     chown root:root ${ROOTHINTS}.new ${ROOTHINTSKEYS}.new
     chmod 444 ${ROOTHINTS}.new ${ROOTHINTSKEYS}.new
     rm -f ${ROOTHINTS}.bak errors
     rm -f ${ROOTHINTSKEYS}.bak errors
     mv ${ROOTHINTS} ${ROOTHINTS}.bak
     mv ${ROOTHINTS}.new ${ROOTHINTS}
     mv ${ROOTHINTSKEYS} ${ROOTHINTSKEYS}.bak
     mv ${ROOTHINTSKEYS}.new ${ROOTHINTSKEYS}
     rndc reload
    
    ) 2>&1 | logger 
    exit 0

else

    (
     echo "Content-Type: text/plain; charset="UTF-8""
     echo "To: <${MAILTO}>"
     echo "MIME-Version: 1.0"
     echo "From: $(hostname -f)  <${MAILFROM}>"

     # Check Bind-Status
     case `rndc status 2>&1` in
        *refused*)
            # Bind down
            echo "Subject: [Update FAILED] Root-Hints and DNSSEC-Keys NOT updated."
            echo
            echo "Bind is DOWN."
            echo "${ROOTHINTS} and ${ROOTHINTSKEYS} NOT updated"
            exit 0
            ;;
     esac

     PATH=/sbin:/usr/sbin:/bin:/usr/bin:
     export PATH
     # NOTE: Bind-Dir must be writable only by trusted users or this script 
     # will cause root compromise/denial of service opportunities.
     cd ${BINDDIR} 2>/dev/null || {
        # Bind-Dir error
        echo "Subject: [Update FAILED] Root-Hints and DNSSEC-Keys NOT updated. No access to ${BINDDIR}, error $?"
        echo
        echo "Bind-Dir not available!"
        echo "${ROOTHINTS} and ${ROOTHINTSKEYS} NOT updated"
        exit 1
     }

     # Online-Check
     case `ping -qnc 1 ${CHECKONLINE} 2>&1` in
       *'100% packet loss'*)
            # Network down
            echo "[Update FAILED] Subject: Root-Hints and DNSSEC-Keys NOT updated."
            echo
            echo "Network is DOWN"
            echo "${ROOTHINTS} and ${ROOTHINTSKEYS} NOT updated"
            exit 1
            ;;
     esac

     #  Download Root-Hints
     dig +bufsize=1200 +norec NS . @a.root-servers.net >${ROOTHINTS}.new 2> errors
     # Download DNSSEC-Keys
     wget "https://ftp.isc.org/isc/bind9/keys/9.11/bind.keys.v9_11" -q -O ${ROOTHINTSKEYS}.new 2> errors


     # No errors in Root-Hints
     case `cat ${ROOTHINTS}.new` in
       *NOERROR*)
            :;;
       *)   
            # Download failed
            echo "Subject: [Update FAILED] Root-Hints and DNSSEC-Keys NOT updated."
            echo
            echo "The Download has failed"
            echo "Check reported errors in ${ROOTHINTS}:"
            echo
            cat ${ROOTHINTS}.new errors
            exit 1
            ;;
     esac

     # No errors in DNSEC-Keys
     case `cat ${ROOTHINTSKEYS}.new` in
       *managed-keys*)
            :;;
       *)   
            # Download failed
            echo "Subject: [Update FAILED] Root-Hints and DNSSEC-Keys NOT updated."
            echo
            echo "The Download has failed"
            echo "Check reported errors in ${ROOTHINTSKEYS}:"
            echo
            cat ${ROOTHINTSKEYS}.new errors
            exit 1
            ;;
     esac

     # Update successful
     echo "Subject: [Update SUCCESSFUL] Root-Hints and DNSSEC-Keys updated."
     echo
     echo "The ${ROOTHINTS} file and the ${ROOTHINTSKEYS} has been updated and Bind has been reloaded."
     echo
     echo "Status:"

     chown root:root ${ROOTHINTS}.new ${ROOTHINTSKEYS}.new
     chmod 444 ${ROOTHINTS}.new ${ROOTHINTSKEYS}.new
     rm -f ${ROOTHINTS}.bak errors
     rm -f ${ROOTHINTSKEYS}.bak errors
     mv ${ROOTHINTS} ${ROOTHINTS}.bak
     mv ${ROOTHINTS}.new ${ROOTHINTS}
     mv ${ROOTHINTSKEYS} ${ROOTHINTSKEYS}.bak
     mv ${ROOTHINTSKEYS}.new ${ROOTHINTSKEYS}
     rndc reload

     echo
     echo "A Backup has been created. You find the previous Versions at:"
     echo "${BINDDIR}/${ROOTHINTS}.bak"
     echo "${BINDDIR}/${ROOTHINTSKEYS}.bak"
     echo
     echo "----"
     echo "Actual Root-Hints:"
     echo
     cat ${ROOTHINTS}
     echo
     echo "Actual DNSSEC-Keys:"
     echo
     cat ${ROOTHINTSKEYS}
     echo
     
    ) 2>&1 | /usr/lib/sendmail -t
    exit 0

fi
#end
Anzupassende Parameter:
# bind-update-root-hints
[...]
# Reports are sent to
MAILTO='root@example.com'

# Reports are sent by
MAILFROM='dns@example.com'
[...]


Vergeben Sie die benötigte Berechtigung, damit die Datei ausgeführt werden kann:

# chmod 755 /etc/cron.weekly/bind-update-root-hints



DNS-Resolver bekanntgeben

Editieren Sie die Datei /etc/resolv.conf und tragen Sie 127.0.0.1 als Nameserver ein:

# resolv.conf
[...]
nameserver 127.0.0.1


Hinweis!

Je nach Netzwerk-Einstellung wird die Datei resolv.conf beim Systemstart überschrieben. Tragen Sie in diesem Fall den DNS-Resolver in der Datei /etc/network/interfaces ein:

# interfaces
iface eth0 inet static
    [...]
    dns-nameservers 127.0.0.1



Inbetriebnahme und Test

resolv.conf überprüfen

Starten Sie zunächst den Server neu...

# init 6


...und kontrollieren Sie den Eintrag in der Datei /etc/resolv.conf. Nach dem Neustart muss immer noch der lokale DNS-Resolver als Nameserver eingetragen sein:

# cat /etc/resolv.conf
	nameserver 127.0.0.1



DNS-Resolver testen

Fragen Sie den Hostname der IP-Adresse 8.8.8.8 ab:

# dig 8.8.8.8 | grep SERVER:
	;; SERVER: 127.0.0.1#53(127.0.0.1)


Kontrollieren Sie, ob die Abfrage über den SERVER: 127.0.0.1 durchgeführt wurde.

Abschluss

Wenn die DNS-Abfrage erfolgreich verlief und Sie keine Fehlermeldungen erhalten haben, ist Ihr DNS-Resolver einsatzbereit.