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 dnsmasq

DNS-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.log

Lokale 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 database

CNAME-Records

# CNAME-Einträge
cname=www.local,server1.local
cname=mail.local,server1.local

Reverse-DNS

# PTR-Records automatisch aus /etc/hosts
expand-hosts
domain=local

# Oder manuell
ptr-record=10.1.168.192.in-addr.arpa,server1.local

DHCP-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.leases

Statische 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,server2

DHCP-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,24h

Mehrere 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.1

PXE-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.efi

DNS-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.com

Pi-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 dnsmasq

DNS-over-HTTPS/TLS

Mit dnscrypt-proxy

# Als Upstream für dnsmasq
server=127.0.0.1#5353

Mit Cloudflared

# cloudflared als DoH-Proxy
# dnsmasq nutzt localhost
server=127.0.0.1#5053

Monitoring

Statistiken

# SIGUSR1 für Statistiken
kill -USR1 $(pidof dnsmasq)

# In /var/log/syslog:
# queries forwarded 1234, queries answered locally 567

Lease-Datei

cat /var/lib/dnsmasq/dnsmasq.leases

# Format:
# EXPIRE MAC IP HOSTNAME CLIENT-ID

DNS-Abfrage-Log

# In Config
log-queries

# Beobachten
tail -f /var/log/dnsmasq.log

Sicherheit

Zugriff beschränken

# Nur lokale Anfragen
local-service

# Bestimmte Interfaces
interface=eth0
bind-interfaces

# Keine Forwarding
localise-queries

DNSSEC

# DNSSEC-Validierung
dnssec
trust-anchor=.,19036,8,2,49AAC11D7B6F6446702E54A1607371607A1A41855200FD2CE1CDDE32F24E8FB5

# Oder Upstream vertrauen
proxy-dnssec

Entwicklungsumgebung

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.101

Docker-Integration

# Docker-Container über DNS
# Container haben Namen wie container.docker.local
server=/docker.local/172.17.0.1

Konfiguration testen

# Syntax prüfen
dnsmasq --test

# Mit eigener Config
dnsmasq --test --conf-file=/etc/dnsmasq.conf

# Debug-Modus (Vordergrund)
dnsmasq --no-daemon --log-queries

Troubleshooting

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.dnsmasq

DHCP-Test

# DHCP-Leases anzeigen
cat /var/lib/dnsmasq/dnsmasq.leases

# DHCP-Log
journalctl -u dnsmasq | grep DHCP

Port-Konflikte

# systemd-resolved deaktivieren (Ubuntu)
systemctl disable systemd-resolved
systemctl stop systemd-resolved

# Oder nur Stub deaktivieren
# /etc/systemd/resolved.conf
[Resolve]
DNSStubListener=no

Komplettes 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.log

Zusammenfassung

| 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.