当前位置:首页>Linux>Linux DNS 与 DHCP 运维详解与生产实践

Linux DNS 与 DHCP 运维详解与生产实践

  • 2026-10-11 06:23:02
Linux DNS 与 DHCP 运维详解与生产实践

Linux DNS 与 DHCP 运维详解与生产实践

一、DNS 工作原理与 Linux 实现

DNS 解析流程(递归 + 迭代):

  1. 客户端查询本地 resolver(/etc/resolv.conf)。
  2. 本地缓存或转发到递归服务器。
  3. 根域名服务器 → TLD 服务器 → 权威服务器。
  4. 返回 A/AAAA/CNAME 等记录,并缓存。

Linux 常见 DNS 软件:

  • systemd-resolved:现代默认(缓存 + LLMNR)。
  • dnsmasq:轻量,适合小型环境(兼 DHCP)。
  • BIND9:功能强大,企业权威服务器首选。
  • Unbound:递归 + 缓存,安全强。
  • CoreDNS:Kubernetes 标配。

客户端配置:

# /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(交换机)。

五、生产运维优化与监控

缓存与性能:

  • dnsmasq cache-size 调大。
  • Unbound 启用 prefetch。
  • BIND:增加 max-cache-size。

日志与监控:

tail -f /var/log/syslog | grep dnsmasq
journalctl -u bind9

# Prometheus + dnsmasq_exporter / bind_exporter

高可用架构:

  • DNS:多台权威 + 递归分离 + CDN 加速。
  • DHCP:Failover 协议(ISC DHCP)或 Keepalived 主备。
  • 容器化:CoreDNS + etcd 动态配置。

常见参数调优:

# /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。
  • dhcpd -t 测试配置。

案例3:解析污染/劫持:

  • 使用 DNSSEC(BIND 支持)。
  • 配置 DNS over TLS/HTTPS(DoT/DoH):systemd-resolved 或 Unbound。

案例4:查询风暴:

  • 限流规则(nftables/iptables rate limit UDP 53)。
  • 启用 Response Rate Limiting(RRL)。

工具链:

  • dig、nslookup、host
  • tcpdump -i any port 53
  • ss -tuln | grep 53
  • drill(DNSSEC 测试)

七、安全加固最佳实践

  • 限制查询来源:allow-query { trusted; };
  • 禁用递归(权威服务器)。
  • TSIG/DDNS 密钥认证。
  • DNSSEC 防篡改。
  • 防火墙:只开放必要 UDP/TCP 53。
  • 定期更新:软件版本 + 根提示文件。
  • 监控异常:大量 NXDOMAIN 或异常源 IP。

零信任:内部使用私有 DNS + mTLS。

最新文章

随机文章