
我正在運行一個 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先生註),兩個訊息之間很容易失去同步。
為了完整起見,恕我直言,將硬體時鐘設定為本地的唯一情況是雙重開機視窗。