1.安装DHCP
[root@localhost ~]# yum -y install dhcp
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.dgut.edu.cn
* extras: mirrors.dgut.edu.cn
* updates: mirrors.dgut.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package dhcp.x86_64 12:4.2.5-83.el7.centos.1 will be installed
2.查看配置文件
[root@localhost ~]# rpm -ql dhcp
/etc/NetworkManager
/etc/NetworkManager/dispatcher.d
/etc/NetworkManager/dispatcher.d/12-dhcpd
/etc/dhcp/dhcpd.conf ##配置文件
/etc/dhcp/dhcpd6.conf
/etc/dhcp/scripts
/etc/dhcp/scripts/README.scripts
/etc/openldap/schema/dhcp.schema
/etc/sysconfig/dhcpd
/usr/bin/omshell
/usr/lib/systemd/system/dhcpd.service
/usr/lib/systemd/system/dhcpd6.service
/usr/lib/systemd/system/dhcrelay.service
/usr/sbin/dhcpd
/usr/sbin/dhcrelay ##中继服务
/usr/share/doc/dhcp-4.2.5
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example ##模板文件
/usr/share/doc/dhcp-4.2.5/dhcpd6.conf.example
/usr/share/doc/dhcp-4.2.5/ldap
/usr/share/doc/dhcp-4.2.5/ldap/README.ldap
/usr/share/doc/dhcp-4.2.5/ldap/dhcp.schema
/usr/share/doc/dhcp-4.2.5/ldap/dhcpd-conf-to-ldap
/usr/share/man/man1/omshell.1.gz
/usr/share/man/man5/dhcpd.conf.5.gz
/usr/share/man/man5/dhcpd.leases.5.gz
/usr/share/man/man8/dhcpd.8.gz
/usr/share/man/man8/dhcrelay.8.gz
/usr/share/systemtap/tapset/dhcpd.stp
/var/lib/dhcpd
/var/lib/dhcpd/dhcpd.leases
/var/lib/dhcpd/dhcpd6.leases
3.DHCP配置文件
打开这个配置文件/etc/dhcp/dhcpd.conf
,发现提示寻找模板配置文件:
/usr/share/doc/dhcp*/dhcpd.conf.example
现在我们将范本配置文件复制到/etc 目录下替换掉空白dhcpd.conf 主配置文件
[root@localhost ~]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y
4.启动DHCP服务
[root@localhost ~]# systemctl start dhcpd
Job for dhcpd.service failed because the control process exited with error code. See "systemctl status dhcpd.service" and "journalctl -xe" for details.
注意:启动失败的原因是 dhcp服务器配置的IP地址和默认配置文件里定义的地址段不相同。
5.DHCP的配置文件解析
5.1DHCP默认的配置文件
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "example.org"; #域名
option domain-name-servers ns1.example.org, ns2.example.org; #域名服务器
default-lease-time 600; #默认的租约时间
max-lease-time 7200; #最大租约时间
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative; #如果是权威DHCP,建议打开
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7; #日志级别
# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.
#作用域配置
subnet 10.152.187.0 netmask 255.255.255.0 {
}
# This is a very basic subnet declaration.
#作用域配置
subnet 10.254.239.0 netmask 255.255.255.224 {
range 10.254.239.10 10.254.239.20;
option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
}
# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.
#作用域配置
subnet 10.254.239.32 netmask 255.255.255.224 {
range dynamic-bootp 10.254.239.40 10.254.239.60;
option broadcast-address 10.254.239.31;
option routers rtr-239-32-1.example.org;
}
# A slightly different configuration for an internal subnet.
#作用域配置
subnet 10.5.5.0 netmask 255.255.255.224 {
range 10.5.5.26 10.5.5.30;
option domain-name-servers ns1.internal.example.org;
option domain-name "internal.example.org";
option routers 10.5.5.1;
option broadcast-address 10.5.5.31;
default-lease-time 600;
max-lease-time 7200;
}
# Hosts which require special configuration options can be listed in
# host statements. If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.
host passacaglia {
hardware ethernet 0:0:c0:5d:bd:95;
filename "vmunix.passacaglia";
server-name "toccata.fugue.com";
}
# Fixed IP addresses can also be specified for hosts. These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP. Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
host fantasia {
hardware ethernet 08:00:07:26:c0:a5;
fixed-address fantasia.fugue.com;
}
# You can declare a class of clients and then do address allocation
# based on that. The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.
class "foo" {
match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}
shared-network 224-29 {
subnet 10.17.224.0 netmask 255.255.255.0 {
option routers rtr-224.example.org;
}
subnet 10.0.29.0 netmask 255.255.255.0 {
option routers rtr-29.example.org;
}
pool {
allow members of "foo";
range 10.17.224.10 10.17.224.250;
}
pool {
deny members of "foo";
range 10.0.29.10 10.0.29.230;
}
}
说明
dhcp范本配置文件内容包含了部分参数、声明以及选项的用法,其中注释部分可以放在任何位置,并以“#”号开头,当一行内容结束时,以“;”号结束,大括号所在行除外,可以看出整个配置文件分成全局和局部两个部分。但是并不容易看出哪些属于参数,哪些属于声明和选项。
5.2DHCP全局配置
# option definitions common to all supported networks...
option domain-name "example.org"; #为客户端指定所属的域名
option domain-name-servers ns1.example.org, ns2.example.org; #为客户端指定DNS服务器地址
default-lease-time 600; #定义最小租约期限,以秒为单位的租约时间
max-lease-time 7200; #定义最大租约时间,以秒为单位的租约时间,当客户端超过租约时间,却尚未更新IP 时,最长可以使用该IP 的时间;
比如,机器在开机获得IP地址后,然后关机了。这时,当时间过了default-lease-time 600秒后,没有机器向DHCP续约,DHCP会保留7200秒,保留此IP地址不用于分配给其它机器。 当超过7200秒后,将不再保留此IP地址给此机器。
log-facility local7; #定义日志类型为 local7
5.3DHCP局部配置
subnet:声明一般用来指定IP 作用域、定义为客户端分配的IP 地址池等等
# A slightly different configuration for an internal subnet.
subnet 10.5.5.0 netmask 255.255.255.224 { #指定IP分配范围与子网掩码
range 10.5.5.26 10.5.5.30; #指定分配地址池
option domain-name-servers ns1.internal.example.org; #为客户端指定DNS服务器地址
option domain-name "internal.example.org"; #DNS名称(有就填域名,没有可默认)
option routers 10.5.5.1; #指定默认网关
option broadcast-address 10.5.5.31; #指定默认广播地址
default-lease-time 600; #定义最小租约期限,以秒为单位的租约时间
max-lease-time 7200; #定义最大租约时间,以秒为单位的租约时间,当客户端超过租约时间,却尚未更新IP 时,最长可以使用该IP 的时间;
}
注意:如果全局配置和局部配置冲突,以局部配置为准
host fantasia { ##配置保留地址
hardware ethernet 08:00:07:26:c0:a5; ##配置保留IP地址的MAC
fixed-address fantasia.fugue.com; #配置IP地址
}
例如:
host fantasia {
hardware ethernet 08:00:07:26:c0:a5; ##配置保留IP地址的MAC
fixed-address 192.168.1.100; #配置IP地址
}
5.4 再次启动
启动之前要定义一个和当前DHCP服务器在同一个网络端的子网段,否则无法启动。
subnet 192.168.1.0 netmask 255.255.255.0 { #定义一个本网段的作用域,注意地址范围必须是和DHCP当前IP地址在同一个范围
range 192.168.1.200 192.168.1.230;
}
[root@localhost dhcp]# systemctl start dhcpd #启动没有问题。
[root@localhost dhcp]# systemctl status dhcpd #查看DHCP状态。
● dhcpd.service - DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
Active: active (running) since 一 2023-04-24 09:11:01 CST; 9s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Main PID: 54141 (dhcpd)
Status: "Dispatching packets..."
Tasks: 1
CGroup: /system.slice/dhcpd.service
└─54141 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid
4月 24 09:11:01 localhost.localdomain dhcpd[54141]: All rights reserved.
4月 24 09:11:01 localhost.localdomain dhcpd[54141]: For info, please visit https://www.isc.org/software/dhcp/
4月 24 09:11:01 localhost.localdomain dhcpd[54141]: Wrote 0 class decls to leases file.
4月 24 09:11:01 localhost.localdomain dhcpd[54141]: Wrote 0 deleted host decls to leases file.
4月 24 09:11:01 localhost.localdomain dhcpd[54141]: Wrote 0 new dynamic host decls to leases file.
4月 24 09:11:01 localhost.localdomain dhcpd[54141]: Wrote 0 leases to leases file.
4月 24 09:11:01 localhost.localdomain dhcpd[54141]: Listening on LPF/ens33/00:0c:29:7a:14:f2/192.168.1.0/24
4月 24 09:11:01 localhost.localdomain dhcpd[54141]: Sending on LPF/ens33/00:0c:29:7a:14:f2/192.168.1.0/24
4月 24 09:11:01 localhost.localdomain dhcpd[54141]: Sending on Socket/fallback/
5.5DHCP服务端口
[root@localhost dhcp]# netstat -antup |grep dhcp
udp 0 0 0.0.0.0:67 0.0.0.0:* 54141/dhcpd
注意:DHCP客户端使用的端口68,服务端使用端口67
6 测试DHCP
注意:将vmware虚拟机本身的DHCP的分配功能给关闭掉,然后将网络切换到vmnet4,客户端的网络也要切换到vmnet4.
6.1 设置DHCP客户端
打开/etc/sysconfig/network-scripts/ifcfg-ens33
6.2 测试
客户端执行dhclient -d 观察dhcp的地址分配过程,发现服务器给分配了一个新的IP地址:192.168.1.200,正好是属于我们的地址范围内的
再次执行这个命令,发现请求的过程就只有DHCP REQUEST的过程,这就相当于IP地址续租
重新观察客户端的IP地址的分配过程
发现,客户端连续几次向服务器发起IP续租,但是最后没有给回应,最终客户端重新发起了DHCP DISCOVER来获取IP,新的IP地址是192.168.1.240;
此刻,DHCP是可以正常工作了,但是我们发现还客户端没有配置网关,因此我们需要给配置一个网关;
编辑DHCP的配置文件,并重启DHCP服务器
[root@localhost dhcp]# systemctl restart dhcpd #重启dhcp
7.保留IP
7.1定义保留IP
注意:保留地址不能是子网中的地址范围
编辑配置文件,打开/etc/dhcp/dhcpd.conf
[root@localhost dhcp]# systemctl restart dhcpd #重启dhcp
客户端重新获取IP地址
8.租约数据库文件
租约数据库文件用于保存一系列的租约声明,其中包含客户端的主机名、MAC 地址、分配到的IP地址,以及IP地址的有效期等相关信息。这个数据库文件是可编辑的ASCII 格式文本文件。每当发生租约变化的时候,都会在文件结尾添加新的租约记录。DHCP刚安装好后租约数据库文件dhcpd.leases 是个空文件,当DHCP 服务正常运行后就可以使用cat 命令查看租约数据库文件内容了
打开/var/lib/dhcpd/dhcpd.leases