????????在 CentOS 9 中,默認的時間同步服務是 chrony,而不是傳統的 ntpd。 因此,建議使用 chrony 來配置和管理時間同步。 以下是使用 chrony 配置 NTP 服務的步驟:
1. 安裝 chrony
首先,確保系統已安裝 chrony。 在 CentOS 9 中,chrony 通常已預安裝。 如果未安裝,可以使用以下命令進行安裝:
sudo dnf install -y chrony
2. 啟動并啟用 chronyd 服務
安裝完成后,啟動 chronyd 服務,并設置為開機自啟:
sudo systemctl enable --now chronyd
3. 配置 NTP 服務器
編輯 /etc/chrony.conf 配置文件,添加或修改 NTP 服務器地址。 例如,使用阿里云的 NTP 服務器:
sudo vi /etc/chrony.conf
在文件中找到以下行:
ini# Use public servers from the pool.ntp.org project.# Please consider joining the pool (https://www.pool.ntp.org/join.html).#pool 2.centos.pool.ntp.org iburst#pool 2.centos.pool.ntp.org iburst#pool 2.centos.pool.ntp.org iburst#pool 2.centos.pool.ntp.org iburst
將其替換為:
ini# Use public servers from the pool.ntp.org project.# Please consider joining the pool (https://www.pool.ntp.org/join.html).server ntp1.aliyun.com iburstserver ntp2.aliyun.com iburstserver ntp3.aliyun.com iburstserver ntp4.aliyun.com iburstserver ntp5.aliyun.com iburstserver ntp6.aliyun.com iburst
保存并退出編輯器。
4. 重啟 chronyd 服務
修改配置后,重啟 chronyd 服務以使更改生效:
sudo systemctl restart chronyd
5. 驗證時間同步狀態
使用以下命令檢查時間同步狀態:
chronyc tracking
如果輸出中顯示 System clock synchronized: yes,則表示時間同步成功。
此外,您還可以使用以下命令查看 NTP 服務器的同步狀態:
chronyc sources
如果輸出中有 ^* 標記的服務器,表示該服務器正在被使用進行時間同步。
6. 設置時區
如果需要設置時區,可以使用 timedatectl 命令:
sudo timedatectl set-timezone Asia/Shanghai
請根據您的實際時區進行調整。
通過以上步驟,您可以在 CentOS 9 上成功配置 NTP 服務,確保系統時間的準確性。