百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

一文带你了解Chrony时间同步服务:让你的设备时间完美同步!

nanshan 2024-11-13 11:32 21 浏览 0 评论

一、软件介绍

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 命令来修改设置,以下是常用命令。

  • accheck 检查 NTP 访问是否对特定主机可用
  • activity 该命令会显示有多少 NTP 源在线/离线
  • add server 手动添加一台新的 NTP 服务器
  • clients 在客户端报告已访问到服务器
  • delete 手动移除 NTP 服务器或对等服务器
  • settime 手动设置守护进程时间
  • tracking 显示系统时间信息
  • help 可以查看更多 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

相关推荐

微软发布Win11/10 ISO镜像Defender更新,提升系统初始安全性

IT之家7月27日消息,除了Setup及WinRE更新外,NeoWin发现微软本周还针对Windows11/10/Server安装镜像发布了新的Defender安全智能...

微软革新Windows装机体验:内置应用全面升级,安全与便捷双提升

Windows内置应用迎来重大变革:更安全、更快速的初始体验如果您曾亲自安装过Windows11,或许注意到其内置应用并非开箱即用,而是一些占位程序,需要首次运行时从微软应用商店(Microsoft...

Hotpatch继续扩展 现在更多Windows PC在更新后无需重启

Windows11最近从其服务器版本中获得了一项非常重要的功能:Windows热补丁。该功能旨在通过允许操作系统在无需重启的情况下安装重要的安全更新来最大限度地减少停机时间和中断。最初,微软在...

微软承认Windows Server六月更新存在BUG:导致DHCP服务器故障

IT之家6月17日消息,科技媒体WindowsLatest今天(6月17日)发布博文,报道称微软承认6月WindowsServer更新存在BUG,可能导致DHCP服...

Windows Server2019安装Hyper-V的2个简单方法!

关于WindowsServer2019WindowsServer2019是微软发布的服务器操作系统,是WindowsServer2016的后续版本。它包含了许多新的特性和改进,适用于数据中心...

如何在不满足系统要求的旧计算机上安装 Windows 11 24H2

如果你想了解这个安装工具以及安装方法(老飞摄影微信公众号内提供安装包下载),请完整的看完后面的文字,以避免在安装过程当中出现问题。Windows11通常需要某些硬件功能,例如TPM和安全启动,...

第 137 期:微软表示 Windows 11 24H2 是迄今为止最稳定的版本

就在刚刚,微软“大言不惭”地声称,Windows1124H2是迄今为止最可靠的Windows版本。我们并不是说它很糟糕,因为我们每天的工作中也在使用它。上述言论只是一份微软的一份官方文件的一...

Windows 11 将推出带有“高级”选项的新设置页面

Windows11即将迎来一个包含一些高级功能的全新“设置”页面。严格来说,它并非全新功能。它更像是“开发者”栏目的重新设计,用户和开发者可以在其中调整各种附加功能。微软可能明白这些东西不仅对开发...

Windows server 2025 重复数据删除

一、概述windowsserver中的重复数据删除功能从windowsserver2012就开始支持了。Windowsserver中默认没有安装重复数据删除功能。在磁盘分区(卷)上启用重复...

Windows Server 2025预览版迎来更新,微软改善Insiders测试体验

在发布WindowsServer的build26040版本之际,微软公布了该产品的官方名称:WindowsServer2025。一同推出的,还有Windows11WindowsInsid...

升不升?Win11 24H2大范围推送了

微软在其官方支持文档中宣布,24H2版现在已经开始向运行Windows11原始版本、22H2和23H2版的合格设备推送。Windows11的24H2更新现已进入新的可用性阶段,这意味着更多符合条件...

微软发布Win11/10/Server安装镜像Defender更新

IT之家6月22日消息,继上个月为Lumma发布更新后,微软本月也为Windows11/10/Server安装镜像发布了新的Defender更新。此更新包很有必要,因为Wi...

第 81 期:微软最近的更新给 Windows Server 带来了 DHCP 问题

近日,微软确认,DHCP服务器服务可能会在WindowsServer安装2025年6月更新后停止响应或拒绝连接。DHCP问题会影响WindowsServer2025(KB50...

windws server 2012 R2 虚拟机windows server2019 经常断网事件

故障现象:在windowsserver2012R2的虚拟主机上面搭建一个Windowsserver2019的虚拟机系统用来做域控。安装完设置好防火墙和IP,经过测试是可以ping同正常访问...

微软扩展热补丁部署,现覆盖ARM架构Win11 24H2设备

IT之家7月9日消息,科技媒体NeoWin今天(7月9日)发布博文,报道称微软扩大热补丁(WindowsHotpatching)覆盖范围,在AMD和英特尔处理器设备外,现覆盖支...

取消回复欢迎 发表评论: