1. 下载DHCP
yum -y install dhcp
2. 配置DHCP主服务器
下载完dhcp服务以后
步骤一:cd /etc/sysconfig/network-scripts/
vi ifcfg-eth0
- 需要将BOOTPROTO由DHCP自动分配地址,改为none。
- IPADDR填写DHCP服务器的地址。
- 填写默认网关和子网掩码。
如下图:
步骤二:1.cd /etc/dhcp
2.vi dhcpd.conf
3.修改的dhcpd.conf内容
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
# see ‘man 5 dhcpd.conf‘
#
option domain-name "archermind.com";
option domain-name-servers 192.168.100.101;
filename "pxelinux.0";
next-server 172.21.80.30;
default-lease-time 600;
max-lease-time 7200;
authoritative;
subnet 172.21.70.0 netmask 255.255.255.0{
range dynamic-bootp 172.21.70.18 172.21.70.130;
option broadcast-address 172.21.70.255;
option routers 172.21.70.1;
}
host test1
{
hardware ethernet E8:39:35:43:F9:C8;
fixed-address 172.21.70.131;
}
规定分配的动态IP的范围,最底下可以静态固定分配IP给指定的MAC地址。
整体过程如下图所示:
步骤三:配置全部完成后
service dhcpd restart
3. 验证动态获取IP和静态获取
寻找另外一台设备连接上DHCP服务器,即从服务器,动态获取和静态获取都在新的设备上输入命令:/etc/init.d/network restart
如下图:
动态分配的结果:
静态分配的结果:在DHCP服务器上之前dhcpd.conf中给固定mac
E8:39:35:43:F9:C8配置的结果如下图:
确认和dhcpd.conf中配置的没有区别。
host test1
{
hardware ethernet E8:39:35:43:F9:C8;
fixed-address 172.21.70.131;
}
4. 查看已经分配出去的动态和静态IP
查看分配情况:cat /var/lib/dhcpd/dhcpd.leases
查看分出去的IP地址: cat /var/lib/dhcpd/dhcpd.leases | grep ^l | sort -u | sed ‘s/{//g‘
查看分出去的MAC地址:cat /var/lib/dhcpd/dhcpd.leases | grep hard | sort -u
如下图: