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 時區修改與時間同步完整指南:CentOS 7/8、Ubuntu 18.04/20.04/22.04 通用方案

編輯:Linux 教學

正確的系統時區與精準的時間同步是日誌分析、定時任務、分布式事務、安全審計的基石。本文提供 CentOS/RHEL 7/8/9、Ubuntu 18.04+ 等主流發行版的時區設置、NTP/Chrony 時間同步、硬件時鐘寫入的標準化操作流程,並解釋底層機制。

一、核心概念釐清

  • 系統時鐘:內核維護的時間 (UTC),進程見到的時間。
  • 硬件時鐘 (RTC/CMOS):主板電池供電的實時時鐘,斷電保持。
  • 時區:系統將 UTC 換算為本地時間的規則集 (/usr/share/zoneinfo/)。
  • 時間同步協議:NTP (Network Time Protocol, UDP 123) 或 Chrony (更適合虛擬機/間歇連接)。

二、CentOS / RHEL 7/8/9 (Systemd + Chrony/NTP)

方法 1:timedatectl 統一管理 (推薦,最簡單)

# 1. 列出所有时区
timedatectl list-timezones | grep Shanghai

# 2. 设置时区为中国标准时间 (CST, UTC+8)
timedatectl set-timezone Asia/Shanghai

# 3. 验证
timedatectl status
# 应显示:Time zone: Asia/Shanghai (CST, +0800)

方法 2:傳統 cp 覆蓋 /etc/localtime (兼容舊腳本)

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

時間同步:Chrony (CentOS 7/8/9 預設)

# 1. 安裝 (通常預裝)
yum install -y chrony

# 2. 配置 NTP 服務器 (編輯 /etc/chrony.conf)
# 推薦使用阿里云/騰訊雲/教育網內部源,低延遲高穩定
server ntp.aliyun.com iburst
server ntp.tencent.com iburst
server cn.ntp.org.cn iburst
# 註釋掉預設的 pool.ntp.org

# 3. 啟用並啟動
systemctl enable --now chronyd

# 4. 強制立即同步 (首次/大偏差時)
chronyc makestep

# 5. 驗證同步狀態
chronyc sources -v
chronyc tracking
# 輸出應顯示 ^* 標記的源,System time 偏差 < 1ms

舊版 NTP (若必須使用)

yum install -y ntp
# 編輯 /etc/ntp.conf 替換 server 行
systemctl enable --now ntpd
ntpdate -u ntp.aliyun.com  # 立即同步 (ntpd 啟動前)
hwclock --systohc          # 系統時寫入硬件時鐘 (UTC)
# 或 hwclock -w

三、Ubuntu 18.04 / 20.04 / 22.04 / 24.04 (Systemd + systemd-timesyncd / Chrony)

方法 1:timedatectl (通用,推薦)

timedatectl set-timezone Asia/Shanghai
timedatectl status

方法 2:交互式 tzdata 重配置 (傳統)

dpkg-reconfigure tzdata
# 界面選擇:Asia -> Shanghai

時間同步:systemd-timesyncd (輕量級預設)

# 1. 編輯 /etc/systemd/timesyncd.conf
[Time]
NTP=ntp.aliyun.com ntp.tencent.com cn.ntp.org.cn
FallbackNTP=ntp.ubuntu.com

# 2. 啟用同步
timedatectl set-ntp true
# 等價於 systemctl enable --now systemd-timesyncd

# 3. 驗證
timedatectl timesync-status
# 或 timedatectl status 觀察 "System clock synchronized: yes"

高精度/虛擬機環境:改用 Chrony

apt install -y chrony
# 配置 /etc/chrony/chrony.conf (同 CentOS)
systemctl enable --now chrony
# 禁用 timesyncd 避免衝突
systemctl disable --now systemd-timesyncd

四、硬件時鐘 (RTC) 處理:關鍵一致性步驟

原則:硬件時鐘建議存儲 **UTC** 時間,由系統根據時區換算顯示本地時間。

# 查看當前 RTC 模式
timedatectl | grep "RTC in local TZ"
# 若為 yes (存儲本地時間),建議改為 UTC:
timedatectl set-local-rtc 0

# 將當前系統時間 (已同步、正確時區) 寫入硬件時鐘 (UTC)
hwclock --systohc
# 或 hwclock -w (舊命令,相同效果)

# 驗證
hwclock --show  # 應顯示 UTC 時間
timedatectl     # Local time 應為 CST (UTC+8),RTC time 為 UTC

避坑:雙系統 (Windows + Linux) 時,Windows 預設 RTC 存本地時間。Linux 可設 timedatectl set-local-rtc 1 --adjust-system-clock 兼容,但推薦修改 Windows 注冊表使其用 UTC。

五、驗證清單 (執行完必查)

# 1. 时区正确
timedatectl | grep "Time zone"
# Time zone: Asia/Shanghai (CST, +0800)

# 2. 时间同步生效
timedatectl | grep "synchronized"
# System clock synchronized: yes
# NTP service: active

# 3. RTC 为 UTC
timedatectl | grep "RTC in local TZ"
# RTC in local TZ: no

# 4. 硬件时间合理
hwclock --show
# 与 UTC 时间一致 (误差 < 1s)

六、容器/雲主機特殊說明

  • Docker 容器:共享宿主機內核時間,**無法**在容器內修改時區/時間。宿主機設置正確即可。如需容器顯示本地時間,掛載 -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone:ro。
  • 雲主機 (AWS/Aliyun/Tencent Cloud):通常預裝 Chrony/timesyncd 並指向內部高精度 NTP 源 (如 169.254.169.123),**無需額外配置**,僅需設置時區 timedatectl set-timezone Asia/Shanghai。

總結

現代 Linux 時區/時間管理核心口訣:
用 timedatectl set-timezone Asia/Shanghai 定時區 → 用 chrony (或 timesyncd) 同時間 → 用 hwclock --systohc 寫硬件鐘 (UTC) → timedatectl status 全維驗證。

Copyright © Windows教程網 All Rights Reserved