掘金 后端 ( ) • 2024-04-28 17:27

什么是DHCP

定义

DHCP:动态主机配置协议,用于自动获取IP地址

工作原理

微信截图_20240428153521.png

  1. 客户端会发送一个广播DHCP Discover报文去寻找DHCP服务器
  2. 客户端只会接受第一个回复的DHCP服务器报文
  3. 服务端会发送一个DHCP Offer报文,报文中携带相关配置信息
  4. 报文客户端收到这个offer报文后自动配置相关信息,并且回复一个DHCP Request报文
  5. 服务器收到request报文会将分配的地址从地址池抹去,并且回复DHCP Ack报文

报文类型

报文类型 含义 Discover 客户端用来寻找DHCP服务器 Offer 用来响应discover报文 Requset 客户端请求确认配置,或者续租 ACK 对request报文的确认响应 Nak 对request报文的拒绝响应 Release 客户端要释放地址时用来通知服务器

Linux中配置DHCP

安装DHCP服务

[root@localhost ~]# yum -y install dhcp

查找dhcp配置文件位置

[root@localhost ~]# rpm -qc dhcp
/etc/dhcp/dhcpd.conf                #该文件为dhcp配置文件
/etc/dhcp/dhcpd6.conf
/etc/openldap/schema/dhcp.schema
/etc/sysconfig/dhcpd
/var/lib/dhcpd/dhcpd.leases
/var/lib/dhcpd/dhcpd6.leases
​

DHCP示例文件位置

[root@localhost ~]# vim /etc/dhcp/dhcpd.conf 
​
  1 #
  2 # DHCP Server Configuration file.
  3 #   see /usr/share/doc/dhcp*/dhcpd.conf.example
  4 #   see dhcpd.conf(5) man page
  5 #
​
#第三行即为dhcp配置示例文件的位置
​
[root@localhost ~]# vim  /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example 
​
#进入该文件查看示例文本内容

将示例文件覆盖/etc/dhcp/dhcpd.conf文件

[root@localhost ~]# cp -a /user/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
​

配置文件格式

#全局设置
# 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;
​
​
#subnet网段声明
# 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;
}
​

DHCP服务配置实验

创建主机A

这里以windows系统为例

  • 关闭windows主机防火墙
  • 网卡设置使用NAT模式
  • 将windows主机网络设置为dhcp自动获取
  • 关闭VMware虚拟机程序自带的DHCP服务
  • 释放windows当前的IP地址:ipconfig /release

微信截图_20240428160434.png

微信截图_20240428160519.png

微信截图_20240428160556.png

创建DHCP服务器

  1. 安装DHCP服务
[root@localhost ~]# yum -y install dhcp
  1. 关闭防火墙
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
​
  1. 将示例文档覆盖至dhcp配置文件存放的文件夹中
[root@localhost ~]# cp -a /user/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
  1. 编辑配置文件
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf 
# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers 218.2.135.1;             #全局设置中将DNS设置为218.2.135.1,但是局部设置比全局配置优先级高
​
# This is a very basic subnet declaration.          #subnet网段声明配置:
​
subnet 192.168.137。0 netmask 255.255.255.0 {        #规定DHCP服务器的网段为192.168.137.0,子网24
  range 192.168.137.50 192.168.137.100;             #规定索要分配的地址范围为:50-100
  option routers 192.168.137.2;                     #设置DHCP服务器的网关为192.168.137.2
​
:wq                                                 #保存设置并退出
​
  1. 开启DHCP服务
[root@localhost ~]# systemctl start dhcpd
  1. 查看DHCP服务状态
[root@localhost ~]# systemctl status dhcpd
● dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2024-04-28 16:19:24 CST; 44s ago
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)
​

主机A上查看网络情况

  • 打开windows主机CMD

微信截图_20240428162238.png

  • 在cmd中输入ipconfig /all查看当前网卡的IP地址,这里显示为192.168.137.50,与DHCP配置文件中所设置的地址池起始IP相符,说明配置成功

QQ截图20240428162400.png

微信截图_20240428162314.png

实现时间同步

与阿里云服务器同步时间

安装chrony服务

[root@localhost ~]# yum install -y chrony

查找chrony配置文件

[root@localhost ~]# rpm -qc chrony
/etc/chrony.conf

修改配置文件与阿里云同步时间

[root@localhost ~]# vim /etc/chrony.conf 
​
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
server ntp.aliyun.com iburst                            #以下几行均为阿里云时间同步服务器地址
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
​
# Allow NTP client access from local network.           #设置允许与本台时间同步服务器同步时间的网段
#allow 192.168.0.0/16
allow 192.168.137.0/24                                  #开启192.168.137.0网段的机器可以同步时间
​

开启chrony服务及查看状态

[root@localhost ~]# systemctl start chronyd
[root@localhost ~]# systemctl status chronyd
​

验证时间同步服务器是否生效

[root@localhost ~]# date
2024年 04月 28日 星期日 16:40:35 CST      #当前本机自动同步的时间
​
# 手动修改错误时间
[root@localhost ~]# date -s '1year'
2024年 04月 28日 星期日 16:40:35 CST      #修改时间增加1年
​
#使用阿里云ntp同步时间
[root@localhost ~]# ntpdate ntp.aliyun.com
28 Apr 16:41:56 ntpdate[3559]: step time server 203.107.6.88 offset -31535999.998718 sec
[root@localhost ~]# date
2024年 04月 28日 星期日 16:42:07 CST      #同步完成后时间更正
​

远端主机B通过时间同步服务器同步时间

[root@localhost ~]# systemctl disable firewalld #关闭防火墙
[root@localhost ~]# setenforce 0                #降低访问限制
​
[root@localhost ~]# yum -y install chrony
[root@localhost ~]# systemctl start chronyd     #开启时间同步服务
[root@localhost ~]# vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
server 192.168.137.101 iburst                   #将时间同步服务器的IP地址作为同步地址
​
​
[root@localhost ~]# systemctl restart chronyd   #重启时间同步服务
[root@localhost ~]# date
2024年 04月 28日 星期日 17:01:08 CST
[root@localhost ~]# date -s '2year'
2026年 04月 28日 星期二 17:01:17 CST
[root@localhost ~]# ntpdate 192.168.137.101
[root@localhost ~]# date
2024年 04月 28日 星期日 17:03:11 CST