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 VPS 時區設置與網路時間同步完整教程

編輯:Linux 教學

VPS 時區錯誤會導致日誌時間混亂、定時任務異常、SSL 證書驗證失效等問題。本文涵蓋時區修改、NTP 網路對時、OpenVZ 限制說明。

一、檢查當前時間與時區

date -R          # 顯示 RFC 2822 格式時間(含時區)
timedatectl      # systemd 系統查看完整狀態

二、修改時區為中國標準時間 (CST, UTC+8)

方法 1:複製時區文件(通用,含非 systemd 系統)

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

方法 2:timedatectl(systemd 系統推薦)

timedatectl set-timezone Asia/Shanghai

方法 3:tzdata 交互式配置(Debian/Ubuntu)

dpkg-reconfigure tzdata

三、網路時間同步 (NTP)

安裝 NTP 客戶端

  • CentOS/RHEL:yum -y install ntpdate ntp
  • Debian/Ubuntu:apt-get update && apt-get -y install ntpdate ntp
  • Alpine:apk add ntpdate openntpd

手動同步一次

ntpdate time.windows.com
# 或使用國內源
ntpdate ntp.aliyun.com
ntpdate cn.pool.ntp.org

啟用 NTP 守護進程持續同步

# CentOS 7+/RHEL 7+
systemctl enable --now ntpd
# 或使用 chronyd(更適合虛擬機/容器)
systemctl enable --now chronyd

# Debian/Ubuntu
systemctl enable --now ntp

四、硬件時鐘 (RTC) 處理

同步系統時間後,寫入主板 CMOS:

hwclock -w              # 系統時間 → 硬件時鐘
hwclock --systohc       # 同義
hwclock --hctosys       # 硬件時鐘 → 系統時間(啟動時自動執行)

建議設置 RTC 使用 UTC:

timedatectl set-local-rtc 0
# 或編輯 /etc/adjtime 最後一行為 UTC

五、OpenVZ 容器特殊限制

OpenVZ VPS 僅可修改時區,無法修改系統時間。 容器時間與宿主機強綁定同步。若時間偏移,需聯繫服務商處理宿主機 NTP。

六、常見問題排查

現象原因解決
同步失敗 "no server suitable"防火牆阻擋 UDP 123開放出站 UDP 123 或用 ntpdate -u 無特權埠
時間忽快忽慢虛擬機無獨立 RTC啟用 chronyd 替代 ntpd
時區修改不生效應用緩存舊時區重啟應用/容器,或設置 TZ 環境變數
Copyright © Windows教程網 All Rights Reserved