NTP(Network Time Protocol)网络时间协议基于UDP用于网络时间同步的协议,使网络中的计算机时钟同步到UTC(世界统一时间),再配合各个时区的偏移调整就能实现精准同步对时功能。
chrony是网络时间协议NTP的实现方式, Chrony是一个开源的自由软件 ,在RHEL7/CentOS7操作系统已经是默认安装服务,它能保持系统时间与NTP时间服务器的时间保持同步,让时间始终保持同步。
软件包:chrony
服务名:chronyd
命令程序:chronyc
配置文件:/etc/chrony.conf
安装chrony程序
#CentOS7/RHEL7以默认安装
[root@localhost ~]# rpm -q chrony
chrony-3.2-2.el7.x86_64
#查看服务运行状态
[root@localhost ~]# systemctl status chronyd
#启动chronyd服务&&设置服务随机期自
[root@localhost ~]# systemctl start chronyd
[root@localhost ~]# systemctl enable chronyd1、配置文件介绍
#修改/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).
#配置NTP时间服务器
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
# Record the rate at which the system clock gains/losses time.
#记录系统时钟获得/丢失时间的速率至drift文件中
driftfile /var/lib/chrony/drift
# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
# 默认情况下,chronyd通过减慢或加快时钟速度来逐渐调整时钟。如果时钟与实际时间偏差太大,则需要很长时间才能纠正错误。这种方法叫做步进时钟(时间跳变)。
# 此处表示如果调整值大于1000秒,则这将使系统时钟步进,但仅在前10个时钟更新中。
makestep 1000 10
# Enable kernel synchronization of the real-time clock (RTC).
# 启用RTC(实时时钟)的内核同步
rtcsync
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# Allow NTP client access from local network.
# 只允许192.168.网段的客户端进行时间同步
#allow 192.168.0.0/16
# Serve time even if not synchronized to a time source.
# NTP服务器不可用时,采用本地时间作为同步标准
#local stratum 10
# Specify file containing keys for NTP authentication.
# 指定包含NTP验证密钥的文件
#keyfile /etc/chrony.keys
# Specify directory for log files.
# 指定日志文件的目录
logdir /var/log/chrony
# Select which information is logged.
#将对系统增益或损耗率的估计值以及所做的任何转换记录的更改记录到名为的文件中tracking.log
#log measurements statistics tracking2、常用命令
#查看时间同步源
[root@localhost ~]# chronyc sources -n
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 17 32 +938us[ -17ms] +/- 24ms
^- time.cloudflare.com 3 6 17 31 +56ms[ +56ms] +/- 163ms
^- ns1.luns.net.uk 2 6 17 30 +26ms[ +26ms] +/- 166ms
^? ntp7.flashdance.cx 0 6 0 - +0ns[ +0ns] +/- 0ns
#立刻手动同步时间
[root@localhost ~]# chronyc -a makestep
200 OK
#检查时间是否同步
[root@localhost ~]# chronyc tracking
Reference ID : CB6B0658 (203.107.6.88) #同步到的服务器ID与IP地址
Stratum : 3
Ref time (UTC) : Wed Jun 23 15:26:55 2021
System time : 0.001027327 seconds fast of NTP time
Last offset : +0.001285393 seconds
RMS offset : 0.016554436 seconds
Frequency : 9.343 ppm slow
Residual freq : -0.813 ppm
Skew : 1.096 ppm
Root delay : 0.038546667 seconds
Root dispersion : 0.000631557 seconds
Update interval : 64.2 seconds
Leap status : Normal3、案例需求
搭建一台NTP服务器,同步阿里云时间,当阿里云不可用时,使用本地时间,其他服务器同步这台服务器上的时间,确保时间一致。
4、配置服务端
[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开头的配置,添加阿里云NTP公共时间同步服务器
#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
#添加阿里NTP服务
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
#允许所有网段客户端进行时间同步
allow 0.0.0.0/0
#NTP服务器不可用时,采用本地时间作为同步标准
local stratum 10
#重启chronyd服务
[root@localhost ~]# systemctl restart chronyd
#查看时间同步源
[root@localhost ~]# chronyc sources -n
210 Number of sources = 2
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 17 43 -87us[-3038us] +/- 22ms
^+ 120.25.115.20 2 6 33 41 +4255us[+4255us] +/- 24ms5、配置客户端
#查看chrony软件包是否安装
[root@host26 ~]# rpm -q chrony
chrony-3.2-2.el7.x86_64
#修改配置文件/etc/chrony.conf
[root@host26 ~]# vim /etc/chrony.conf
# 注释其他server开头的配置,添加本地NTP公共时间同步服务器
server 192.168.0.24 iburst
#重启chronyd服务&&设置服务随机自启
[root@host26 ~]# systemctl restart chronyd
[root@host26 ~]# systemctl enable chronyd
[root@host26 ~]# systemctl status chronyd
#查看时间同步源
[root@host26 ~]# chronyc sources -n
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.0.24 0 7 0 - +0ns[ +0ns] +/- 0n
#查看系统时间
[root@host26 ~]# date
评论区