时间同步软件


一、软件介绍

Chrony 是一个开源的自由软件,它能帮助你保持系统时钟与时钟服务器同步,因此让你的时间保持精确。它由两个程序组成,分别是 chronyd 和 chronyc。

服务器列表 - 全球可用的NTP服务器列表与解析服务 - ntp.org.cn

工作模式

  • chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
  • chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

软件优势

  • 在初始同步后,它不会停止时钟,以防对需要系统时间保持单调的应用程序造成影响。
  • 在应对临时非对称延迟时(例如,在大规模下载造成链接饱和时)提供了更好的稳定性。
  • 无需对服务器进行定期轮询,因此具备间歇性网络连接的系统仍然可以快速同步时钟。
  • 能够更好地响应时钟频率的快速变化,这对于具备不稳定时钟的虚拟机或导致时钟频率发生变化的节能技术而言非常有用。
  • 更快的同步只需要数分钟而非数小时时间,从而最大程度减少了时间和频率误差,这对于并非全天 24 小时运行的台式计算机或系统而言非常有用。

二、安装启用

CentOS7 已经默认安装有 Chrony 工具,其既可作时间服务器服务端,也可作客户端。而且性能比 ntp 要好很多、配置简单、管理方便。

[1] 安装启动

#安装服务
yum install -y chrony

# 启动服务
systemctl start chronyd.service

# 设置开机自启动,默认就是enable的
systemctl enable chronyd.service

[2] 防火墙配置

# 因NTP使用123/UDP端口协议,所以允许NTP服务即可
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload
chronyc -a makestep

[3] 设置时区

# 查看日期时间、时区及NTP状态
timedatectl

# 查看时区列表
timedatectl list-timezones

# 修改时
timedatectl set-timezone Asia/Shanghai

# 修改日期时间
timedatectl set-time "2023-11-21 11:50:00"

# 设置完时区后,强制同步下系统时钟
chronyc -a makestep

三、主要配置

当 Chrony 启动时,它会读取 /etc/chrony.conf 配置文件中的设置,配置内容格式和 ntpd 服务基本相似。

[root@localhost ~]# cat /etc/chrony.conf |grep -v ^#|grep -v ^$
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
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony

四、查看状态

  • [1] 检查 ntp 源服务器状态
[root@bogon ~]# chronyc sourcestats
210 Number of sources = 6
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
ntp1.flashdance.cx         15   7   910     +3.964     20.053    -29ms  4416us
ntp.jxust.edu.cn           19  11   978     -0.014      7.182  -1702ns  2772us
ntp8.flashdance.cx         15  10   983     -2.895     23.856    -26ms  7556us
ntp6.flashdance.cx         13   5   853     +1.580     15.504    -21ms  3510us
36.154.179.82               0   0     0     +0.000   2000.000     +0ns  4000ms
time.neu.edu.cn             2   0    64     +0.000   2000.000  +2276us  4000ms
  • [2] 检查 ntp 详细同步状态
[root@bogon ~]# chronyc sources -v
210 Number of sources = 5

  .-- 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               
===============================================================================
^? ntp1.flashdance.cx            2   6     1    62    -30ms[  -30ms] +/-  162ms
^* ntp.jxust.edu.cn              2   6   177     5   -666us[-1389us] +/-   51ms
^- ntp8.flashdance.cx            2   6   177     4    -10ms[  -10ms] +/-  174ms
^- ntp6.flashdance.cx            2   6   252   192    -29ms[  -25ms] +/-  140ms
^? 36.154.179.82                 0   8     0     -     +0ns[   +0ns] +/-    0ns

[3] 设置硬件时间

# 硬件时间默认为UTC
$ timedatectl set-local-rtc 1

# 启用或关闭NTP时间同步
$ timedatectl set-ntp yes|flase

# 校准时间服务器
$ chronyc tracking

五、工具使用

可以通过运行 chronyc 命令来修改设置,以下是常用命令。

root@bogon ~]# chronyc
chrony version 3.1
Copyright (C) 1997-2003, 2007, 2009-2017 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
200 OK
5 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
chronyc> add server  edu.ntp.org.cn
200 OK
chronyc> 
chronyc> sourcestats
210 Number of sources = 6
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
ntp1.flashdance.cx         14   7   845     +3.192     23.874    -30ms  4568us
ntp.jxust.edu.cn           19  11   978     -0.014      7.182   -286ns  2772us
ntp8.flashdance.cx         15  10   983     -2.895     23.856    -25ms  7556us
ntp6.flashdance.cx         13   5   853     +1.580     15.504    -21ms  3510us
36.154.179.82               0   0     0     +0.000   2000.000     +0ns  4000ms
time.neu.edu.cn             1   0     0     +0.000   2000.000     +0ns  4000ms
chronyc> add server  cn.ntp.org.cn
200 OK
chronyc> sourcestats
210 Number of sources = 7
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
ntp1.flashdance.cx         16   8   975     +1.588     18.059    -30ms  4475us
ntp.jxust.edu.cn           20  12  1108     +0.074      5.875  +3349ns  2595us
ntp8.flashdance.cx         16  11  1114     -0.945     20.762    -24ms  7371us
ntp6.flashdance.cx         14   6  1110     -0.308     10.407    -23ms  3311us
36.154.179.82               0   0     0     +0.000   2000.000     +0ns  4000ms
time.neu.edu.cn             3   3   129    +22.397   7720.335  +4764us   915us
58.220.133.132              1   0     0     +0.000   2000.000     +0ns  4000ms

免责声明
    以上文章转载自互联网,文章内容仅供参考,不构成建议,也不代表百科学社赞同其观点。如有侵权请联系755934052@qq.com,提供原文链接地址以及资料原创证明,本站将会立即删除

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请通知我们,一经查实,本站将立刻删除。