在 Debian 上通过 DHCP 服务器(通常是 isc-dhcp-server) 来“绑定主机名”,一般有两种理解:
下面分别说明。
sudo apt update
sudo apt install isc-dhcp-server
配置文件一般在:
/etc/dhcp/dhcpd.conf
host pc-office {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.1.100;
option host-name "pc-office";
}
说明:
hardware ethernet:客户端 MACfixed-address:分配的 IPoption host-name:下发给客户端的主机名(Windows 通常会用)如果有子网,要放在 subnet 内:
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.200 192.168.1.250;
option routers 192.168.1.1;
option domain-name-servers 8.8.8.8;
host server01 {
hardware ethernet aa:bb:cc:dd:ee:ff;
fixed-address 192.168.1.10;
option host-name "server01";
}
}
重启服务:
sudo systemctl restart isc-dhcp-server
⚠️ 注意:
class "by-hostname" {
match option host-name;
}
subnet 192.168.1.0 netmask 255.255.255.0 {
pool {
range 192.168.1.50 192.168.1.60;
allow members of "by-hostname";
}
}
更常见做法是:
如果你希望:
“客户端说自己叫 node1,网络里就能用 node1.local 访问”
需要:
简要结构:
ddns-update-style interim;
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200;
option domain-name "local";
option domain-name-servers 192.168.1.1;
}
并配置:
bind9rndc-keyzone 允许 DHCP 更新hostnamectl set-hostname myhost
并确保 DHCP 客户端发送 hostname(Debian 默认通常发送):
# /etc/dhcp/dhclient.conf
send host-name = gethostname();
控制面板 → 系统 → 计算机名 → 会自动通过 DHCP 发送
| 需求 | 推荐方式 |
|---|---|
| 固定 IP | MAC + fixed-address |
| 固定主机名 | MAC + option host-name |
| 自动 DNS 主机名 | DHCP + DDNS |
| 按主机名分配 IP | 不推荐 |
如果你愿意,可以告诉我:
isc-dhcp-server 还是 dnsmasq我可以直接给你一份可复制的配置文件。