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修改系統時間兩步走:date + hwclock

編輯:Linux 教學

Linux系統時間分為系統時間(核心維護,精度高)與硬體時間/RTC(主板CMOS晶片,斷電靠電池維持)。修改時間需同步更新兩者。

標準兩步驟

# 1. 設定系統時間 (格式: MMDDhhmmYYYY)
date 062111032024   # 6月21日 11:03 2024年

# 2. 寫入硬體時鐘
hwclock -w          # --systohc 同義

參數詳解

命令關鍵參數說明
dateMMDDhhmm[[CC]YY][.ss]月日時分[世紀年][.秒]
date-s "2024-06-21 11:03:00"人類可讀格式(推薦)
hwclock-w / --systohc系統時間 → 硬體時鐘
hwclock-s / --hctosys硬體時鐘 → 系統時間 (開機自動執行)
hwclock--utc / --localtime指定RTC存儲時區模式
hwclock--show顯示硬體時鐘時間

常見場景

  1. 手動校時:date -s "$(curl -s --head https://www.google.com | grep ^Date: | sed 's/Date: //')" 從HTTP頭同步網路時間,再hwclock -w。
  2. NTP自動同步(生產環境標準做法):
    yum install chrony -y
    systemctl enable --now chronyd
    # chrony自動同步系統時間並定期寫入RTC
    
  3. 時區調整:
    timedatectl set-timezone Asia/Taipei
    # 或 ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime
    
  4. 虛擬機時間漂移:安裝chrony或vmware-tools/open-vm-tools啟用時間同步。

驗證

date; hwclock --show
# 輸出應一致(時區相同情況下)

切記:單獨執行date重啟後失效;單獨hwclock --set系統時間不變。必須雙雙執行或用NTP/chrony自動管理。

Copyright © Windows教程網 All Rights Reserved