如何使系统时钟与硬件时钟永久同步?

如何使系统时钟与硬件时钟永久同步?

我正在运行一个 cronjob,它检查服务器更新,一一安装它们,然后检查是否需要重新启动。如果需要重新启动,该脚本将启动reboot服务器。

硬件时钟在启动时使用,在使用系统时钟之前需要一分钟。

在切换到系统时钟之前,日志显示服务器晚了一个小时左右,当服务器从硬件切换到系统时钟时,日志跳转并提前了一个小时左右,这触发了 cron 守护进程,它注意到:服务器更新脚本的时间已过,请再次执行。

这会触发重新启动循环,因为该脚本仅每周执行一次。

执行会使hwclock --systohc系统时钟与硬件时钟同步,但不是永久的。重启后,时差又消失了。

我的问题是

如何使系统时钟与硬件时钟永久同步?

输出timedatectl status

      Local time: Tue 2018-08-28 12:05:37 CEST
  Universal time: Tue 2018-08-28 10:05:37 UTC
        RTC time: Tue 2018-08-28 10:05:37
       Time zone: n/a (CEST, +0200)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: yes
      DST active: yes
 Last DST change: DST began at
                  Sun 2018-03-25 01:59:59 CET
                  Sun 2018-03-25 03:00:00 CEST
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2018-10-28 02:59:59 CEST
                  Sun 2018-10-28 02:00:00 CET

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

  • CentOS Linux 版本 7.5.1804(核心)
    • 这是一个 VPS 服务器。
  • 欧洲/阿姆斯特丹

答案1

根据你的timedatectl status

  • 您的硬件时钟 (RTC) 设置为 UTC 时间 ( RTC time),
  • 您的系统设置为将硬件时钟评估为本地时间 ( RTC in local TZ)。

因此,在启动时,您的系统时钟会延迟一小时(您的时区没有夏令时)。

man hwclock

POSIX 系统与 Linux 一样,被设计为让系统时钟在 UTC 时间刻度下运行。

您可以轻松地执行更正它:

timedatectl --adjust-system-clock set-local-rtc 0

万恶的根源在于RTC不记得它所设置的TZ。请注意,该RTC time线没有 TZ 指示。当您将时间保存在硬件中并将 TZ 保存在文件中时(/etc/adjtime@spuratic先生注),两个信息之间很容易失去同步。

为了完整起见,恕我直言,将硬件时钟设置为本地的唯一情况是双引导机器视窗

相关内容