1.概念
NTP(Network Time Protocol)网络时间协议基于UDP用于网络时间同步的协议,使网络中的计算机时钟同步到UTC(世界统一时间),再配合各个时区的偏移调整就能实现精准同步对时功能。
chrony是网络时间协议NTP的实现方式, Chrony是一个开源的自由软件,在RHEL7/CentOS7操作系统已经是默认安装服务,它能保持系统时间与NTP时间服务器的时间保持同步,让时间始终保持同步。
软件包:chrony
服务名:chronyd
命令程序:chronyc
配置文件:/etc/chrony.conf
2.安装chrony
[root@localhost ~]# rpm -q chrony #cenos7/RHEL8默认安装
[root@localhost ~]# systemctl status chronyd #查看服务状态
[root@localhost ~]# systemctl is-enabled chronyd #查看服务是否是开机自启
[root@localhost ~]# systemctl start chronyd #开启服务
[root@localhost ~]# systemctl enable chronyd #设置开机自启动
3.配置文件介绍
配置源文件:[root@localhost ~]# vim /etc/chrony.conf
解释:
# 使用 pool.ntp.org 项目中的公共服务器。以server开,理论上想添加多少时间服务器都可以。
# 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
# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
# 如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进。
# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
makestep 1.0 3
# 启用实时时钟(RTC)的内核同步。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# 通过使用 hwtimestamp 指令启用硬件时间戳
# 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
# 指定 NTP 客户端地址,以允许或拒绝连接到扮演时钟服务器的机器
# Allow NTP client access from local network.
#allow 192.168.0.0/16
# Serve time even if not synchronized to a time source.
#local stratum 10
# 指定包含 NTP 身份验证密钥的文件。
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys
# 指定日志文件的目录。
# Specify directory for log files.
logdir /var/log/chrony
# 选择日志文件要记录的信息。
# Select which information is logged.
#log measurements statistics tracking
4.常用命令
[root@localhost ~]# chronyc sources -n #查看时间同步源
解释:
①M:这表示信号源的模式。^表示服务器,=表示对等方,并#指示本地连接的参考时钟。
②S:此列指示源的状态。“ *”表示chronyd当前同步到的源。“ +”表示可接受的来源,已与所选来源合并。“-”表示组合算法排除的可接受源。“?” 指示已失去连接性或其数据包未通过所有测试的源。“ x”表示一个时钟,它chronyd认为是虚假行情(其时间与大多数其他来源不一致)。“〜”表示时间似乎变化太大的来源。“?” 启动时也会显示条件,直到从中至少收集了3个样本为止。
③Name/IP address: 这显示了IP源的名称或地址。
④Stratum:这显示了来源的层次,如其最近收到的样本中所报告的那样。层1表示具有本地连接的参考时钟的计算机。与第1层计算机同步的计算机位于第2层。与第2层计算机同步的计算机位于第3层,依此类推。
⑤Poll:这显示轮询源的速率,以秒为单位的间隔的以2为底的对数。因此,值为6表示每64秒进行一次测量。chronyd 根据当前情况自动更改轮询速率。
⑥Reach :这显示了源的范围寄存器以八进制数字打印。该寄存器有8位,并在从源接收或丢失的每个数据包上进行更新。值377表示已收到所有最后八次传输的有效回复。
⑦LastRx: 此列显示多久以前从源接收到最后一个样本。通常以秒为单位。这些信件m,h,d或y表示分钟,小时,天或数年。10年的值表示尚未从此来源接收到任何样本。
⑧Last sample:此列显示上次测量时本地时钟与源之间的偏移。方括号中的数字表示实际测得的偏移量。这可以以ns(表示纳秒),us(表示微秒),ms(表示毫秒)或s(表示秒)作为后缀。方括号左侧的数字表示原始测量值,已调整为允许此后施加于本地时钟的任何摆度。+/-指示器后面的数字表示测量中的误差范围。正偏移表示本地时钟位于源时钟之前。
5.实操
搭建一台NTP服务器,同步阿里云时间,当阿里云不可用时,使用本地时间,其他服务器同步这台服务器上的时间,确保时间一致。
①配置服务端
[root@localhost ~]# vim /etc/chrony.conf #编辑服务端配置文件
[root@localhost ~]# systemctl restart chronyd #重启chronyd服务
[root@localhost ~]# chronyc sources -n #查看时间同步源
②配置客户端
[root@host26 ~]# rpm -q chrony #查看是否安装chrony
[root@host26 ~]# vim /etc/chrony.conf #编辑客户端配置文件
[root@host26 ~]# systemctl restart chronyd #重启服务
[root@host26 ~]# systemctl enable chronyd #设置开机自启
[root@host26 ~]# systemctl status chronyd #查看服务状态
[root@host26 ~]# chronyc sources -n #查看时间同步源