Linux system-timesyncd同步机制梳理

时间同步机制

种类:

ntpd作为一个成熟且广泛支持的解决方案,依然在很多环境中被使用,特别是在那些对时间同步有非常严格要求的应用中
chronyd是一个用于网络时间同步的守护进程,设计用来替代传统的 ntpd(Network Time Protocol Daemon),通过提供更快的收敛速度、更低的资源消耗和更简单的配置,成为了 ntpd 的一个有力的替代品,尤其适合那些对性能和资源敏感的场景
systemd-timesyncd 从远程服务器查询然后同步到本地时钟
断点式更新时间,也就是时间不同立即更新
systemd-timesyncd只能作为客户端,不能作为NTP服务器
ntpdate-sync 可以同步,似乎不依赖ntpd,但不及时

总之:

ntp是开发最早,使用最多,行将淘汰的同步方案
chrony优于ntp,但不逢时,遇到了systemd-timesync
systemd-timesync是简单的同步客户端,尤其在ARM平台上使用更广泛

  • ntp与chrony两者对比(https://blog.csdn.net/qq_26572567/article/details/118198019)
另外

chrony 不是从 ntpd 分叉出来的,而是从零开始实现的。它实现了完整的 NTPv4 协议(RFC5905)的客户端和服务器模式。在企业级用户中,我们看到越来越多的用户从传统的 ntpd 切换到像 Red Hat(RHEL 7 及以后版本)和 SuSE(从 SLES 15 开始)这样的 chrony。

systemd-timesyncd 只实现了 SNTP 协议(RFC4330)的客户端模式。因此,复杂的用例无法由 systemd-timesyncd 处理。例如,SNTP 无法通过默认方式从多个源获取时间来提高精度,而 NTP 可以。因此,systemd-timesyncd 无法提供与 chrony 一样高精度的时间。

常用命令与配置

常用命令

timedatectl

Commands:
  status                   Show current time settings
  show                     Show properties of systemd-timedated
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Enable or disable network time synchronization

systemd-timesyncd Commands:
  timesync-status          Show status of systemd-timesyncd
  show-timesync            Show properties of systemd-timesyncd

systemctl

systemctl restart systemd-timesyncd
systemctl status systemd-timesyncd
systemctl stop systemd-timesyncd
systemctl start systemd-timesyncd
systemctl enable systemd-timesyncd
systemctl disable systemd-timesyncd
常用配置
服务器配置

配置文件位于:/lib/systemd/system/systemd-timesyncd.service

  • 决定是否enable与disable
    [Install]
    WantedBy=sysinit.target
    Alias=dbus-org.freedesktop.timesync1.service
  • 决定是否显示打印
    [Service]
    Environment=SYSTEMD_LOG_LEVEL=debug

stemctl restart systemd-timesyncd
journalctl -u systemd-timesyncd -f

NTP配置

配置文件位于:/etc/systemd/timesyncd.conf

  • 默认配置
[Time]
#NTP=
#FallbackNTP=time1.google.com time2.google.com time3.google.com time4.google.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
  • 常用NTP服务器
NTP=ntp.aliyun.com
FallbackNTP=time1.aliyun.com time2.aliyun.com time3.aliyun.com time4.aliyun.com


NTP=0.arch.pool.ntp.org 1.arch.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org
FallbackNTP=0.pool.ntp.org 1.pool.ntp.org 0.fr.pool.ntp.org

NTP=ntp.tencent.com
FallbackNTP=ntp1.tencent.com,ntp2.tencent.com,ntp3.tencent.com
  • 查看当前配置
    systemd-analyze cat-config systemd/timesyncd.conf

调试与测试

设置时间

方法一:
$ date MMDDhhmmYYYY 月日时分年
$ date 010203042024 如2024年1月2日3时4分

$ date -s “2022-07-29”

root@imx8mqrom5720a1:~# date
Fri Oct 21 21:23:38 CST 2016
root@imx8mqrom5720a1:~# date 010203042024 
Tue Jan  2 03:04:00 CST 2024
root@imx8mqrom5720a1:~# date
Tue Jan  2 03:04:02 CST 2024
root@imx8mqrom5720a1:~# 

root@imx8mqrom5720a1:~# date
Tue Jan  2 03:04:06 CST 2024
root@imx8mqrom5720a1:~# date -s "2022-07-29"
Fri Jul 29 00:00:00 CST 2022
root@imx8mqrom5720a1:~# date
Fri Jul 29 00:00:01 CST 2022

方法二:
$ timedatectl set-time ‘YYYY-MM-DD HH:MM:SS’
$ timedatectl set-time “2021-05-08 9:30:23”

root@imx8mqrom5720a1:~# timedatectl 
               Local time: Fri 2022-07-29 00:00:12 CST
           Universal time: Thu 2022-07-28 16:00:12 UTC
                 RTC time: Fri 2016-10-21 13:24:07
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no
root@imx8mqrom5720a1:~#  timedatectl set-time "2021-05-08 9:30:23"
root@imx8mqrom5720a1:~# timedatectl 
               Local time: Sat 2021-05-08 09:30:25 CST
           Universal time: Sat 2021-05-08 01:30:25 UTC
                 RTC time: Sat 2021-05-08 01:30:25
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

方法三:
设置硬件时间
$ hwclock --set --date “21 Oct 2016 21:17”

获取系统硬件时间
$ hwclock
Fri 23 Jan 2015 03:33:17 PM CST -0.567492 seconds

设置操作系统的软件时间,与系统硬件时间同步
$ sudo hwclock -s

设置系统硬件时间,与操作系统的软件时间同步
$ sudo hwclock -w

root@imx8mqrom5720a1:~# hwclock
2021-05-08 09:30:49.108865+08:00
root@imx8mqrom5720a1:~# hwclock --set --date "21 Oct 2016 21:17"
root@imx8mqrom5720a1:~# hwclock
2016-10-21 21:17:03.770352+08:00
root@imx8mqrom5720a1:~# date
Sat May  8 09:31:14 CST 2021
root@imx8mqrom5720a1:~#  sudo hwclock -s
root@imx8mqrom5720a1:~# date
Fri Oct 21 21:17:26 CST 2016
查看timedatectl

运行 systemd-timesyncd后,时间更新为当前实时时间

root@imx8mqrom5720a1:~# timedatectl 
               Local time: Fri 2016-10-21 21:18:40 CST
           Universal time: Fri 2016-10-21 13:18:40 UTC
                 RTC time: Fri 2016-10-21 13:18:40
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no
root@imx8mqrom5720a1:~# systemctl start systemd-timesyncd
root@imx8mqrom5720a1:~# timedatectl 
               Local time: Mon 2024-11-18 17:37:22 CST
           Universal time: Mon 2024-11-18 09:37:22 UTC
                 RTC time: Mon 2024-11-18 09:37:23
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
root@imx8mqrom5720a1:~# timedatectl timesync-status
       Server: 202.112.31.197 (0.arch.pool.ntp.org)
Poll interval: 2min 8s (min: 32s; max 34min 8s)
         Leap: normal
      Version: 4
      Stratum: 1
    Reference: BDS
    Precision: 1us (-25)
Root distance: 45us (max: 5s)
       Offset: +4.804ms
        Delay: 49.283ms
       Jitter: 4.721ms
 Packet count: 3
    Frequency: +18.767ppm
root@imx8mqrom5720a1:~# timedatectl show-timesync  
SystemNTPServers=0.arch.pool.ntp.org 1.arch.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org
FallbackNTPServers=0.pool.ntp.org 1.pool.ntp.org 0.fr.pool.ntp.org
ServerName=0.arch.pool.ntp.org
ServerAddress=202.112.31.197
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=2min 8s
NTPMessage={ Leap=0, Version=4, Mode=4, Stratum=1, Precision=-25, RootDelay=30us, RootDispersion=30us, Reference=BDS, OriginateTimestamp=Mon 2024-11-18 17:38:57 CST, ReceiveTimestamp=Mon 2024-11-18 17:38:57 CST, TransmitTimestamp=Mon 2024-11-18 17:38:57 CST, DestinationTimestamp=Mon 2024-11-18 17:38:57 CST, Ignored=no PacketCount=3, Jitter=4.721ms }
Frequency=1229919
root@imx8mqrom5720a1:~# timedatectl show         
Timezone=Asia/Shanghai
LocalRTC=no
CanNTP=yes
NTP=yes
NTPSynchronized=yes
TimeUSec=Mon 2024-11-18 17:41:25 CST
RTCTimeUSec=Mon 2024-11-18 17:41:26 CST

同步打印

同时打印信息如下,每隔poll interval间隔,获取一次数据,即同步一次

root@imx8mqrom5720a1:~#  journalctl -f |grep system
Oct 21 21:18:39 imx8mqrom5720a1 systemd[1]: Starting Time & Date Service...
Oct 21 21:18:40 imx8mqrom5720a1 systemd[1]: Started Time & Date Service.
Oct 21 21:19:01 imx8mqrom5720a1 crond[374]: (*system*) RELOAD (/etc/crontab)
Oct 21 21:19:03 imx8mqrom5720a1 systemd[1]: Created slice system-sshd.slice.
Oct 21 21:19:03 imx8mqrom5720a1 systemd[1]: Started OpenSSH Per-Connection Daemon (192.168.2.103:58640).
Oct 21 21:19:05 imx8mqrom5720a1 systemd-logind[464]: New session c3 of user root.
Oct 21 21:19:05 imx8mqrom5720a1 systemd[1]: Started Session c3 of user root.
Oct 21 21:19:10 imx8mqrom5720a1 systemd[1]: systemd-timedated.service: Succeeded.
Oct 21 21:19:22 imx8mqrom5720a1 systemd[1]: Starting Network Time Synchronization...
Oct 21 21:19:23 imx8mqrom5720a1 systemd-timesyncd[843]: System clock time unset or jumped backwards, restoring from recorded timestamp: Mon 2024-11-18 16:23:47 CST
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Bus bus-api-timesync: changing state UNSET → OPENING
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Bus bus-api-timesync: changing state OPENING → AUTHENTICATING
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Added new server 0.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Added new server 1.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Added new server 2.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Added new server 3.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Added new server 0.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Added new server 1.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Added new server 0.fr.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: systemd-timesyncd running as pid 843
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Selected server 0.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd[1]: Started Network Time Synchronization.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Resolving 0.arch.pool.ntp.org...
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Bus bus-api-timesync: changing state AUTHENTICATING → HELLO
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Sent message type=method_call sender=n/a destination=org.freedesktop.DBus path=/org/freedesktop/DBus interface=org.freedesktop.DBus member=Hello cookie=1 reply_cookie=0 signature=n/a error-name=n/a error-message=n/a
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Sent message type=method_call sender=n/a destination=org.freedesktop.DBus path=/org/freedesktop/DBus interface=org.freedesktop.DBus member=RequestName cookie=2 reply_cookie=0 signature=su error-name=n/a error-message=n/a
Nov 18 16:23:47 imx8mqrom5720a1 systemd[1]: Reached target System Time Set.
Nov 18 16:23:47 imx8mqrom5720a1 systemd[1]: Reached target System Time Synchronized.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Got message type=method_return sender=org.freedesktop.DBus destination=:1.41 path=n/a interface=n/a member=n/a cookie=1 reply_cookie=1 signature=s error-name=n/a error-message=n/a
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Bus bus-api-timesync: changing state HELLO → RUNNING
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Got message type=signal sender=org.freedesktop.DBus.Local destination=n/a path=/org/freedesktop/DBus/Local interface=org.freedesktop.DBus.Local member=Connected cookie=4294967295 reply_cookie=0 signature=n/a error-name=n/a error-message=n/a
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Got message type=signal sender=org.freedesktop.DBus destination=:1.41 path=/org/freedesktop/DBus interface=org.freedesktop.DBus member=NameAcquired cookie=2 reply_cookie=0 signature=s error-name=n/a error-message=n/a
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Got message type=signal sender=org.freedesktop.DBus destination=:1.41 path=/org/freedesktop/DBus interface=org.freedesktop.DBus member=NameAcquired cookie=3 reply_cookie=0 signature=s error-name=n/a error-message=n/a
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Got message type=method_return sender=org.freedesktop.DBus destination=:1.41 path=n/a interface=n/a member=n/a cookie=4 reply_cookie=2 signature=u error-name=n/a error-message=n/a
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Successfully acquired requested service name.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Resolved address 78.46.102.180:123 for 0.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Resolved address 202.112.31.197:123 for 0.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Resolved address 84.16.73.33:123 for 0.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Resolved address 193.182.111.12:123 for 0.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Selected address 78.46.102.180:123 of server 0.arch.pool.ntp.org.
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Connecting to time server 78.46.102.180:123 (0.arch.pool.ntp.org).
Nov 18 16:23:47 imx8mqrom5720a1 systemd-timesyncd[843]: Sent NTP request to 78.46.102.180:123 (0.arch.pool.ntp.org).
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]: Timed out waiting for reply from 78.46.102.180:123 (0.arch.pool.ntp.org).
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]: Selected address 202.112.31.197:123 of server 0.arch.pool.ntp.org.
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]: Connecting to time server 202.112.31.197:123 (0.arch.pool.ntp.org).
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]: Sent NTP request to 202.112.31.197:123 (0.arch.pool.ntp.org).
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]: NTP response:
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   leap         : 0
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   version      : 4
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   mode         : 4
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   stratum      : 1
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   precision    : 0.000000 sec (-25)
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   root distance: 0.000046 sec
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   reference    : BDS
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   origin       : 1731918237.364
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   receive      : 1731922641.056
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   transmit     : 1731922641.056
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   dest         : 1731918237.410
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   offset       : +4403.669 sec
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   delay        : +0.047 sec
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   packet count : 1
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   jitter       : 0.000
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   poll interval: 32
Nov 18 16:23:57 imx8mqrom5720a1 systemd-timesyncd[843]:   adjust (jump): +4403.669 sec
Nov 18 17:37:21 imx8mqrom5720a1 systemd-timesyncd[843]:   status       : 8192 sync
Nov 18 17:37:21 imx8mqrom5720a1 systemd-timesyncd[843]:   time now     : 1731922641.080
Nov 18 17:37:21 imx8mqrom5720a1 systemd-timesyncd[843]:   constant     : 2
Nov 18 17:37:21 imx8mqrom5720a1 systemd-timesyncd[843]:   offset       : +0.000 sec
Nov 18 17:37:21 imx8mqrom5720a1 systemd-timesyncd[843]:   freq offset  : +0 (+0 ppm)
Nov 18 17:37:21 imx8mqrom5720a1 systemd-timesyncd[843]: interval/delta/delay/jitter/drift 32s/+4403.669s/0.047s/0.000s/+0ppm
Nov 18 17:37:21 imx8mqrom5720a1 systemd-timesyncd[843]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/timesync1 interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=3 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a
Nov 18 17:37:21 imx8mqrom5720a1 systemd-timesyncd[843]: Synchronized to time server for the first time 202.112.31.197:123 (0.arch.pool.ntp.org).
Nov 18 17:37:21 imx8mqrom5720a1 systemd[1]: Starting Rotate log files...
Nov 18 17:37:21 imx8mqrom5720a1 systemd[1]: logrotate.service: Succeeded.
Nov 18 17:37:21 imx8mqrom5720a1 systemd[1]: Started Rotate log files.
Nov 18 17:37:22 imx8mqrom5720a1 systemd[1]: Starting Time & Date Service...
Nov 18 17:37:22 imx8mqrom5720a1 systemd[1]: Started Time & Date Service.
Nov 18 17:37:49 imx8mqrom5720a1 crond[374]: (*system*) RELOAD (/etc/crontab)
Nov 18 17:37:52 imx8mqrom5720a1 systemd[1]: systemd-timedated.service: Succeeded.
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]: Sent NTP request to 202.112.31.197:123 (0.arch.pool.ntp.org).
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]: NTP response:
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   leap         : 0
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   version      : 4
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   mode         : 4
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   stratum      : 1
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   precision    : 0.000000 sec (-25)
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   root distance: 0.000046 sec
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   reference    : BDS
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   origin       : 1731922673.283
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   receive      : 1731922673.313
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   transmit     : 1731922673.313
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   dest         : 1731922673.342
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   offset       : +0.001 sec
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   delay        : +0.059 sec
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   packet count : 2
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   jitter       : 0.000
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   poll interval: 64
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   adjust (slew): +0.001 sec
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   status       : 8193 sync
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   time now     : 1731922673.343
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   constant     : 2
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   offset       : +0.001 sec
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]:   freq offset  : +0 (+0 ppm)
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]: interval/delta/delay/jitter/drift 64s/+0.001s/0.059s/0.000s/+0ppm
Nov 18 17:37:53 imx8mqrom5720a1 systemd-timesyncd[843]: Sent message type=signal sender=n/a destination=n/a path=/org/freedesktop/timesync1 interface=org.freedesktop.DBus.Properties member=PropertiesChanged cookie=4 reply_cookie=0 signature=sa{sv}as error-name=n/a error-message=n/a

