Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 硬體時鐘與系統時鐘同步完整指令:date、hwclock、ntpdate、chrony 實戰

編輯:關於 Linux

Linux 擁有兩組時鐘:**硬體時鐘 (RTC, BIOS)** 與 **系統時鐘 (Kernel)**。開機時 RTC → 系統時鐘,關機時系統時鐘 → RTC。若兩者偏差過大會導致定時任務、日誌時間戳、SSL 憑證驗證、資料庫複寫等異常。本文整理 date、hwclock/clock、ntpdate、chrony 同步指令與定時任務自動化。

指令對照表

操作date (系統時鐘)hwclock / clock (硬體時鐘)
查看datehwclock --show / clock --show
設定date --set "MM/DD/YY HH:MM"hwclock --set --date="MM/DD/YY HH:MM"
硬體 → 系統hwclock --hctosys / clock --hctosys
系統 → 硬體hwclock --systohc / clock --systohc
寫入 RTC (舊指令)clock -w (等同 --systohc)

與外部 NTP 伺服器同步

ntpdate (單次同步,舊工具)


service ntpd stop          # 必須停用 ntpd 釋放 UDP 123
ntpdate ntp.sjtu.edu.cn
# 正常輸出:adjust time server 202.120.2.101 offset -0.006107 sec
service ntpd start
chkconfig ntpd on

chrony (現代推薦,支援斷網漸進校時)


# 安裝
yum install -y chrony   # 或 apt-get install chrony

# 設定 /etc/chrony.conf
server 210.72.145.44 iburst prefer   # 中國國家授時中心
server 202.112.10.36 iburst
server ntp.sjtu.edu.cn iburst
server ntp.aliyun.com iburst
# 允許區域網客戶端同步
allow 192.168.0.0/16
local stratum 10

# 啟用
systemctl enable --now chronyd
chronyc sources -v
chronyc tracking

定時自動同步 (crontab)


crontab -e
# 每小時第 5 分同步 NTP
05 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn >/dev/null 2>&1
# 每天 17:05 將系統時鐘寫入硬體時鐘
05 17 * * * /sbin/hwclock --systohc
# 或使用 clock -w

常用 NTP 伺服器清單 (中國大陸可用)


210.72.145.44        # 中國國家授時中心
202.112.10.36        # 中國國家授時中心備用
ntp.sjtu.edu.cn      # 上海交通大學
ntp.aliyun.com       # 阿里雲
ntp.tuna.tsinghua.edu.cn # 清華 TUNA
time.apple.com       # Apple
time.windows.com     # Microsoft
pool.ntp.org         # 全球池 (自動就近)

時區設定


# 設定為上海/台北時區 (UTC+8)
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 或 timedatectl set-timezone Asia/Shanghai (systemd)
hwclock -w   # 將目前系統時間寫入 RTC (需確認 RTC 為 UTC 或本地時)

RTC 模式:UTC vs 本地時

  • timedatectl set-local-rtc 0 (預設):RTC 存 UTC,核心啟動自動換算時區,**推薦**。
  • timedatectl set-local-rtc 1:RTC 存本地時,雙系統 (Windows) 相容較好,但夏令時間切換易出問題。

驗證同步狀態


# 查看系統與硬體時間差
date; hwclock --show
# 應相差 < 1 秒

# chrony 詳細狀態
chronyc tracking
# Reference ID, Stratum, Offset, Frequency, Skew 等

時鐘同步流程圖

Copyright © Windows教程網 All Rights Reserved