DHCP服务
preparation:关闭vmware虚拟机的【使用本地DHCP服务将IP地址分配给虚拟机】
选项位置:编辑–>首选网络编辑器–>点击vmnet8网卡
Server端配置
[root@centos8 ~]#dnf install -y dhcp-server -y
注意:安装完不能直接启动,要写好配置文件再启动
安装完直接启动报错如下
没有声明子网
No subnet declaration for ens33 (10.0.0.5).
Aug 28 15:12:57 centos8 dhcpd[2901]: ** Ignoring requests on ens33. If this is not what
Aug 28 15:12:57 centos8 dhcpd[2901]: you want, please write a subnet declaration
Aug 28 15:12:57 centos8 dhcpd[2901]: in your dhcpd.conf file for the network segment
Aug 28 15:12:57 centos8 dhcpd[2901]: to which interface ens33 is attached. **
Aug 28 15:12:57 centos8 dhcpd[2901]:
Aug 28 15:12:57 centos8 dhcpd[2901]:
Aug 28 15:12:57 centos8 dhcpd[2901]: Not configured to listen on any interfaces!
配置文件
[root@centos8 ~]#vim /etc/dhcp/dhcpd.conf
option domain-name "magedu.org";
option domain-name-servers 223.5.5.5, 114.114.114.114;
default-lease-time 86400;
max-lease-time 106400;
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.190 10.0.0.200;
option routers 10.0.0.2;
}
启动
[root@centos8 ~]#systemctl start dhcpd
[root@centos8 ~]#systemctl status dhcpd
● dhcpd.service - DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2021-08-28 15:20:57 CST; 6min ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Main PID: 2931 (dhcpd)
Status: "Dispatching packets..."
Tasks: 1 (limit: 12117)
Memory: 5.3M
CGroup: /system.slice/dhcpd.service
└─2931 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid
Aug 28 15:20:57 centos8.magedu.org dhcpd[2931]: Source compiled to use binary-leases
Aug 28 15:20:57 centos8.magedu.org dhcpd[2931]: Wrote 0 class decls to leases file.
Aug 28 15:20:57 centos8.magedu.org dhcpd[2931]: Wrote 0 deleted host decls to leases file.
Aug 28 15:20:57 centos8.magedu.org dhcpd[2931]: Wrote 0 new dynamic host decls to leases file.
Aug 28 15:20:57 centos8.magedu.org dhcpd[2931]: Wrote 0 leases to leases file.
Aug 28 15:20:57 centos8.magedu.org dhcpd[2931]: Listening on LPF/ens33/00:0c:29:f6:1b:96/10.0.0.0/24
Aug 28 15:20:57 centos8.magedu.org dhcpd[2931]: Sending on LPF/ens33/00:0c:29:f6:1b:96/10.0.0.0/24
Aug 28 15:20:57 centos8.magedu.org dhcpd[2931]: Sending on Socket/fallback/fallback-net
Aug 28 15:20:57 centos8.magedu.org dhcpd[2931]: Server starting service.
Aug 28 15:20:57 centos8.magedu.org systemd[1]: Started DHCPv4 Server Daemon.
分配绑定地址
[root@centos8 ~]#vim /etc/dhcp/dhcpd.conf
host passip {
hardware ethernet 00:0c:29:ef:11:53;
fixed-address 10.0.0.20;
}
[root@centos8 ~]#systemctl restart dhcpd
Client端测试
[root@centos8 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d4:08:f4 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.190/24 brd 10.0.0.255 scope global dynamic noprefixroute eth0
valid_lft 86324sec preferred_lft 86324sec
inet6 fe80::94e4:805c:bde9:51eb/64 scope link noprefixroute
valid_lft forever preferred_lft forever
服务端查看地址分配记录
[root@centos8 ~]#tail -f /var/lib/dhcpd/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.3.6
# authoring-byte-order entry is generated, DO NOT DELETE
authoring-byte-order little-endian;
server-duid "\000\001\000\001(\274\240Y\000\014)\366\033\226";
lease 10.0.0.190 {
starts 6 2021/08/28 07:32:52;
ends 0 2021/08/29 07:32:52;
cltt 6 2021/08/28 07:32:52;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:d4:08:f4;
uid "\001\000\014)\324\010\364";
client-hostname "centos8";
}
lease 10.0.0.191 {
starts 6 2021/08/28 07:46:27;
ends 0 2021/08/29 07:46:27;
cltt 6 2021/08/28 07:46:27;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:ef:11:53;
client-hostname "centos7";
}
注意:此处显示的时间为UTC时间,不是当地时间UTC+8才是当地时间
查看端口
[root@centos8 network-scripts]# ss -ntu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp ESTAB 0 0 10.0.0.190%eth0:68 10.0.0.5:67
CentOS7中已经有地址重新获取地址并显示详细过程
[root@centos7 ~]#dhclient -d
Internet Systems Consortium DHCP Client 4.2.5
Copyright 2004-2013 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/veth82e8c76/ba:05:f3:38:4b:22
Sending on LPF/veth82e8c76/ba:05:f3:38:4b:22
Listening on LPF/docker0/02:42:a6:6b:d0:00
Sending on LPF/docker0/02:42:a6:6b:d0:00
Listening on LPF/eno16777728/00:0c:29:ef:11:53
Sending on LPF/eno16777728/00:0c:29:ef:11:53
Sending on Socket/fallback
DHCPDISCOVER on veth82e8c76 to 255.255.255.255 port 67 interval 7 (xid=0x3b2d6a9a)
DHCPDISCOVER on docker0 to 255.255.255.255 port 67 interval 5 (xid=0x60b565a3)
DHCPDISCOVER on eno16777728 to 255.255.255.255 port 67 interval 5 (xid=0x4cc0cfc1)
DHCPDISCOVER on docker0 to 255.255.255.255 port 67 interval 6 (xid=0x60b565a3)
DHCPDISCOVER on eno16777728 to 255.255.255.255 port 67 interval 14 (xid=0x4cc0cfc1)
DHCPREQUEST on eno16777728 to 255.255.255.255 port 67 (xid=0x4cc0cfc1)
DHCPOFFER from 10.0.0.5
DHCPACK from 10.0.0.5 (xid=0x4cc0cfc1)
bound to 10.0.0.192 -- renewal in 38750 seconds.
#查看IP地址
[root@centos7 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ef:11:53 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.191/24 brd 10.0.0.255 scope global noprefixroute dynamic eno16777728
valid_lft 86232sec preferred_lft 86232sec
inet 10.0.0.192/24 brd 10.0.0.255 scope global secondary dynamic eno16777728
valid_lft 86344sec preferred_lft 86344sec
inet6 fe80::20c:29ff:feef:1153/64 scope link noprefixroute
valid_lft forever preferred_lft forever
DHCP Client/Server 交互 说明:
- DHCPDISCOVER:客户端寻找发现DHCP信息
- DHCPOFFER:DHCP 服务器对客户机提供信息
- DHCPREQUEST:客户机接受 DHCP 服务器提供的信息
- DHCPACK:DHCP 服务器应答客户机并租赁 IP 地址