Linux之搭建chrony时钟同步服务

一、chrony时钟同步服务

  chrony是网络时间协议(NTP)的通用实现。它可以将系统时钟与NTP服务器,参考时钟(例如GPS接收器)以及使用手表和键盘进行的手动输入进行同步。它还可以充当NTPv4(RFC 5905)服务器并与之同行,以向网络中的其他计算机提供时间服务。
  它被设计为在各种条件下均能良好运行,包括间歇性网络连接,网络严重拥塞,温度变化(普通计算机时钟对温度敏感)以及无法连续运行或在虚拟机上运行的系统。
  通过Internet同步的两台计算机之间的典型精度在几毫秒内;在LAN上,精度通常为数十微秒。使用硬件时间戳记或硬件参考时钟,亚微秒精度是可能的。
  chrony包括两个程序,chronyd一个是守护程序,可以在引导时启动,chronyc另一个是命令行界面程序,可以用来监视其chronyd性能并在运行时更改各种操作参数。目前centos7已经默认安装chrony3.2作为时钟服务。

二、chrony服务搭建步骤

1、chrony服务端安装及配置

1)、使用yum install -y chrony命令安装软件

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

2)、编辑/etc/chrony.conf配置文件

#注释如下四行
# 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 ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
#设置运行使用通过此服务器同步时钟的网段
allow 192.168.10.0/24

3)、启动chrony服务

[root@test1 ~]# systemctl start chronyd
[root@test1 ~]# systemctl status chronyd

4)、设置开机自启动

[root@test1 ~]# systemctl enable chronyd

5)、检查阿里云时钟服务状态

^*表示当前选择的时钟源,^表示有效的时钟源服务器

[root@test1 ~]# chronyc sources
210 Number of sources = 2
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 120.25.115.20 2 7 377 123 +1733us[+1679us] +/- 13ms
^+ 203.107.6.88 2 7 377 123 -6546us[-6546us] +/- 39ms

2、chrony客户端安装及配置

1)、使用yum install -y chrony命令安装软件

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

2)、编辑/etc/chrony.conf配置文件

#注释如下四行
# 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.0.124 iburst

3)、启动chrony服务

[root@test1 ~]# systemctl start chronyd
[root@test1 ~]# systemctl status chronyd

4)、设置开机自启动

[root@test1 ~]# systemctl enable chronyd

5)、检查时钟服务器工作状态

[root@test2 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.0.124 3 6 377 60 +353us[-1166us] +/- 36ms

三、chrony配置参数说明

配置文件路径/etc/chrony.conf

  • server: 该参数可以多次用于添加时钟服务器,必须以"server "格式使用。可以添加任意个时间服务器。
  • stratumweight: stratumweight指令设置当chronyd从可用源中选择同步源时,每个层应该添加多少距离到同步距离。
    默认情况下,CentOS中设置为0,让chronyd在选择源时忽略源的层级。
  • driftfile: 根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中是最合理的,
    在重启后为系统时钟作出补偿,甚至可能的话,会从时钟服务器获得较好的估值。
  • rtcsync: rtcsync指令将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)。
  • allow/deny: 这里你可以指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器。
    例:
    allow 192.168.0.10
    deny 192.168.2.10
  • cmdallow/cmddeny: 指定哪个IP地址或哪台主机可以通过chronyd使用控制命令。
  • bindcmdaddress : 该指令允许你限制chronyd监听哪个网络接口的命令包(由chronyc执行)。
    该指令通过cmddeny机制提供了一个除上述限制以外可用的额外的访问控制等级。
    例:
    bindcmdaddress 127.0.0.1
    bindcmdaddress ::1
  • makestep: chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在调整期大于某个阀值时步进调整系统时钟,但只有在因为chronyd启动时间超过指定限制(可使用负值来禁用限制),没有更多时钟更新时才生效。
    常见参数如上,实际参数还有很多,可以在官网查看。
    https://chrony.tuxfamily.org/documentation.html

四、chrony与ntp比较

1、chrony比ntp做得更好的点

  • chrony可以在断断续续地访问时间参考的环境中发挥作用。ntp需要定期对参考进行轮询才能正常工作。
  • chrony 通常可以更快,更准确地同步时钟。
  • chrony快速适应时钟速率的突然变化(例如,由于晶体振荡器温度的变化)。ntp可能需要很长时间才能重新安定下来。
  • chrony 即使网络长时间处于拥塞状态也可以正常工作。
  • chrony在默认配置中,从不花时间不打扰其他正在运行的程序。ntp可以将其配置为永远也不会步进时间,但是在那种情况下,它必须使用其他调整时钟的方式(守护进程而不是内核规则),这可能会对时钟的准确性产生负面影响。
  • chrony 可以在更大范围内调整时钟速率,从而使其即使在时钟中断或不稳定的计算机上(例如某些虚拟机)也可以运行。
  • chrony 较小的设备,它使用较少的内存,并且仅在必要时才唤醒CPU,这更有利于节能。

