時間同步是分散式系統、日誌分析、安全審計、資料庫複寫的基石。本文對比四種主流方案,提供生產環境部署最佳實踐。
# 安裝
yum install -y rdate # CentOS
apt install -y rdate # Debian/Ubuntu
# 手動同步
rdate -s time.nist.gov
rdate -s tick.greyware.com
# 定時同步(cron)
echo "0 * * * * root /usr/bin/rdate -s time.nist.gov > /dev/null 2>&1" > /etc/cron.d/rdate-sync
缺點: 精度僅秒級、無加密認證、單點故障、RFC 868 已淘汰。不建議生產環境使用。
ntpdate -u pool.ntp.org
# -u 使用非特權埠(穿透防火牆)
狀態: ntpdate 官方已停止維護,建議改用 ntpd -gq 或 chronyd -q。
# 指定上游伺服器(優選就近、多源)
server 0.pool.ntp.org iburst
server 1.pool.ntp.org iburst
server 2.pool.ntp.org iburst
server 3.pool.ntp.org iburst
# 本地參考時鐘(斷網時允許繼續服務)
server 127.127.1.0
fudge 127.127.1.0 stratum 10
# 限制存取
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
# Drift 檔案記錄頻率誤差
driftfile /var/lib/ntp/drift
# 日誌
logfile /var/log/ntp.log
systemctl enable --now ntpd
ntpq -p # 查看同步狀態,* 表示當前同步源
ntpdc -c sysinfo # 系統資訊
watch -n 1 ntpq -p
# 上游來源
pool pool.ntp.org iburst maxsources 4
# 或指定具體伺服器
# server time1.google.com iburst
# server time2.google.com iburst
# 允許區域網客戶端同步(若本機作為 NTP 伺服器)
allow 192.168.0.0/16
# allow 10.0.0.0/8
# 本地參考時鐘
local stratum 10
# Drift 檔
driftfile /var/lib/chrony/drift
# 即時時鐘 (RTC) 同步
rtcsync
# 日誌
logdir /var/log/chrony
log measurements statistics tracking
systemctl enable --now chronyd
chronyc sources -v # 詳細來源狀態
chronyc tracking # 同步精度、頻率誤差
chronyc sourcestats # 來源統計
chronyc ntpdata # 原始 NTP 資料
適合桌面、容器、簡單伺服器(不提供 NTP 服務給他人)。
# /etc/systemd/timesyncd.conf
[Time]
NTP=time1.google.com time2.google.com time3.google.com
FallbackNTP=pool.ntp.org
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048
systemctl enable --now systemd-timesyncd
timedatectl status
timedatectl timesync-status
# 第一層:核心 NTP 伺服器(2-3 台,同步外部 stratum 1/2)
# /etc/chrony.conf
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server time.google.com iburst
allow 10.0.0.0/8
# 第二層:區域 NTP 伺服器(同步核心層)
server ntp-core-1.internal iburst
server ntp-core-2.internal iburst
allow 192.168.0.0/16
# 客戶端:統一指向區域層
server ntp-region-1.internal iburst
server ntp-region-2.internal iburst
# 查看硬體時鐘
hwclock --show
# 系統時間寫入硬體時鐘(關機時自動執行)
hwclock --systohc --utc
# 硬體時鐘設為本地時間(雙系統共存時)
timedatectl set-local-rtc 1 --adjust-system-clock
# 系統時間設為 UTC(推薦)
timedatectl set-timezone Asia/Taipei
timedatectl set-ntp true
# 精度檢查(需安裝 ntpdate/chrony)
ntpdate -q pool.ntp.org # 查看 offset(毫秒級)
# 監控告警腳本
#!/bin/bash
OFFSET=$(chronyc tracking | awk '/System time/ {print $4}' | sed 's/[^0-9.-]//g')
OFFSET_ABS=${OFFSET#-}
if (( $(echo "$OFFSET_ABS > 1" | bc -l) )); then
echo "Time offset ${OFFSET}s exceeds 1s threshold" | mail -s "NTP Alert" admin@example.com
fi
# chrony 4.0+ 支援 NTS(TLS 加密 + 驗證)
# /etc/chrony.conf
server time.cloudflare.com iburst nts
server nts.ntp.se iburst nts
# 需信任 CA 憑證,通常系統 ca-certificates 已包含
| 場景 | 推薦方案 | 理由 |
|---|---|---|
| 生產伺服器叢集 | chrony(三層架構) | 高精度、穩健、支援 NTS、資源佔用低 |
| 單台獨立伺服器 | chrony | 開箱即用、預設安全 |
| 容器/桌面/輕量 VM | systemd-timesyncd | 極簡、整合 systemd、無需額外套件 |
| 舊版系統維護 | ntpd | 相容既有配置 |
| 臨時手動同步 | chronyd -q / ntpdate -u | 一次性同步 |