温馨提示×

linux dhcp怎样绑定ip

小樊
36
2026-09-30 21:02:51
栏目: 智能运维

在 Linux 下“DHCP 绑定 IP(固定分配 IP)”通常是指 在 DHCP 服务器上把某个 MAC 地址和固定 IP 绑定。下面分常见场景说明。


一、使用 isc-dhcp-server(最常见)

1. 安装

# Debian / Ubuntu
apt install isc-dhcp-server

# CentOS / RHEL
yum install dhcp

2. 配置文件

通常是:

  • Debian/Ubuntu:/etc/dhcp/dhcpd.conf
  • CentOS/RHEL:/etc/dhcp/dhcpd.conf

3. 配置子网(示例)

subnet 192.168.1.0 netmask 255.255.255.0 {
  range 192.168.1.100 192.168.1.200;
  option routers 192.168.1.1;
  option domain-name-servers 8.8.8.8;
}

4. 绑定 MAC 和 IP(关键)

host pc01 {
  hardware ethernet 00:11:22:33:44:55;
  fixed-address 192.168.1.50;
}

5. 重启服务

# Debian / Ubuntu
systemctl restart isc-dhcp-server

# CentOS / RHEL
systemctl restart dhcpd

✅ 此后该 MAC 永远拿到 192.168.1.50


二、使用 dnsmasq(轻量 DHCP)

1. 安装

apt install dnsmasq

2. 配置(/etc/dnsmasq.conf)

dhcp-range=192.168.1.100,192.168.1.200,12h

dhcp-host=00:11:22:33:44:55,192.168.1.50

3. 重启

systemctl restart dnsmasq

三、如果是“客户端想固定 IP”

⚠️ 这不是 DHCP 绑定,而是客户端不使用 DHCP

Linux 客户端静态 IP(示例)

nmcli con mod eth0 ipv4.method manual \
ipv4.addresses 192.168.1.50/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns 8.8.8.8

或修改:

/etc/network/interfaces

或

/etc/netplan/*.yaml

四、常见注意事项

  • ✅ fixed-address 不能在 range 内(建议分开)
  • ✅ MAC 格式必须是 : 分隔
  • ✅ 客户端需重新获取 IP(重启网络或 dhclient -r)

如果你能告诉我:

  • 用的是 哪个 Linux 发行版
  • 用的是 isc-dhcp / dnsmasq / 路由器
  • 是 服务器绑定 还是 客户端固定

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

0 踩