参考:linux dhcp配置多vlan ip_linux 接口vlan-CSDN博客
配置静态IP地址:
给固定的MAC地址分配指定的IP地址,固定的IP地址不必包含在指定的IP池中,如果包含在IP地址池中,固定的IP地址会从IP地址池中移除
配置方法:
subnet 192.168.2.0 netmask 255.255.255.0 {
option domain-name-servers 192.168.2.1;
option subnet-mask 255.255.255.0;
max-lease-time 2592000;
default-lease-time 2592000;
#指定分配的ip范围,可以跳过一些地址
range 192.168.2.30 192.168.2.100;
range 192.168.2.150 192.168.2.200;
#指定网关
option routers 192.168.2.1;
}
host pc1 {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.2.222;
}
也可以将固定IP的配置section放置在subnet区域
subnet 192.168.2.0 netmask 255.255.255.0 {
option domain-name-servers 192.168.2.1;
option subnet-mask 255.255.255.0;
max-lease-time 2592000;
default-lease-time 2592000;
#指定分配的ip范围,可以跳过一些地址
range 192.168.2.30 192.168.2.100;
range 192.168.2.150 192.168.2.200;
#指定网关
option routers 192.168.2.1;
host pc1 {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.2.222;
}
}
当存在多个host需要固定IP的时候,可以使用group进行分组,也可以不分组,分组的形式如下:
subnet 192.168.2.0 netmask 255.255.255.0 {
option domain-name-servers 192.168.2.1;
option subnet-mask 255.255.255.0;
max-lease-time 2592000;
default-lease-time 2592000;
#指定分配的ip范围,可以跳过一些地址
range 192.168.2.30 192.168.2.100;
range 192.168.2.150 192.168.2.200;
#指定网关
option routers 192.168.2.1;
group {
host pc1 {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.2.66;
}
host pc2 {
hardware ethernet 66:77:88:99:AA:BB;
fixed-address 192.168.2.69;
}
}
}
还可以将group配置在subnet section 之外
subnet 192.168.2.0 netmask 255.255.255.0 {
option domain-name-servers 192.168.2.1;
option subnet-mask 255.255.255.0;
max-lease-time 2592000;
default-lease-time 2592000;
#指定分配的ip范围,可以跳过一些地址
range 192.168.2.30 192.168.2.100;
range 192.168.2.150 192.168.2.200;
#指定网关
option routers 192.168.2.1;
}
group {
host pc1 {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.2.66;
}
host pc2 {
hardware ethernet 66:77:88:99:AA:BB;
fixed-address 192.168.2.69;
}
}
以上四种配置,都已验证过,都可以正常获取固定IP
# dhcp 服务器查看哪个MAC地址已使用IP
less /var/lib/dhcpd/dhcpd.leases
# 查看dhcp服务器已分配的IP地址
cat /var/lib/dhcpd/dhcpd.leases|grep -o '\<[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\>'
# # 查看dhcp服务器已分配的IP地址对应的MAC
cat /var/lib/dhcpd/dhcpd.leases|grep -o '\<[0-9a-f]\{2\}:[0-9a-f]\{2\}:[0-9a-f]\{2\}:[0-9a-f]\{2\}:[0-9a-f]\{2\}:[0-9a-f]\{2\}\>'