在 Linux 上配置 DHCP 服务器(通常是 ISC DHCP Server,即 dhcpd)来设置网关(默认路由),关键是在配置文件里使用 option routers。
下面分场景说明。
# Debian / Ubuntu
apt install isc-dhcp-server
# CentOS / RHEL
yum install dhcp
编辑配置文件:
/etc/dhcp/dhcpd.conf
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 subnet-mask 255.255.255.0;
option domain-name-servers 8.8.8.8, 8.8.4.4;
default-lease-time 600;
max-lease-time 7200;
}
✅ 关键参数
option routers → 客户端拿到的默认网关option routers 192.168.1.1, 192.168.1.2;
systemctl restart isc-dhcp-server
# 或
systemctl restart dhcpd
host pc1 {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.1.50;
option routers 192.168.1.1;
}
如果你用的是 dnsmasq:
# /etc/dnsmasq.conf
dhcp-range=192.168.1.100,192.168.1.200,255.255.255.0,12h
dhcp-option=3,192.168.1.1 # 3 = 默认网关
或:
dhcp-option=option:router,192.168.1.1
重启:
systemctl restart dnsmasq
ip route
应看到:
default via 192.168.1.1 dev eth0
ipconfig /all
查看“默认网关”
❌ 网关不在 subnet 范围内
❌ 忘了 option routers
❌ 防火墙阻止 DHCP(UDP 67/68)
❌ 多 DHCP 服务器冲突
如果你能告诉我:
我可以给你更精确的配置示例。