DHCP overview and principle
For DHCP (Dynamic Host Configuration Protocol) details, please refer to the previous blog
Configuration command steps in CentOS7 environment
Configure DHCP server
yum install -y dhcp #安装DHCP
cd /etc/dhcp/
ls
less dhcpd.conf #查看配置文件,发现模板路径
cd /usr/share/doc/dhcp-4.2.5/
ls
less dhcpd.conf.example #查看模板
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf #复制模板并编辑
Here I did not configure the local yum source, so I used the RPM installation
Set global configuration parameters
vim /etc/dhcp/dhcpd.conf
default-lease-time 21600; #默认租约为 6 小时,单位为秒
max-lease-time 43200; #最大租约为 12 小时,单位为秒
option domain-name "benet.com"; #指定默认域名
option domain-name-servers 202.106.0.20, 202.106.148.1; #指定 DNS 服务器地址
ddns-update-style none; #禁用 DNS 动态更新
#subnet网段声明(作用于整个子网段,部分配置参数优先级高于全局配置参数)
subnet 192.168.100.0 netmask 255.255.255.0 { #声明要分配的网段地址
range 192.168.100.10 192.168.100.20; #设置地址池
option routers 192.168.100.254; #指定默认网关地址
}
subnet 192.168.10.0 netmask 255.255.255.0 {
range 192.168.10.100 192.168.1.200;
option routers 192.168.10.254;
}
subnet 192.168.20.0 netmask 255.255.255.0 {
ran
ge 192.168.20.220 192.168.20.230;
option routers 192.168.20.254;
}
#host主机声明(给单机分配固定的 IP 地址)
host hostname { #指定需要分配固定 IP地址的客户机名称
hardware ethernet 00:c0:c3:22:46:81; #指定该主机的 MAC地址
fixed-address 192.168.4.100; #指定保留给该主机的 IP地址
}
#后面内容可都删除
systemctl start dhcpd #开启DHCP服务
systemctl stop firewalld #关闭防火墙
setenforce 0 #关闭防火墙
netstat -anpu | grep ":67" #查询服务端口是否启动
If the DHCP service fails to start, you can view the log file
tail -f /var/log/messages
Virtual machine configuration information
Change the network card address
Change the network link mode
Use the local DHCP service to assign the IP address to the virtual machine
Change the IPV4 attribute of the adapter locally to use the following address
Configure DHCP relay in ensp
The commands here are not listed in detail, but a screenshot
is given. Method two
Linux客户端使用 DHCP 动态获取IP
#方法一:
vi /etc/sysconfig/network-scripts/ifcfg-ens33
DEVICE=ens33
ONBOOT=yes
BOOTPROTO=dhcp
ifdown ens33 ; ifup ens33
#方法二:
dhclient -d ens33
查看租约文件
less /var/lib/dhcpd/dhcpd.lease