在 Debian 系统中,通过 DHCP 固定 IP(静态 IP 分配) 通常有两种常见场景,下面分别说明:
适用于:你管理路由器或 DHCP 服务器(如 ISC DHCP)
原理:根据客户端的 MAC 地址 分配固定 IP。
在 Debian 客户端上执行:
ip link
或
ip addr
找到网卡(如 eth0、ens33)的 link/ether 地址,例如:
00:16:3e:12:34:56
编辑配置文件:
sudo nano /etc/dhcp/dhcpd.conf
添加如下内容:
host debian-client {
hardware ethernet 00:16:3e:12:34:56;
fixed-address 192.168.1.100;
}
确保该 IP 不在 DHCP 动态分配范围内。
重启 DHCP 服务:
sudo systemctl restart isc-dhcp-server
sudo dhclient -r
sudo dhclient
适用于:你无法控制 DHCP 服务器,但希望系统行为像固定 IP
查看连接名:
nmcli connection show
设置固定 IP(通过 DHCP 分配):
nmcli connection modify "Wired connection 1" \
ipv4.method auto \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8"
⚠️ 注意:
这种方式 不是真正的 DHCP 保留,只是客户端请求指定 IP,不能保证一定成功。
/etc/network/interfaces(传统方式)编辑:
sudo nano /etc/network/interfaces
示例:
auto eth0
iface eth0 inet dhcp
hwaddress ether 00:16:3e:12:34:56
⚠️ 仍然依赖 DHCP 服务器是否支持固定分配。
✅ 不在 DHCP 层次固定,而是直接使用静态 IP
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8
然后重启网络:
sudo systemctl restart networking
| 方法 | 是否真正固定 | 推荐度 |
|---|---|---|
| DHCP 服务器 MAC 绑定 | ✅ 是 | ⭐⭐⭐⭐⭐ |
| NetworkManager 指定 IP | ❌ 否 | ⭐⭐ |
| 静态 IP | ✅ 是 | ⭐⭐⭐⭐⭐ |
如果你愿意,可以告诉我:
我可以给你一份完全适配你环境的配置示例。