DHCP服务器工作流程:
● 当已连接到网络的客户端计算机(配置为使用 DHCP)启动时,它会发送一个 DHCPDISCOVER 消息到 DHCP 服务器。
● 当 DHCP 服务器接收到 DHCPDISCOVER 请求消息时,它会回复一个 DHCPOFFER 消息。
● 客户端收到 DHCPOFFER 消息后,它再发送给服务器一个 DHCPREQUEST 消息,表示客户端已准备好获取 DHCPOFFER 消息中提供的网络配置。
● 最后,DHCP 服务器收到客户端的 DHCPREQUEST 消息,并回复 DHCPACK 消息,表示允许客户端使用分配给它的 IP 地址
clone VMware如果出现错误,需要将被clone主机开启,然后关闭,再开启clone操作。
注意事项:VMware本身的DHCP服务器会和centos搭建的DHCP冲突。现实环境中不存在这个问题,因为实际环境中,都是实际物理主机。当起冲突的时候,客户端按先接收到的 dhcp offer来设置。
(1)在运行中(win+r),输入services.msc 或者搜索services,进入系统服务
(2)关闭VMware DHCP Service,这样VMWare DHCP服务就关闭了,就避免竞争
(3)建议暂时关闭VMware DHCP Service,否则后期如果安装dhcp的服务器主机没有启动,其他的主机都没有IP地址。
采用如下措施:
每一个虚拟主机,右键设置,将网络连接由原来NAT模式改为仅主机模式(OnlyHost)。
VMware点击编辑,点击虚拟网络编辑器,针对仅主机模式中,不勾选“使用本地DHCP服务”
前期工作:
先在NAT模式下:
(1)安装dhcp服务器:yum -y install dhcp
(2)通过克隆安装多台centos,作为客户端,来验证dhcp功能。
客户端可以不用 yum -y install dhcp
切换到OnlyHost模式下:
(3)编辑DHCP服务器配置文件,配置文件的路径: /etc/dhcp/dhcpd.conf
DHCP示例配置文件存放路径如下:
DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
配置文件编辑成功后,需要将selinux关闭,(临时关闭,setenforce 0;
永久关闭,编辑这个文件 /etc/selinux/config SELINUX=disabled)
/etc/sysconfig/selinux是/etc/selinux/config的一个软件连接 (soft link)
/etc/selinux/config才是原文件。
如果/etc/selinux/config被删除,/etc/sysconfig/selinux将失。
通过getenforce 命令可以查看linux状态
(4)systemctl restart dhcpd #重新启动dhcp服务
systemctl enable dhcpd #开机启动dhcp服务
至于客户端Linux主机,从始至终采用OnlyHost模式。
DHCP 配置文件中定义了两种类型的语句:
● 参数 - 说明如何执行任务、是否执行任务、或者给 DHCP 客户端发送什么网络配置选项。
● 声明 - 指定网络拓扑、定义客户端、提供客户端地址、或将一组参数应用于一组声明。
注意事项:
配置文件的选项后要有 “;”
配置文件中“{”与前面的字符要有空格。
配置文件中,如果出现不符合语法规则的,
执行 systemctl restart dhcpd,系统会报错,根据错误修改。
查看dhcp服务运行情况,systemctl status dhcpd -l,其中 -l 选项是列出详细信息。
在centos dhcp服务搭建完成并启动后,关闭重启vmware dhcp服务(win+r 查找特定服务)
客户端配置:ifconfig 查看网卡名称
/etc/sysconfig/network-scripts/ifcfg-网卡名称
关于DHCP更加详细用法,终端中输入: man dhcpd
dhcpd 命令选项
-t Test the configuration file. The server tests the configuration file for correct syntax, but will not attempt to perform any network operations. This can be used to test a new configuration file automatically before installing it.
-T Test the lease file. The server tests the lease file for correct syntax, but will not attempt to perform any network operations. This can be used to test a new leases file automatically before installing it.
---------------------------------------------------------
定位错误信息:
tail /var/log/messages 显示最后10行
tail -20 /var/log/messages 显示最后20行
tail -f /var/log/messages 动态显示,ctrl+c退出显示,可以单独开一个终端,动态监控
tail -f /var/log/messages | grep DHCPACK 筛查DHCPACK信息
DHCP配置文件示例:
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
default-lease-time 600;
max-lease-time 7200;
subnet 192.168.153.0 netmask 255.255.255.0 {
range 192.168.153.222 192.168.153.232;
option routers 192.168.153.1;
option broadcast-address 192.168.255.255;
host xiaozhongzi {
hardware ethernet 00:0c:29:84:2c:47;
fixed-address 192.168.153.230;
}
}
查看DHCP服务器分配的IP资源以及租期信息
[root@localhost dhcp]# cat /var/lib/dhcpd/dhcpd.leases | head -30
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.2.5
lease 192.168.153.222 {
starts 3 2018/05/02 05:25:15;
ends 3 2018/05/02 05:35:15;
tstp 3 2018/05/02 05:35:15;
cltt 3 2018/05/02 05:25:15;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:bb:68:e6;
}
lease 192.168.153.223 {
starts 3 2018/05/02 05:25:16;
ends 3 2018/05/02 05:35:16;
tstp 3 2018/05/02 05:35:16;
cltt 3 2018/05/02 05:25:16;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:0a:a5:10;
}
lease 192.168.153.224 {
starts 3 2018/05/02 05:25:16;
ends 3 2018/05/02 05:35:16;
tstp 3 2018/05/02 05:35:16;
cltt 3 2018/05/02 05:25:16;
binding state active;
next binding state free;