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
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:使用非特權埠(避開防火牆限制);hwclock -w:將系統時間寫入硬體時鐘(BIOS)。# 安裝
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
# timedatectl 統一管理
timedatectl set-timezone Asia/Shanghai
timedatectl set-ntp true # 啟用 systemd-timesyncd 或 chronyd
# chrony 取代 ntp(CentOS 7 預設)
systemctl enable chronyd --now
chronyc sources -v
「手動修正解決當下,NTP 同步保障長久」,雙管齊下才是運維正解。
