参考文献

chrony

安装

1
apt-get install chrony

显示系统时钟性能的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# chronyc tracking
Reference ID : CA760182 (time.neu.edu.cn)
Stratum : 3
Ref time (UTC) : Wed Mar 19 01:28:02 2025
System time : 0.000000166 seconds fast of NTP time
Last offset : +0.000341989 seconds
RMS offset : 0.000341989 seconds
Frequency : 9.475 ppm slow
Residual freq : -216.112 ppm
Skew : 1000000.000 ppm
Root delay : 0.041528080 seconds
Root dispersion : 6.417131901 seconds
Update interval : 1.4 seconds
Leap status : Normal

显示时间同步源

1
2
3
4
5
6
7
8
9
10
11
12
# chronyc sources
210 Number of sources = 8
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^- prod-ntp-5.ntp1.ps5.cano> 2 6 17 17 -15ms[ -15ms] +/- 111ms
^- prod-ntp-4.ntp1.ps5.cano> 2 6 17 17 -5136us[-5136us] +/- 120ms
^- alphyn.canonical.com 2 6 17 19 -3494us[-3152us] +/- 134ms
^- prod-ntp-3.ntp4.ps5.cano> 2 6 17 18 -30ms[ -30ms] +/- 115ms
^- ntp1.flashdance.cx 2 6 17 18 +6771us[+6771us] +/- 129ms
^* time.neu.edu.cn 2 6 17 18 +391us[ +733us] +/- 21ms
^- a.chl.la 2 6 17 20 +7982us[+8324us] +/- 95ms
^? 113.207.49.85 0 6 0 - +0ns[ +0ns] +/- 0ns

查看当前系统的时间同步源状态信息(例如漂移率和偏移估算过程)

1
2
3
4
5
6
7
8
9
10
11
12
# chronyc sourcestats
210 Number of sources = 8
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
prod-ntp-5.ntp4.ps5.cano> 4 3 8 +2174.369 25830.625 +69ms 5601us
prod-ntp-4.ntp4.ps5.cano> 4 3 9 -1728.351 98739.695 -94ms 14ms
alphyn.canonical.com 4 3 9 +338.964 45813.980 +13ms 7470us
prod-ntp-3.ntp1.ps5.cano> 4 3 10 -2410.892 15184.396 -125ms 3969us
ntp1.flashdance.cx 4 4 10 -25.600 1606.538 +303us 338us
time.neu.edu.cn 4 3 8 -216.112 2223.333 -8637us 431us
a.chl.la 4 3 8 -56.325 12130.501 +3911us 1982us
113.207.49.85 0 0 0 +0.000 2000.000 +0ns 4000ms

查看时间同步源的状态

1
2
3
4
5
6
7
# chronyc activity
200 OK
8 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

配置文件

  • 配置文件可能路径(不同操作系统可能存在差异):

    • /etc/chrony.conf

    • /etc/chrony/chrony.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usuable directives.

# This will use (up to):
# - 4 sources from ntp.ubuntu.com which some are ipv6 enabled
# - 2 sources from 2.ubuntu.pool.ntp.org which is ipv6 enabled as well
# - 1 source from [01].ubuntu.pool.ntp.org each (ipv4 only atm)
# This means by default, up to 6 dual-stack and up to 2 additional IPv4-only
# sources will be used.
# At the same time it retains some protection against one of the entries being
# down (compare to just using one of the lines). See (LP: #1754358) for the
# discussion.
#
# About using servers from the NTP Pool Project in general see (LP: #104525).
# Approved by Ubuntu Technical Board on 2011-02-08.
# See http://www.pool.ntp.org/join.html for more information.
pool ntp.ubuntu.com iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2

# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys

# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift

# Uncomment the following line to turn logging on.
#log tracking measurements statistics

# Log files location.
logdir /var/log/chrony

# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0

# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync

# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3

配置服务器

  • 作为时钟源的服务器配置
1
2
3
4
5
vim /etc/chrony/chrony.conf

server 127.0.0.1 iburst #本地作为时钟源
local stratum 10 #允许本地同步
allow #允许所有连接
1
2
3
4
5
6
7
8
# 开机启动
systemctl enable chronyd
# 启动服务
systemctl start chronyd
# 查看状态
systemctl status chronyd
# 查看同步 带星号*为同步成功
chronyc sources -v
  • 作为客户端配置
1
2
3
4
5
vim /etc/chrony/chrony.conf

server 时钟源服务器IP iburst
# 运行所有访问
allow
1
2
# 查看同步状态,显示刚刚配置的ip 并且ip前面有,*星号即为成功 ,这个符号 ^ 为失败
chronyc sources -v