源码阅读

  • 源码
    https://github.com/systemd/systemd/tree/v243
    https://github.com/systemd/systemd/blob/v243/src/timesync/timesyncd-manager.c
    https://github.com/systemd/systemd/blob/v243/src/timesync/timesyncd.c

  • timesync源码
    代码实现,主要基于dbus封装接口,如果对此了解,看容易理清楚逻辑,在src/timesync目录下,主要有4个文件
    timesyncd 实现的入口
    timesyncd-bus对dbus的封装
    timesyncd-conf对配置文件的解析
    timesyncd-server对server的封装
    timesyncd-manager实现的主体逻辑

其中timesyncd.c中的run的调用关系如下

run
  manager_new
    manager_network_monitor_listen
      manager_network_event_handler-
  if(network_is_online()
    manager_connect
      manager_resolve_handler
  sd_event_loop(m->event)
/**
      if (network_is_online()) {------------------------启动时有网络
                r = manager_connect(m);
                if (r < 0)
                        return r;
        }

        r = sd_event_loop(m->event);------启动时无网络,进行循环,等待有网络,调用manager_network_event_handler
        if (r < 0)
                return log_error_errno(r, "Failed to run event loop: %m");

*

而在timesyncd-manager.c,存在如下调用关系,即最后在调用clock_adjtime同步时钟后,会在目录下生成一个
/run/systemd/timesync/synchronized


manager_network_event_handler/manager_connect/manager_resolve_handler
  manager_begin
    manager_send_request
      manager_listen_setup
        manager_receive_response
            manager_adjust_clock
              clock_adjtime
/**
        r = clock_adjtime(CLOCK_REALTIME, &tmx);
        if (r < 0)
                return -errno;

        /* If touch fails, there isn't much we can do. Maybe it'll work next time. */
        (void) touch("/var/lib/systemd/timesync/clock");
        (void) touch("/run/systemd/timesync/synchronized"); 同步成功,创建文件
**/

参考

【1】systemd-resolved 开启 debug 日志
【2】systemd-timesyncd
【3】timesyncd.conf
【4】systemd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值