Dnsmasq ist ein leichtgewichtiger DNS-Forwarder und DHCP-Server. Es eignet sich perfekt für kleine Netzwerke, Heimserver und Entwicklungsumgebungen.
Installation
# Debian/Ubuntu
apt install dnsmasq
# RHEL/CentOS
dnf install dnsmasq
# Starten
systemctl enable dnsmasq
systemctl start dnsmasqDNS-Konfiguration
Grundkonfiguration
# /etc/dnsmasq.conf
# Interface binden
interface=eth0
bind-interfaces
# Upstream DNS-Server
server=1.1.1.1
server=8.8.8.8
# Keine Auflösung aus /etc/resolv.conf
no-resolv
# Cache-Größe
cache-size=1000
# Keine negativen Antworten cachen
no-negcache
# Logs
log-queries
log-facility=/var/log/dnsmasq.logLokale DNS-Einträge
# /etc/dnsmasq.conf
# Einzelne Einträge
address=/server1.local/192.168.1.10
address=/server2.local/192.168.1.11
# Wildcard
address=/.dev.local/192.168.1.100
# Aus /etc/hosts lesen
no-hosts
addn-hosts=/etc/dnsmasq.hosts# /etc/dnsmasq.hosts
192.168.1.10 server1.local server1
192.168.1.11 server2.local server2
192.168.1.12 db.local databaseCNAME-Records
# CNAME-Einträge
cname=www.local,server1.local
cname=mail.local,server1.localReverse-DNS
# PTR-Records automatisch aus /etc/hosts
expand-hosts
domain=local
# Oder manuell
ptr-record=10.1.168.192.in-addr.arpa,server1.localDHCP-Server
Basis-Konfiguration
# /etc/dnsmasq.conf
# DHCP aktivieren
dhcp-range=192.168.1.100,192.168.1.200,24h
# Gateway
dhcp-option=option:router,192.168.1.1
# DNS-Server (dieser Host)
dhcp-option=option:dns-server,192.168.1.1
# Domain
dhcp-option=option:domain-name,local
# Lease-Datei
dhcp-leasefile=/var/lib/dnsmasq/dnsmasq.leasesStatische Zuweisungen
# MAC zu IP
dhcp-host=00:11:22:33:44:55,192.168.1.50,server1
dhcp-host=00:11:22:33:44:66,192.168.1.51,server2,infinite
# Aus Datei
dhcp-hostsfile=/etc/dnsmasq.dhcp-hosts# /etc/dnsmasq.dhcp-hosts
00:11:22:33:44:55,192.168.1.50,server1
00:11:22:33:44:66,192.168.1.51,server2DHCP-Optionen
# NTP-Server
dhcp-option=option:ntp-server,192.168.1.1
# TFTP-Server (für PXE)
dhcp-option=option:tftp-server,192.168.1.1
# Lease-Zeit
dhcp-range=192.168.1.100,192.168.1.200,12h
# Verschiedene Bereiche
dhcp-range=set:server,192.168.1.50,192.168.1.99,infinite
dhcp-range=set:client,192.168.1.100,192.168.1.200,24hMehrere Subnetze
# Subnetz 1
dhcp-range=eth0,192.168.1.100,192.168.1.200,24h
dhcp-option=eth0,option:router,192.168.1.1
# Subnetz 2
dhcp-range=eth1,192.168.2.100,192.168.2.200,24h
dhcp-option=eth1,option:router,192.168.2.1PXE-Boot
# TFTP aktivieren
enable-tftp
tftp-root=/var/lib/tftpboot
# PXE-Boot-Datei
dhcp-boot=pxelinux.0
# Für UEFI
dhcp-match=set:efi-x86_64,option:client-arch,7
dhcp-boot=tag:efi-x86_64,grubnetx64.efiDNS-Blacklisting
# Werbung blockieren
address=/ad.doubleclick.net/0.0.0.0
address=/ads.facebook.com/0.0.0.0
# Aus Datei
addn-hosts=/etc/dnsmasq.block# /etc/dnsmasq.block
0.0.0.0 ad.doubleclick.net
0.0.0.0 ads.facebook.com
0.0.0.0 tracking.example.comPi-hole-artig
#!/bin/bash
# update-blocklist.sh
BLOCKLIST_URL="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
OUTPUT="/etc/dnsmasq.block"
curl -s "$BLOCKLIST_URL" | grep "^0.0.0.0" > "$OUTPUT"
systemctl reload dnsmasqDNS-over-HTTPS/TLS
Mit dnscrypt-proxy
# Als Upstream für dnsmasq
server=127.0.0.1#5353Mit Cloudflared
# cloudflared als DoH-Proxy
# dnsmasq nutzt localhost
server=127.0.0.1#5053Monitoring
Statistiken
# SIGUSR1 für Statistiken
kill -USR1 $(pidof dnsmasq)
# In /var/log/syslog:
# queries forwarded 1234, queries answered locally 567Lease-Datei
cat /var/lib/dnsmasq/dnsmasq.leases
# Format:
# EXPIRE MAC IP HOSTNAME CLIENT-IDDNS-Abfrage-Log
# In Config
log-queries
# Beobachten
tail -f /var/log/dnsmasq.logSicherheit
Zugriff beschränken
# Nur lokale Anfragen
local-service
# Bestimmte Interfaces
interface=eth0
bind-interfaces
# Keine Forwarding
localise-queriesDNSSEC
# DNSSEC-Validierung
dnssec
trust-anchor=.,19036,8,2,49AAC11D7B6F6446702E54A1607371607A1A41855200FD2CE1CDDE32F24E8FB5
# Oder Upstream vertrauen
proxy-dnssecEntwicklungsumgebung
Lokale Domains
# Alle .test-Domains auf localhost
address=/.test/127.0.0.1
# Wildcard für Projekte
address=/.project1.dev/192.168.1.100
address=/.project2.dev/192.168.1.101Docker-Integration
# Docker-Container über DNS
# Container haben Namen wie container.docker.local
server=/docker.local/172.17.0.1Konfiguration testen
# Syntax prüfen
dnsmasq --test
# Mit eigener Config
dnsmasq --test --conf-file=/etc/dnsmasq.conf
# Debug-Modus (Vordergrund)
dnsmasq --no-daemon --log-queriesTroubleshooting
DNS-Test
# Lokale Auflösung testen
dig @localhost server1.local
nslookup server1.local 127.0.0.1
# Cache-Statistiken
dig @localhost +short txt chaos stats.dnsmasqDHCP-Test
# DHCP-Leases anzeigen
cat /var/lib/dnsmasq/dnsmasq.leases
# DHCP-Log
journalctl -u dnsmasq | grep DHCPPort-Konflikte
# systemd-resolved deaktivieren (Ubuntu)
systemctl disable systemd-resolved
systemctl stop systemd-resolved
# Oder nur Stub deaktivieren
# /etc/systemd/resolved.conf
[Resolve]
DNSStubListener=noKomplettes Beispiel
# /etc/dnsmasq.conf
# Allgemein
no-resolv
bind-interfaces
interface=eth0
# DNS
server=1.1.1.1
server=8.8.8.8
cache-size=1000
# Lokale Einträge
addn-hosts=/etc/dnsmasq.hosts
expand-hosts
domain=local
# Wildcard für Entwicklung
address=/.dev/192.168.1.100
# DHCP
dhcp-range=192.168.1.100,192.168.1.200,24h
dhcp-option=option:router,192.168.1.1
dhcp-option=option:dns-server,192.168.1.1
dhcp-option=option:domain-name,local
dhcp-leasefile=/var/lib/dnsmasq/dnsmasq.leases
# Statische Zuweisungen
dhcp-hostsfile=/etc/dnsmasq.dhcp-hosts
# Logging
log-queries
log-dhcp
log-facility=/var/log/dnsmasq.logZusammenfassung
| Funktion | Konfiguration | |----------|---------------| | Upstream DNS | server=1.1.1.1 | | Lokale Hosts | addn-hosts=file | | Wildcard | address=/.domain/IP | | DHCP-Range | dhcp-range=start,end,time | | Statisch | dhcp-host=MAC,IP,name |
| Datei | Zweck | |-------|-------| | /etc/dnsmasq.conf | Hauptkonfiguration | | /etc/dnsmasq.hosts | Lokale DNS-Einträge | | /etc/dnsmasq.dhcp-hosts | Statische DHCP | | /var/lib/dnsmasq/dnsmasq.leases | DHCP-Leases |
| Befehl | Funktion | |--------|----------| | dnsmasq --test | Konfiguration prüfen | | kill -USR1 pid | Statistiken ausgeben | | dig @localhost | DNS testen |
Fazit
Dnsmasq ist ideal für kleine bis mittlere Netzwerke. Die Kombination aus DNS und DHCP in einem Dienst vereinfacht die Konfiguration. Lokale DNS-Einträge machen Entwicklungsumgebungen komfortabler. Für große Netzwerke sind dedizierte Server wie BIND und ISC DHCP besser geeignet.