萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 時間同步實戰:rdate、ntpdate、chrony、systemd-timesyncd 四大方案對比

從簡單 rdate 到企業級 chrony,打造精準可靠的時間同步體系

時間同步是分散式系統、日誌分析、安全審計、資料庫複寫的基石。本文對比四種主流方案,提供生產環境部署最佳實踐。

一、rdate:簡單但過時的 RFC 868 協定

# 安裝
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:NTP 客戶端單次同步(已棄用)

ntpdate -u pool.ntp.org
# -u 使用非特權埠(穿透防火牆)

狀態: ntpdate 官方已停止維護,建議改用 ntpd -gq 或 chronyd -q。

三、ntpd:傳統 NTP 守護程序

安裝與配置(/etc/ntp.conf)

# 指定上游伺服器(優選就近、多源)
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

四、chronyd:現代首選(RHEL 7+/CentOS 7+ 預設)

優勢

  • 啟動極快,適合虛擬機/容器頻繁遷移
  • 網路斷斷續續仍能維持精度
  • 支援硬體時間戳(PTP/NIC 硬體協助)
  • 記憶體佔用極低
  • 安全性更好(預設 drop root 權限、支援 NTS)

配置(/etc/chrony.conf)

# 上游來源
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 資料

五、systemd-timesyncd:輕量級 SNTP 客戶端

適合桌面、容器、簡單伺服器(不提供 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 伺服器架構

# 第一層:核心 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

七、硬體時鐘 (RTC/HWCLOCK) 管理

# 查看硬體時鐘
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

九、安全性:NTS (Network Time Security)

# 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開箱即用、預設安全
容器/桌面/輕量 VMsystemd-timesyncd極簡、整合 systemd、無需額外套件
舊版系統維護ntpd相容既有配置
臨時手動同步chronyd -q / ntpdate -u一次性同步
copyright © 萬盛學電腦網 all rights reserved