温馨提示×

linux dhcp怎样集成dns

小樊
38
2026-10-12 13:56:31
栏目: 智能运维

在 Linux 下,DHCP 与 DNS 的“集成”一般有两种常见场景,下面分别说明,并给出常用实现方式。


一、DHCP 动态更新 DNS(最典型的“集成”)

目标:
客户端通过 DHCP 获取 IP 后,自动把 主机名 → IP 注册到 DNS 中(正向 + 反向解析)。

常见组合

  • ISC DHCP Server + BIND DNS
  • dnsmasq(DHCP + DNS 一体)
  • Kea DHCP + BIND / PowerDNS

二、方案 1:ISC DHCP + BIND(企业常用)

1. 安装

# Debian / Ubuntu
apt install isc-dhcp-server bind9

# CentOS / RHEL
yum install dhcp bind bind-utils

2. 配置 BIND 支持动态更新

/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

3. DHCP 配置动态更新

/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


三、方案 2:dnsmasq(最简单)

DHCP + DNS 一体化,适合家庭 / 小型网络

安装

apt install dnsmasq

配置 /etc/dnsmasq.conf

domain=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 访问


四、方案 3:Kea DHCP(现代方案)

  • Kea + DDNS 模块
  • 支持 BIND / PowerDNS
  • 适合大规模部署
"DhcpDdns": {
  "reverse-ddns": { ... },
  "forward-ddns": { ... }
}

五、客户端注意事项

  • Windows:默认会发 hostname ✅
  • Linux 客户端需配置:
# /etc/dhcp/dhclient.conf
send host-name = gethostname();

六、总结对比

方案 难度 适用场景
dnsmasq ⭐ 家庭 / 小公司
ISC DHCP + BIND ⭐⭐⭐ 企业
Kea + DDNS ⭐⭐⭐⭐ 大规模

如果你能说明:

  • 用的 Linux 发行版
  • 是 家庭 / 企业
  • 是否要 自动 DNS 注册

我可以给你一份可直接用的配置文件。

0 踩