Linux DNS 与 DHCP 运维详解与生产实践
一、DNS 工作原理与 Linux 实现
DNS 解析流程(递归 + 迭代):
- 客户端查询本地 resolver(/etc/resolv.conf)。
- 根域名服务器 → TLD 服务器 → 权威服务器。
Linux 常见 DNS 软件:
- systemd-resolved:现代默认(缓存 + LLMNR)。
- dnsmasq:轻量,适合小型环境(兼 DHCP)。
客户端配置:
# /etc/resolv.conf(建议通过 NetworkManager 或 resolved 管理)
nameserver 8.8.8.8
nameserver 114.114.114.114
search example.com
# systemd-resolved
systemctl status systemd-resolved
resolvectl status
二、dnsmasq 快速部署(轻量运维推荐)
dnsmasq 集成 DNS + DHCP + TFTP,适合分支机构或测试环境。
安装与基础配置(/etc/dnsmasq.conf):
apt install dnsmasq # 或 yum/dnf
# 配置
port=53
domain-needed
bogus-priv
no-resolv
server=8.8.8.8
server=114.114.114.114
cache-size=10000
local=/internal.example.com/
# 本地记录
address=/test.internal/192.168.10.100
cname=www.internal,app.internal
启动与测试:
systemctl enable --now dnsmasq
dig @127.0.0.1 test.internal
nslookup www.baidu.com 127.0.0.1
结合防火墙(第三篇):
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
三、BIND9 权威 DNS 生产配置
安装:
apt install bind9
主配置文件(/etc/bind/named.conf):
options {
directory "/var/cache/bind";
recursion no; # 权威服务器关闭递归
allow-query { any; }; # 生产中限制来源
listen-on { 192.168.10.50; };
};
zone "example.com" {
type master;
file "/etc/bind/db.example.com";
};
区域文件(db.example.com):
$TTL 3600
@ IN SOA ns1.example.com. admin.example.com. (
2024062801 ; Serial
3600 ; Refresh
1800 ; Retry
604800 ; Expire
3600 ) ; Minimum
@ IN NS ns1.example.com.
ns1 IN A 192.168.10.50
www IN A 192.168.10.100
检查与重载:
named-checkconf
named-checkzone example.com /etc/bind/db.example.com
rndc reload
rndc status
从服务器(Slave):
zone "example.com" {
type slave;
masters { 192.168.10.50; };
file "db.example.com.slave";
};
高可用:主从同步 + Keepalived/VIP 或 Anycast。
四、DHCP 工作原理与配置
DHCP 四步:Discover → Offer → Request → ACK。
dnsmasq DHCP 配置(简单):
dhcp-range=192.168.10.100,192.168.10.200,12h
dhcp-option=3,192.168.10.1 # 网关
dhcp-option=6,8.8.8.8,114.114.114.114 # DNS
dhcp-host=00:11:22:33:44:55,192.168.10.150,static-host
ISC DHCP Server(复杂环境):
apt install isc-dhcp-server
# /etc/dhcp/dhcpd.conf
subnet 192.168.10.0 netmask 255.255.255.0 {
range 192.168.10.100 192.168.10.200;
option routers 192.168.10.1;
option domain-name-servers 192.168.10.50;
default-lease-time 86400;
}
host fixed {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.10.150;
}
Relay Agent(跨网段): dhcp-helper 或 ip helper-address(交换机)。
五、生产运维优化与监控
缓存与性能:
日志与监控:
tail -f /var/log/syslog | grep dnsmasq
journalctl -u bind9
# Prometheus + dnsmasq_exporter / bind_exporter
高可用架构:
- DNS:多台权威 + 递归分离 + CDN 加速。
- DHCP:Failover 协议(ISC DHCP)或 Keepalived 主备。
常见参数调优:
# /etc/sysctl.conf
net.core.somaxconn=4096 # DNS 查询高峰
六、故障排查实战案例
案例1:解析失败:
dig +trace www.example.com 分步追踪。- 检查 /etc/resolv.conf 是否被覆盖。
systemd-resolve --statistics
案例2:DHCP 地址冲突:
- 检查 MAC 绑定、租约文件
/var/lib/dhcp/dhcpd.leases。
案例3:解析污染/劫持:
- 配置 DNS over TLS/HTTPS(DoT/DoH):systemd-resolved 或 Unbound。
案例4:查询风暴:
- 限流规则(nftables/iptables rate limit UDP 53)。
- 启用 Response Rate Limiting(RRL)。
工具链:
七、安全加固最佳实践
- 限制查询来源:
allow-query { trusted; }; - 监控异常:大量 NXDOMAIN 或异常源 IP。
零信任:内部使用私有 DNS + mTLS。