就Linux 时间同步软件来说,只要有Chrony,ntpd,systemd-timesyncd等,对一般是使用场景,这三种软件都能满足准确性的要求,但就深入到时间精度和同步时间方式来说,还是有很大的区别的,通常是选用顺序是Chrony,systemd-timesyncd,ntpd,然后才是其他。Chrony与ntpd主要区别如下:
chronyd
可以正常工作,其中对时间参考的访问是间歇性的,而ntpd
需要定期轮询时间引用才能正常工作。- 即使网络在较长时间内拥塞,chronyd
也能
表现良好。 chronyd
通常可以更快准确地同步时钟。chronyd
能够快速适应时钟速率的突然变化,例如,由于碳粉电器温度的变化,ntpd
可能需要很长时间才能再次下降。- 在默认配置中,
chrony
d 从不调整时钟在系统启动时同步的时间,以便不设置其他正在运行的程序。ntpd
也可以配置为从不调整时间,但是它必须使用不同的调整时钟的方法,这种方法存在一些缺点,包括对时钟准确性的负面影响。 chronyd
可以调整较大范围内 Linux 系统上的时钟速率,即使在时钟中断或不稳定的机器上运行。例如,在某些虚拟机上:chronyd
比较小,使用较少的内存,仅在需要时才会唤醒 CPU,这更有利于节能。
一、chronyd
1.1 修改chronyd配置文件
chronyd
的默认配置文件是 /etc/chrony.conf
#指定要用作时间源的 NTP 服务器池地址。与 `server` 类似,您可以指定多个服务器池,以提高时间源的可靠性。例如:`pool pool.ntp.org`
pool 1.centos.pool.ntp.org iburst
pool 2.centos.pool.ntp.org iburst
# 指定要用作时间源的 NTP 服务器地址。您可以在此处列出多个服务器,并可以使用 IP 地址或主机名支持网络时间安全 (NTS) 身份验证机制
# pool 或 server 二选一即可
server 1.centos.pool.ntp.org iburst nts
#授予对 IPv4 的访问权限,填下允许的IP地址主机号或IP地址
allow 192.0.2.0/24
#拒绝的IP地址主机号或IP地址
deny 1.2.3.0/24
#授予对 IPv6 的访问权限:
allow 2001:0db8:85a3::8a2e:0370:7334
#这会通知保持系统时钟同步的内核,内核会每 11 分钟更新实时时钟。
rtcsync
#系统时钟同步且两个时钟之间的估计误差大于指定阈值时,chronyd将调整 RTC,单位:秒
rtcautotrim 30
#若网卡支持硬件时间戳,可启用硬件时间戳进行非常准确的同步
hwtimestamp
注意:服务端与客户端配置文件基本相同,只是客户端不需要 allow 和 deny 指令,只要将allow和deby注释即可。
1.2 设置开机启动、启动/重启服务及时间本地化配置
以 root
身份运行以下命令:
firewall-cmd --permanent --zone=public --add-port=123/udp
#chronyd 在系统启动时自动启动
systemctl enable chronyd
#启动 chronyd
systemctl start chronyd
# 设置系统时区
timedatectl set-timezone Asia/Shanghai
# yes/no "yes" 开启网络时间同步
timedatectl set-ntp yes
# yes/no "no" 表述在RTC中存储UTC时间
timedatectl set-local-rtc no
#检查 chronyd 的状态
systemctl status chronyd
#检查 chrony 是否同步
chronyc tracking
1.3 NTP时间同步状态检查
通过NTP时间同步状态检查,不是必须的操作,按需取用。
1.3.1 检查 chrony 是否同步
要检查 chrony 是否同步
,以 root
身份运行以下命令:
chronyc tracking
运行后截图
字段解释如下:
参考 ID
这是当前与计算机同步的服务器的参考 ID 和名称(或 IP
)。参考 ID 是一个十六进制数字,用于避免与 IPv4 地址混淆。
Stratum
stratum 代表到带有一个附加的参考时钟的计算机的距离。此类计算机是 stratum-1 计算机,因此示例中的计算机是两个跃点(即 a.b.c 是 stratum-2 并且从 stratum-1 同步)。
ref time
(UTC)是处理参考源中最后一次测量的时间。
System time
在正常操作中,chrony
d 从不停止系统时钟,因为时间范围的任何跳转都可能会给某些应用程序程序带来负面影响。相反,系统时钟中的任何错误都会通过稍微加快或减慢系统时钟直到错误被删除,然后返回到系统时钟正常速度来解决。这是因为,系统时钟(如使用 gettimeofday()系统调用
的其他程序读取,或 shell 中的 date 命令)将不同于 chronyd
对当前真实时间的估算值(这在服务器模式中运行时报告 NTP
客户端)。这一行中报告的数值是因此造成的不同。
Last offset
这是最后一次时钟更新中估计的本地误差。
RMS offset
这是位移值的一个长期平均值。
Frequency
"频率"是指如果 chronyd
没有更正系统时钟会出错的速率。它以 ppm(每百万的一部分)表示。例如,1 ppm 的值意味着,当系统的时钟认为它已提前 1 秒时,它实际上已比真实时间增长 1.000001 秒。
Residual freq
这显示了当前所选参考源的"有效频率"。这反映了来自参考源的测量结果表示频率和当前使用频率之间的差别。
原因并不总是为零,即对频率应用平滑过程。每次从参考源进行测量并计算出新的遗留频率时,都会将这一消耗的估计准确性与现有频率值的估算准确性(参见下一个 偏移
数)进行对比。为新频率计算加权平均值,权重取决于这些准确度。如果参考源中的测量结果采用一致的倾向,则持续时间会一直趋向于零。
skew
这是频率上估计的错误绑定。
Root delay
这是到 stratum-1 计算机的网络路径延迟总数,最终与计算机同步。根延迟值以纳秒分辨率打印。在某些极端情况下,这个值可以是负数。(这可能出现在对称对等排列中,计算机的频率不会互相跟踪,并且相对于每台计算机的往返时间而言,网络延迟非常短。)
Root dispersion
这是所有计算机回滚到 stratum-1 计算机的总分散积累,最终与计算机同步。分布是由系统时钟解析、统计测量差异导致的。Root 分散值以纳秒分辨率打印。
Leap status
这是闰秒状态,可以是 Normal、Insert second、Delete second 或 Not syncd。
1.3.2 检查 chrony 源
要检查chrony 源 ,以 root 身份运行以下命令:
chronyc sources -v
运行后截图
字段解释如下:
M
这表示源的模式。^
表示服务器,=
表示对等,#
代表本地连接的参考时钟。
S
此列指示源的状态。"*"表示当前同步的 chronyd
的源。"+"表示可接受的源与所选源结合使用。"-"表示合并算法排除的可接受的源。"?"表示丢失了哪个连接或者数据包没有通过所有测试的源。"x" " 表示 chronyd
认为是 假勾号( 时间与大多数其他来源不一致)。"~"表示时间似乎有太多变化的来源。"?" 条件也会在启动时显示,直到从中收集了至少三个样本。
名称/IP 地址
此时会显示源的名称或 IP
地址,或者参考时钟的引用 ID。
Stratum
这显示了源的 stratum,如其最近收到的示例中所报告。Stratum 1 代表有本地附加参考时钟的计算机。与 stratum 1 计算机同步的计算机处于 stratum 2。与 stratum 2 计算机同步的计算机处于stratum 3,以此类推。
Poll
显示源轮询率(以 2 为底数进行计算,以秒为单位)。因此,数值 6 表示每 64 秒就进行一次测量。chronyd
会自动根据需要条件来改变轮询率。
Reach
这将显示源的 reach 寄存器,以一个十六进制数字代表。寄存器有 8 位,并在每次从源接收或错过的数据包时进行更新。377 表示在最后的 8 个数据传输中都收到有效回复。
LastRx
这一列显示了在多久前从源中获取了最后的样本。这通常以秒为单位。字母 m
、h
、d
和 y
代表分钟、小时、天和年。值为 10 年表示尚未从该源收到任何样本。
Last sample
这一列显示本地时钟和最后一个测量源之间的偏差。方括号中的数字显示了实际测量的误差。这可以使用 ns
(代表 nanoseconds)、us
(代表 microseconds)、ms
(代表 milliseconds)或 s
(代表秒)后缀。方括号左边的数字显示了原来的测量,经过调整以允许应用本地时钟。+/-
指示符后的数字显示了测量中的错误裕度。正偏差表示本地时钟在源前面。
1.3.3 检查 chrony 源统计信息
sourcestats
命令显示目前被 chronyd
检查的每个源的偏移率和误差估算过程的信息。
可以使用可选参数 -v
来包括详细信息。在这种情况下,会输出额外的标头行显示字段含义的信息。
检查 chrony 源统计信息,以 root 身份运行以下命令:
chronyc sourcestats -v
运行后截图:
字段解释如下:
名称/IP 地址
这是 NTP
服务器(或 peer)的名称或 IP
地址,或者引用其他行中其它部分相关参考时钟的 ID。
NP
这是服务器当前保留的样本点数。偏移率和当前偏移是通过在这些点上进行线性回退来估算的。
NR
这是在上一次回滚返回后运行具有相同符号的驻留的数量。如果这个数值与样本数量相比变得太小了,意味着直线不再适合于数据。如果数量太低,chronyd
会丢弃旧的样本并重新运行回滚直到运行次数变得可以接受。
Span
这是最旧和最新样本之间的间隔。如果没有单位显示,则该数值以秒为单位。在这个示例中,间隔为 46 分钟。
Frequency
这是服务器估计的遗留频率,以每百万个部分为单位。在这种情况下,相比服务器,计算机的时钟预计运行速度为 109 个部分。
freq Skew
这是 Freq 上估计的错误绑定(以每百万分页表示)。
Offset
这是源估计的误差。
Std Dev
这是估计的示例标准偏差。
1.4 手动调整系统时钟
要立即调整系统时钟,绕过单机进行的任何调整,以 root
身份运行以下命令:
~]# chronyc makestep
如果使用了 rtcfile
指令,则不应该手动调整实时时钟。随机调整会影响 chrony测量实时时钟偏移速度的需求。
1.5 验证硬件时间戳支持
要验证接口是否支持使用 NTP
的硬件时间戳,请使用 ethtool -T
命令。如果 ethtool
列出了 SOF_TIMESTAMPING_TX_HARDWARE
和 SOF_TIMESTAMPING_TX_SOFTWARE
模式,以及 HWTSTAMP_FILTER_ ALL
过滤器模式,则可以使用硬件时间戳的 NTP
。
验证硬件时间戳支持,以 root
身份运行以下命令:
ethtool -T eth0 #eth0为网卡名称
输出解释如下:
Timestamping parameters for eth0:
Capabilities:
hardware-transmit (SOF_TIMESTAMPING_TX_HARDWARE)
software-transmit (SOF_TIMESTAMPING_TX_SOFTWARE)
hardware-receive (SOF_TIMESTAMPING_RX_HARDWARE)
software-receive (SOF_TIMESTAMPING_RX_SOFTWARE)
software-system-clock (SOF_TIMESTAMPING_SOFTWARE)
hardware-raw-clock (SOF_TIMESTAMPING_RAW_HARDWARE)
PTP Hardware Clock: 0
Hardware Transmit Timestamp Modes:
off (HWTSTAMP_TX_OFF)
on (HWTSTAMP_TX_ON)
Hardware Receive Filter Modes:
none (HWTSTAMP_FILTER_NONE)
all (HWTSTAMP_FILTER_ALL)
ptpv1-l4-sync (HWTSTAMP_FILTER_PTP_V1_L4_SYNC)
ptpv1-l4-delay-req (HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ)
ptpv2-l4-sync (HWTSTAMP_FILTER_PTP_V2_L4_SYNC)
ptpv2-l4-delay-req (HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ)
ptpv2-l2-sync (HWTSTAMP_FILTER_PTP_V2_L2_SYNC)
ptpv2-l2-delay-req (HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ)
ptpv2-event (HWTSTAMP_FILTER_PTP_V2_EVENT)
ptpv2-sync (HWTSTAMP_FILTER_PTP_V2_SYNC)
ptpv2-delay-req (HWTSTAMP_FILTER_PTP_V2_DELAY_REQ)
1.6 查验时间是否同步
# 设置系统时区
timedatectl set-timezone Asia/Shanghai
# yes/no "yes" 开启网络时间同步,相当于执行 systemctl enable --now systemd-timesyncd.service
timedatectl set-ntp yes
# yes/no "no" 表述在RTC中存储UTC时间
timedatectl set-local-rtc no
timedatectl show
#或
timedatectl
二、ntpd
2.1 修改ntpd配置文件
守护进程 ntpd
在系统启动时或 服务重启时读取配置文件。文件的默认位置为 /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# nomodify 选项可防止对配置进行任何更改。
# notrap 选项可防止 ntpdc 控制 消息协议陷阱。
# nopeer 选项可防止形成同级关联。
# noquery 选项可防止回答 ntpq 和 ntpdc 查询,而不是时间查询。
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#根据实际需要改 NTP 服务器地址
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
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
2.2 设置开机启动、启动/重启服务及时间本地化配置
以 root 身份运行以下命令:
#禁用chronyd
systemctl stop chronyd
#安装ntpd
yum -y install ntp
#启用 ntpd
systemctl enable ntpd
#检查 NTP 的状态
systemctl status ntpd
firewall-cmd --permanent --zone=public --add-port=123/udp
# 设置系统时区
timedatectl set-timezone Asia/Shanghai
# yes/no "yes" 开启网络时间同步
timedatectl set-ntp yes
# yes/no "no" 表述在RTC中存储UTC时间
timedatectl set-local-rtc no
2.3 检查 NTP 的状态
要检查 ntpd
是否在系统启动时运行并配置为在系统启动时运行,以 root 身份运行以下命令:
systemctl status ntpd
要从 ntpd
获取简短状态报告,以 root 身份运行以下命令:
~]$ ntpstat
unsynchronised
time server re-starting
polling server every 64 s
因为轮询时间间隔的关系,上一步执行返回结果可能是“unsynchronised”,需几分钟后,需多次以 root 身份运行以下命令:
~]$ ntpstat
synchronised to NTP server (10.5.26.10) at stratum 2
time correct to within 52 ms
polling server every 1024 s
2.4 配置硬件时钟更新
系统时钟可用于更新硬件时钟,也称为实时时钟(RTC)。本节演示了任务的三种方法:2.4.1
2.4.1 即时一次性更新
要对硬件时钟进行即时一次性更新,以 root 用户身份运行这个命令:
hwclock --systohc
2.4.2 在每次引导时更新
要在执行 ntpdate 同步程序后在每次引导时更新硬件时钟,请执行以下操作:
a.在 /etc/sysconfig/ntpdate
文件中添加以下行:
SYNC_HWCLOCK=yes
b.以 root 用户身份启用 ntpdate
服务:
systemctl enable ntpdate.service
请注意,nt date
服务使用 /etc/ntp/step-tickers
文件中定义的 NTP 服务器。
2.4.3 通过 NTP 更新
每次由 ntpd
或 chronyd
服务更新系统时钟时,您可以更新硬件时钟:
以 root 用户身份自启动 ntpd
服务:
systemctl start ntpd.service
systemctl enable ntpd.service
因此,每次由 ntpd
或 chronyd
同步系统时钟时,内核会在 11 分钟内自动更新硬件时钟。
要检查软件时钟与硬件时钟的同步,以 root
用户身份使用 ntpdc -c kerninfo
或 ntptime
命令:
ntpdc -c kerninfo
结果可能类似如下:
pll offset: 0 s
pll frequency: 0.000 ppm
maximum error: 8.0185 s
estimated error: 0 s
status: 2001 pll nano
pll time constant: 6
precision: 1e-09 s
frequency tolerance: 500 ppm
或者
ntptime
结果可能类似如下:
ntp_gettime() returns code 0 (OK)
time dcba5798.c3dfe2e0 Mon, May 8 2017 11:34:00.765, (.765135199),
maximum error 8010000 us, estimated error 0 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
modes 0x0 (),
offset 0.000 us, frequency 0.000 ppm, interval 1 s,
maximum error 8010000 us, estimated error 0 us,
status 0x2001 (PLL,NANO),
time constant 6, precision 0.001 us, tolerance 500 ppm,
要识别硬件时钟是否与系统时钟同步,请查看输出中的状态行。如果该行包含单词 unsync 或
UNSYNC
,则硬件时钟不会与系统时钟同步。
硬件时钟与系统时钟同步。
status 0x2001 (PLL,NANO)
硬件时钟未与系统时钟同步。
status 0x41 (PLL,UNSYNC)
2.5 查验时间是否同步
# 设置系统时区
timedatectl set-timezone Asia/Shanghai
# yes/no "yes" 开启网络时间同步,相当于执行 systemctl enable --now systemd-timesyncd.service
timedatectl set-ntp yes
# yes/no "no" 表述在RTC中存储UTC时间
timedatectl set-local-rtc no
timedatectl show
#或
timedatectl
三、systemd-timesyncd
systemd-timesyncd 时间同步方式一般是在debian 和Ubuntu 系统中被使用 timesyncd 开启NTP时间同步,并替换为ntpd步进式的逐渐校正时间
3.1 修改timesyncd服务配置文件
timesyncd是systemd系统中的一个守护进程,在系统启动时或 服务重启时读取配置文件。文件的默认位置为 /etc/systemd/timesyncd.conf
文件的默认内容是这样的:
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating "drop-ins" in
# the timesyncd.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
#
# See timesyncd.conf(5) for details.
[Time]
#主用NTP服务器
#NTP=
#备用NTP服务器
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#最大可接受的"root distance"秒数(最大误差)。 默认值为 5 秒。
#RootDistanceMaxSec=5
#NTP消息的最小轮询间隔秒数。
#PollIntervalMinSec=32
#NTP消息的最大轮询间隔秒数。
#PollIntervalMaxSec=2048
# 30秒后连接重试
#ConnectionRetrySec=30
#SaveIntervalSec=60
把 [Time]
下的注释取消掉。NTP
为主时间同步服务器修改为 网络内部的 NTP 服务器地址,可以是域名,也可以是NTP服务器的IP地址,FallbackNTP
为备用服务器。
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating "drop-ins" in
# the timesyncd.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
#
# See timesyncd.conf(5) for details.
[Time]
# 填入自定的 NTP 服务器地址,如下
NTP=cn.ntp.org.cn 192.168.100.100
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
#ConnectionRetrySec=30
#SaveIntervalSec=60
3.2 设置开机启动、启动/重启服务
systemctl enable systemd-timesyncd.service
systemctl start systemd-timesyncd.service
systemctl restart systemd-timesyncd.service
systemctl status systemd-timesyncd.service
注意:systemd-timesyncd 和其他NTP服务会有冲突,需要禁用其他NTP服务
3.3 查验时间是否同步
# 设置系统时区
timedatectl set-timezone Asia/Shanghai
# yes/no "yes" 开启网络时间同步,相当于执行 systemctl enable --now systemd-timesyncd.service
timedatectl set-ntp yes
# yes/no "no" 表述在RTC中存储UTC时间
timedatectl set-local-rtc no
timedatectl show
#或
timedatectl
------------------------------------------------------
四、 四种网络对时的优缺点
网络对时方式 | 特点 |
ntpdate | 必须和cronb配合使用,会产生时间跳变 |
systemd-timesyncd | 是断点式更新时间,也就是时间不同立即更新,这样会对某些服务产生影响,所以在生产环境尽量不要用。 |
ntpd | ntpd有一个自我保护设置: 如果本机与上源时间相差太大, ntpd不运行. 所以新设置的时间服务器一定要先ntpdate从上源取得时间初值, 然后启动ntpd服务。ntpd服务运行后, 先是每64秒与上源服务器同步一次, 根据每次同步时测得的误差值经复杂计算逐步调整自己的时间, 随着误差减小, 逐步增加同步的间隔. 每次跳动, 都会重复这个调整的过程. |
chronyd | chrony也是一种网络时间同步服务,它可以精确地测量和同步时间,并自动调整系统时钟偏差。与NTP不同,chrony使用较小的时钟跳变,并且可以在网络连接不稳定或不可靠的情况下以更快的速度进行同步。 |
五、参考链接:
chrony官网 https://chrony-project.org/
NTP Documentation https://www.ntp.org/documentation/