hwclock --hctosys (RTC→系統),hwclock --systohc (系統→RTC)。| 命令 | 輸出範例 | 說明 |
|---|---|---|
date | Mon Jan 15 10:30:45 CST 2024 | 系統時間 (預設本地時區) |
date -u / date --utc | Mon Jan 15 02:30:45 UTC 2024 | UTC 時間 |
date '+%Y-%m-%d %H:%M:%S %Z' | 2024-01-15 10:30:45 CST | 自訂格式 |
hwclock --show / hwclock -r | 2024-01-15 10:30:45.123456+08:00 | 硬體時鐘 (需 root) |
timedatectl | 見下表 | systemd 統一介面,含時區、NTP 狀態、RTC 模式 |
timedatectl status | 完整狀態 | 推薦日常使用 |
# 格式: MMDDhhmm[[CC]YY][.ss]
date 011510302024.00 # 2024-01-15 10:30:00
date -s "2024-01-15 10:30:45"
date --set="2024-01-15 10:30:45"
hwclock --set --date="2024-01-15 10:30:45" --localtime # 本地時間
hwclock --set --date="2024-01-15 02:30:45" --utc # UTC 時間
# 寫入後建議同步系統時鐘
hwclock --hctosys
# 設定系統時間
timedatectl set-time "2024-01-15 10:30:45"
# 設定時區
timedatectl set-timezone Asia/Taipei
# 設定 RTC 模式 (建議 UTC)
timedatectl set-local-rtc 0 # 0=UTC (推薦), 1=Local Time
# 啟用/停用 NTP 同步
timedatectl set-ntp true
timedatectl set-ntp false
tzselect
# 依提示選擇洲洲 → 國家 → 地區 → 確認
# 輸出: TZ='Asia/Taipei'; export TZ
永久生效:將輸出加入 ~/.profile 或 /etc/profile.d/timezone.sh。
timedatectl list-timezones | grep -i taipei
timedatectl set-timezone Asia/Taipei
# 方法 1:複製時區文件
cp /usr/share/zoneinfo/Asia/Taipei /etc/localtime
# 方法 2:符號連結 (推薦,套件更新自動同步)
ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime
# 方法 3:/etc/timezone (Debian/Ubuntu 偏好)
echo "Asia/Taipei" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
| 實作 | 特點 | 適用場景 | 配置文件 |
|---|---|---|---|
| chronyd | 現代標準、適應間歇網路、支援硬體時間戳、安全認證 (NTS)、systemd 整合佳 | RHEL 8+/Fedora/SUSE/Arch 預設、筆記本/行動裝置、虛擬機 | /etc/chrony.conf |
| ntpd (ntpsec) | 傳統參考實作、完整 NTPv4、複雜配置、安全審計較多 | 舊系統、特殊需求 (廣播/多播、Autokey) | /etc/ntp.conf |
| systemd-timesyncd | 極簡 SNTP 客戶端、僅同步時間不提供服務、systemd 原生、資源極少 | 容器、嵌入式、桌面、簡單客戶端 | /etc/systemd/timesyncd.conf |
# 使用 NTP 池伺服器
pool time.google.com iburst maxsources 4
pool time.cloudflare.com iburst maxsources 4
pool ntp.aliyun.com iburst maxsources 4
# 允許區域網客戶端查詢 (可選)
allow 192.168.1.0/24
# 啟用 NTS (Network Time Security) - 需伺服器支援
# nts server time.cloudflare.com iburst
# 硬體時間戳 (需 NIC/driver 支援)
# hwtimestamp *
# RTC 同步
rtcsync
# 日誌
logdir /var/log/chrony
log measurements statistics tracking
chronyc sources -v # 顯示來源詳情
chronyc sourcestats -v # 來源統計
chronyc tracking # 系統時間追蹤狀態 (偏移、頻率、殘差)
chronyc ntpdata # 原始 NTP 數據
chronyc -a makestep # 強制階躍校時 (大偏移時)
chronyc -a 'burst 4/4' # 突發測量
systemctl status chronyd
journalctl -u chronyd -f
[Time]
NTP=time.google.com time.cloudflare.com ntp.aliyun.com
FallbackNTP=time.windows.com pool.ntp.org
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048
systemctl enable --now systemd-timesyncd
timedatectl set-ntp true # 實際啟用 timesyncd
timedatectl timesync-status # 顯示同步狀態
kvm-clock (kvmclock) 為時鐘源,客體自動同步宿主。dmesg | grep -i kvm_clock。timedatectl status 檢查 NTP 同步狀態 (System clock synchronized: yes/no)。chronyc tracking / ntpq -p 檢查上游伺服器可達、stratum、偏移量。firewall-cmd --list-all / ufw status 確認 UDP 123 通訊放行。hwclock --show 比對 RTC 與系統時間差異。journalctl -u chronyd -u systemd-timesyncd -u ntpd 查看同步日誌。RealTimeIsUniversal=1) 或 Linux timedatectl set-local-rtc 1 --adjust-system-clock。時間是分散式系統的基石。統一使用 UTC、部署 chronyd、啟用 NTS、監控同步狀態,是生產環境的專業標配。