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

CentOS 6 時區錯誤修正:手動設定與 NTP 自動同步雙重保障

編輯:關於 Linux

CentOS 6 系統時間若顯示 UTC(+0000)而非 CST(+0800),會導致定時任務、日誌分析、資料庫時間戳錯亂。本文提供手動複製時區檔與NTP 自動同步兩種方案,確保時間準確可靠。

問題確認

$ date +%z
+0000  # 應為 +0800

$ cat /etc/localtime
# 末尾顯示 UTC,確認時區檔錯誤

方案一:手動設定中國標準時間(立即生效)

# 備份原檔(可選)
cp /etc/localtime /etc/localtime.bak

# 複製上海時區檔覆蓋
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 驗證
$ date +%z
+0800
$ date
2015年 05月 11日 星期五 18:56:39 CST

方案二:配置 NTP 自動同步(長期治理)

方法 1:開機自動同步(寫入 rc.local)

vi /etc/rc.d/rc.local
# 新增一行:
/usr/sbin/ntpdate -u 192.168.0.2 192.168.0.3 192.168.0.4; /sbin/hwclock -w
  • -u:使用非特權埠(避開防火牆限制);
  • 多個 NTP 伺服器 IP 提供容錯;
  • hwclock -w:將系統時間寫入硬體時鐘(BIOS)。

方法 2:啟用 ntpd 服務持續同步(推薦生產環境)

# 安裝
yum install ntp

# 配置 /etc/ntp.conf,指定上游伺服器
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
# 或內網 NTP 伺服器
server 192.168.0.2 iburst

# 啟用並設定開機自啟
service ntpd start
chkconfig ntpd on

# 監控同步狀態
ntpq -p

CentOS 7/8 現代化做法(補充)

# timedatectl 統一管理
timedatectl set-timezone Asia/Shanghai
timedatectl set-ntp true  # 啟用 systemd-timesyncd 或 chronyd

# chrony 取代 ntp(CentOS 7 預設)
systemctl enable chronyd --now
chronyc sources -v

避坑提醒

  • 容器環境需在宿主機同步,容器共享宿主機核心時間;
  • 虛擬機建議啟用宿主機時間同步(VMware Tools / Hyper-V Integration Services / qemu-guest-agent);
  • 跨時區部署應用時,統一儲存 UTC,顯示層再轉換本地時間。

「手動修正解決當下,NTP 同步保障長久」,雙管齊下才是運維正解。

Copyright © Windows教程網 All Rights Reserved