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.
Verwendete Software:
Voraussetzungen:
- Ubuntu 20.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
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; };
};
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/named
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:
[...] plugin query "filter-aaaa.so" { filter-aaaa-on-v4 yes; };
Editieren Sie die Date /etc/bind/named.conf.default-zones
und ändern Sie folgenden Parameter:
# nano /etc/bind/named.conf.default-zones
# named.conf.default-zones [...] zone "." { type hint; file "/etc/bind/db.root"; }; [...]
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
Löschen Sie eventuell vorhandene Cache-Dateien:
# rm /var/cache/bind/managed-keys.bind*
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
#!/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
# 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 via Netplan bekanntgeben
Editieren Sie die Datei /etc/netplan/01-netcfg.yaml
und tragen Sie 127.0.0.1
als Nameserver ein:
Ersetzen Sie 01-netcfg.yaml
durch die Datei, die auf Ihrem System verwendet wird.
# nano /etc/netplan/01-netcfg.yaml
# 01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
[...]
nameservers:
search: [example.com]
addresses:
- "127.0.0.1"
[...]
Wenden Sie die neue Konfiguration an:
# netplan apply
Kontrollieren Sie, ob der lokale DNS-Resolver (127.0.0.1
) als Nameserver verwendet wird:
# resolvectl status [...] Link 2 (eth0) Current Scopes: DNS DefaultRoute setting: yes LLMNR setting: yes MulticastDNS setting: no DNSOverTLS setting: no DNSSEC setting: no DNSSEC supported: no Current DNS Server: 127.0.0.1 DNS Servers: 127.0.0.1 DNS Domain: example.com
Inbetriebnahme und Test
DNS-Resolver testen
Fragen Sie einen Hostnamen (z.B. heise.de
) ab und kontrollieren Sie die Ausgabe:
# nslookup heise.de Server: 127.0.0.53 Address: 127.0.0.53#53 Non-authoritative answer: Name: heise.de Address: 193.99.144.80
Abschluss
Wenn die DNS-Abfrage erfolgreich verlief und Sie keine Fehlermeldungen erhalten haben, ist Ihr DNS-Resolver einsatzbereit.