Sonntag, 19. Juni 2016

Eigener DHCP und DNS Dienst hinter Fritz!Box betreiben

Eigener DHCP und DNS Dienst hinter Fritz!Box betreiben

Mit dnsmasq kann man auf dem eigenen Server einen DHCP und DNS Server betreiben, der im LAN die Adressen vergibt und Domainnamen auflöst. Das ganze soll im Dualstack Betrieb also IPv4 und IPv6 funktionieren.
Meine Umgebung:

  • Fritz!box 6360 mit einem KabelBw (unitymedia) Kabelanschluß, DualStack also IPv4 mit NAT und ein IPv6 Präfix (/56) für das hinter dem Router liegende LAN.
  • Server: Ubuntu 14.04
Die Motivation ist eigentlich, dass man die Rechner im LAN automatisch mit den korrekten IP Adressen per DHCP versorgen kann und auch die interne Domain (fritz.box) korrekt aufgelöst wird.

dnsmasq ist schnell installiert mit
sudo apt install dnsmasq

Ganz wichtig war eine Einstellung in /etc/default/dnsmasq


# If the resolvconf package is installed, dnsmasq will use its output 
# rather than the contents of /etc/resolv.conf to find upstream 
# nameservers. Uncommenting this line inhibits this behaviour.
# Not that including a "resolv-file=<filename>" line in 
# /etc/dnsmasq.conf is not enough to override resolvconf if it is
# installed: the line below must be uncommented.
IGNORE_RESOLVCONF=yes

Solange man resolvconf nicht ignoriert, funktioniert die Weiterleitung der DNS Anfragen an die Fritzbox nicht.

Hier sind die Einstellungen, die ich in der /etc/dnsmasq.conf machen musste.

# Configuration file for dnsmasq.
#
# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv

# Add local-only domains here, queries in these domains are answered
# from /etc/hosts or DHCP only.
local=/localnet/
local=/local/
local=/fritz.box/

# If you want dnsmasq to listen for DHCP and DNS requests only on
# specified interfaces (and the loopback) give the name of the
# interface (eg eth0) here.
# Repeat the line for more than one interface.
interface=eth0

# Set the domain for dnsmasq. this is optional, but if it is set, it
# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
#     as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
#    domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"
#domain=thekelleys.org.uk
domain=fritz.box

# Uncomment this to enable the integrated DHCP server, you need
# to supply the range of addresses available for lease and optionally
# a lease time. If you have more than one network, you will need to
# repeat this for each network on which you want to supply DHCP
# service.
#dhcp-range=192.168.0.50,192.168.0.150,12h
dhcp-range=192.168.178.100,192.168.178.150,2h

# Enable DHCPv6. Note that the prefix-length does not need to be specified
# and defaults to 64 if missing/
#dhcp-range=1234::2, 1234::500, 64, 12h
dhcp-range=2a02:8070:4bc:e300::, ra-stateless, ra-names

# Do router advertisements for all subnets where we're doing DHCPv6
# Unless overriden by ra-stateless, ra-names, et al, the router 
# advertisements will have the M and O bits set, so that the clients
# get addresses and configuration from DHCPv6, and the A bit reset, so the 
# clients don't use SLAAC addresses.
enable-ra

# Override the default route supplied by dnsmasq, which assumes the
# router is the same machine as the one running dnsmasq.
#dhcp-option=3,1.2.3.4
dhcp-option=3,192.168.178.1
dhcp-option=option:dns-server,0.0.0.0,192.168.178.1

# Send DHCPv6 option for namservers as the machine running 
# dnsmasq and another.
#dhcp-option=option6:dns-server,[::],[1234::88]
dhcp-option=option6:dns-server,[::]

# Ask client to poll for option changes every six hours. (RFC4242)
dhcp-option=option6:information-refresh-time,6h
dhcp-option=option6:domain-search,fritz.box

# Set the NTP time server address to be the same machine as
# is running dnsmasq
dhcp-option=42,0.0.0.0
dhcp-option=option6:42,[::]

# Log lots of extra information about DHCP transactions.
log-dhcp

Dazu müssen in der Fritz!Box noch folgende Änderungen vorgenommen werden: 
Heimnetz-> Netzwerk -> IPv4-Adressen und IPv6 Adressen

IPv4-Adressen: hier schaltet man den DHCP Server aus.
IPv6-Adressen: da ich vom Internetanbieter ein IPv6 Präfix zugewiesen bekomme, soll die Fritz!box weiterhin die IPv6 Adressen vergeben. Aber alle anderen Konfigurationen sollen von dnsmasq kommen. Dazu wählt man "O-Flag" aus. Weiterhin habe ich mir ULA Adressen erzeugen lassen und benutze zusätzlich zu den offiziellen IPv6 Adressen noch interne ULA Adressen falls mal das Internet nicht funktioniert.













Nun habe ich mir noch die LAN Konfiguration aller Geräte in eine separate Konfigurationsdatei geschrieben und in /etc/dnsmasq.d/ abgelegt. Das Format sieht so aus:
dhcp-host=00:1a:22:03:f4:16,hostname,192.168.178.11,infinite
Dort definiere ich für bestimmte Geräte (MAC Adressen) feste Hostnamen und feste IP Adressen, welche dann auf Anfrage korrekt verteilt werden.
Nach einem Neustart des Dienstes sollte alles soweit funktionieren und nach einem halben Tag sollten so langsam alle IPs erneuert worden sein.
Interessant ist die Datei /var/lib/misc/dnsmasq.leases wo immer die jeweils den Geräten zugewiesenen IPs aufgelistet sind.
Die DHCP Anfragen werden in /var/log/syslog mit protokolliert.







1 Kommentar:

  1. danke für die anleitung -

    IGNORE_RESOLVCONF=yes bedeutet vermutlich

    no-resolv

    und es wäre noch hilfreich, die /etc/network/interfaces zu sehen.

    AntwortenLöschen