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

Linux 伺服器時間不準/跑快完整解決方案

編輯:關於伺服器

快速同步指令(單次校時)

# 使用阿里雲/國家授時中心 NTP 伺服器
ntpdate -u 0.asia.pool.ntp.org
# 或
ntpdate -u time.pool.aliyun.com

永久解決:部署 NTP 服務自動同步

1. 安裝 NTP 服務

yum -y install ntp  # CentOS/RHEL
apt-get install ntp  # Debian/Ubuntu

2. 配置 /etc/ntp.conf

# 優先使用亞洲池,添加 prefer 標記
server 0.asia.pool.ntp.org prefer
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org
server 210.72.145.44  # 國家授時中心

# 允許區網機器同步(可選)
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

3. 啟用並設定開機自啟

systemctl enable ntpd --now
# 或舊版
chkconfig --level 35 ntpd on
service ntpd start

4. 驗證同步狀態

ntpq -p
# 輸出中 * 表示當前同步源,+ 表示候選源
# reach 377 表示連續 8 次輪詢成功

時區設定(解決「時間正確但顯示錯誤」)

方法 1:複製時區檔(最直接)

cp -f /usr/share/zoneinfo/Asia/Taipei /etc/localtime
# 或上海/北京
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

方法 2:互動式工具

# CentOS/RHEL
timedatectl set-timezone Asia/Taipei
# 或
tzselect

# Debian/Ubuntu
dpkg-reconfigure tzdata

方法 3:舊版指令

timeconfig  # 僅限 RedHat/CentOS 6 以前

硬體時鐘同步(防重開機失效)

# 將系統時間寫入 BIOS
hwclock -w

# 查看硬體時鐘
hwclock -r

定時同步排程(備援方案)

crontab -e
# 每 6 小時同步一次
0 */6 * * * /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1

常見問題排查

  • ntpdate: no server suitable:防火牆阻擋 UDP 123 埠,需開放
  • 時間倒退導致服務異常:資料庫/日誌系統敏感,建議使用 ntpd -x 平滑調整(slew mode)
  • 虛擬機時間飄移:安裝 VMware Tools / VirtIO Guest Tools,啟用宿主機時間同步
Copyright © Windows教程網 All Rights Reserved