在 Linux 下,DHCP 与 DNS 的“集成”一般有两种常见场景,下面分别说明,并给出常用实现方式。
目标:
客户端通过 DHCP 获取 IP 后,自动把 主机名 → IP 注册到 DNS 中(正向 + 反向解析)。
# Debian / Ubuntu
apt install isc-dhcp-server bind9
# CentOS / RHEL
yum install dhcp bind bind-utils
/etc/bind/named.conf.local(或 named.conf)
zone "example.com" {
type master;
file "/var/lib/bind/db.example.com";
allow-update { key dhcp_key; };
};
zone "1.168.192.in-addr.arpa" {
type master;
file "/var/lib/bind/db.192.168.1";
allow-update { key dhcp_key; };
};
生成 TSIG key:
tsig-keygen dhcp_key > /etc/bind/dhcp_key.key
/etc/dhcp/dhcpd.conf
ddns-updates on;
ddns-update-style interim;
update-static-leases on;
key dhcp_key {
algorithm hmac-md5;
secret "xxxx==";
}
zone example.com. {
primary 127.0.0.1;
key dhcp_key;
}
zone 1.168.192.in-addr.arpa. {
primary 127.0.0.1;
key dhcp_key;
}
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200;
option domain-name "example.com";
option domain-name-servers 192.168.1.1;
}
✅ 客户端 hostname 会自动写进 DNS
DHCP + DNS 一体化,适合家庭 / 小型网络
apt install dnsmasq
/etc/dnsmasq.confdomain=example.com
dhcp-range=192.168.1.100,192.168.1.200,12h
dhcp-option=option:router,192.168.1.1
dhcp-option=option:dns-server,192.168.1.1
# 自动根据 DHCP 租约生成 DNS
addn-hosts=/etc/dnsmasq.hosts
✅ 客户端获取 IP 后,可直接用 hostname.example.com 访问
"DhcpDdns": {
"reverse-ddns": { ... },
"forward-ddns": { ... }
}
# /etc/dhcp/dhclient.conf
send host-name = gethostname();
| 方案 | 难度 | 适用场景 |
|---|---|---|
| dnsmasq | ⭐ | 家庭 / 小公司 |
| ISC DHCP + BIND | ⭐⭐⭐ | 企业 |
| Kea + DDNS | ⭐⭐⭐⭐ | 大规模 |
如果你能说明:
我可以给你一份可直接用的配置文件。