2、chrony能做到但ntp不能的

  • chrony 支持网络时间安全(NTS)身份验证机制。
  • chrony 在Linux上支持硬件时间戳,从而可以在本地网络中实现极其稳定和准确的同步。
  • chrony无论时间校正的唯一方法是手动输入(例如,由管理员看着时钟),都可以为隔离的网络提供支持。chrony可以查看在不同更新中纠正的错误,以计算出计算机增加或减少时间的速率,然后使用此估计值来调整计算机时钟。
  • chrony提供支持来计算实时时钟的增益或丢失率,即维持计算机关闭时间的时钟。当系统启动时,可以使用此数据从更正后的实时时钟版本中设置系统时间。到目前为止,这些实时时钟功能仅在Linux上可用。

3、ntp能做到但chrony不能的

  • ntp支持RFC 5905中的所有操作模式,包括广播,多播和多播服务器/客户端。但是,广播和多播模式固有地比普通服务器/客户端模式更不准确,也不安全(即使具有身份验证),因此通常应避免。
  • ntp支持自动密钥协议(RFC 5906),以使用公共密钥加密对服务器进行身份验证。请注意,该协议已显示为不安全,并且已被NTS(RFC 8915)废弃。
  • ntp 已移植到更多操作系统。
  • ntp包括大量用于各种硬件参考时钟的驱动程序。chrony需要其他程序(例如gpsd或ntp-refclock)通过SHM或SOCK接口提供参考时间。

五、chromyc命令示例

  • chronyc:进入设置

[root@test2 ~]# chronyc
chrony version 3.4
Copyright © 1997-2003, 2007, 2009-2018 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and
you are welcome to redistribute it under certain conditions. See the
GNU General Public License version 2 for details.


chronyc>

  • activity: 该命令会显示有多少NTP源在线/离线

[root@test2 ~]# chronyc activity
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

  • add server: 手动添加一台新的NTP服务器

[root@test2 ~]# chronyc add server 192.168.0.131
200 OK
[root@test2 ~]# chronyc sources
210 Number of sources = 2
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.0.124 3 6 377 60 -287us[ -420us] +/- 14ms
^? 192.168.0.131 11 6 1 4 +36ms[ +36ms] +/- 2085us

  • clients: 在客户端报告已访问到服务器

[root@test1 ~]# chronyc clients
Hostname NTP Drop Int IntL Last Cmd Drop Int Last
===============================================================================
192.168.0.125 40 0 6 - 49 0 0 - -

  • delete: 手动移除NTP服务器或对等服务器

[root@test2 ~]# chronyc delete 192.168.0.131
200 OK
[root@test2 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.0.124 3 6 377 23 -300us[ -422us] +/- 15ms

  • tracking: 显示系统时间信息

[root@test2 ~]# chronyc settime
505 Facility not enabled in daemon
[root@test2 ~]# chronyc tracking
Reference ID : C0A8007C (192.168.0.124)
Stratum : 4
Ref time (UTC) : Fri Jan 15 08:52:16 2021
System time : 0.000000299 seconds fast of NTP time
Last offset : -0.000250601 seconds
RMS offset : 0.000372452 seconds
Frequency : 33.019 ppm slow
Residual freq : -1.977 ppm
Skew : 4.737 ppm
Root delay : 0.022533407 seconds
Root dispersion : 0.005204781 seconds
Update interval : 64.4 seconds
Leap status : Normal

  • help:显示帮助

[root@test2 ~]# chronyc -help
chrony version 3.4
Copyright © 1997-2003, 2007, 2009-2018 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and
you are welcome to redistribute it under certain conditions. See the
GNU General Public License version 2 for details.


Could not open connection to daemon

  • sources:查看时钟源工作情况

[root@test2 ~]# chronyc sources -v
210 Number of sources = 1


.-- Source mode ‘^’ = server, ‘=’ = peer, ‘#’ = local clock.
/ .- Source state ‘*’ = current synced, ‘+’ = combined , ‘-’ = not combined,
| / ‘?’ = unreachable, ‘x’ = time may be in error, ‘~’ = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | |
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.0.124 3 6 377 39 -301us[-1206us] +/- 16ms

猜你喜欢

转载自blog.csdn.net/carefree2005/article/details/112664186