温馨提示×

Linux 如何设置 DHCP 保留地址

小樊
40
2026-08-31 20:03:58
栏目: 智能运维

在 Linux 上“设置 DHCP 保留地址”通常指 在 DHCP 服务器上把某个 MAC 地址固定分配某个 IP。常见情况如下:


一、使用 dnsmasq(最常见,轻量)

1. 安装

# Debian / Ubuntu
apt install dnsmasq

# CentOS / Rocky / Alma
yum install dnsmasq

2. 配置保留地址

编辑配置文件(如 /etc/dnsmasq.conf/etc/dnsmasq.d/resv.conf):

dhcp-host=00:11:22:33:44:55,192.168.1.100
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.1.101,hostname-pc

参数说明:

  • MAC 地址
  • 保留 IP
  • (可选)主机名

3. 重启服务

systemctl restart dnsmasq

二、使用 ISC DHCP Server(isc-dhcp-server)

1. 安装

apt install isc-dhcp-server

2. 修改配置文件

编辑 /etc/dhcp/dhcpd.conf

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 00:11:22:33:44:55;
  fixed-address 192.168.1.100;
}

3. 重启服务

systemctl restart isc-dhcp-server

三、使用 NetworkManager 自带的 dnsmasq(桌面/服务器常见)

vim /etc/NetworkManager/dnsmasq.d/resv.conf
dhcp-host=00:11:22:33:44:55,192.168.1.100

重启:

systemctl restart NetworkManager

四、在客户端“固定 IP”(非 DHCP 保留)

如果你只是想 不让客户端走 DHCP,可以改网卡配置:

# Ubuntu (netplan)
vim /etc/netplan/01-netcfg.yaml
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1

五、注意事项

  • 保留 IP 不能落在 DHCP 动态分配范围内
  • MAC 地址格式要正确(: 分隔)
  • 改完配置后客户端可先释放再获取:
    dhclient -r
    dhclient
    

如果你能告诉我:

  • 用的是哪种 Linux(Ubuntu / CentOS / 树莓派)
  • DHCP 服务类型(dnsmasq / isc-dhcp / 路由器)

我可以给你更精确的配置示例。

0