1. 程式人生 > 實用技巧 >時間同步服務

時間同步服務

chrony介紹

chrony 的優勢:

  • 更快的同步只需要數分鐘而非數小時時間,從而最大程度減少了時間和頻率誤差,對於並非全天 24 小時執行的虛擬計算機而言非常有用
  • 能夠更好地響應時鐘頻率的快速變化,對於具備不穩定時鐘的虛擬機器或導致時鐘頻率發生變化的節 能技術而言非常有用
  • 在初始同步後,它不會停止時鐘,以防對需要系統時間保持單調的應用程式造成影響
  • 在應對臨時非對稱延遲時(例如,在大規模下載造成連結飽和時)提供了更好的穩定性
  • 無需對伺服器進行定期輪詢,因此具備間歇性網路連線的系統仍然可以快速同步時鐘

chrony官網:https://chrony.tuxfamily.org

chrony官方文件:https://chrony.tuxfamily.org/documentation.html

chrony 檔案組成

包:chrony

兩個主要程式:chronyd和chronyc chronyd:

  • 後臺執行的守護程序,用於調整核心中執行的系統時鐘和時鐘伺服器同步。它確定計算 機增減時間的比率,並對此進行補償
  • chronyc:命令列使用者工具,用於監控效能並進行多樣化的配置。它可以在chronyd例項控制的計 算機上工作,也可在一臺不同的遠端計算機上工作
  • 服務unit 檔案: /usr/lib/systemd/system/chronyd.service

監聽埠: 服務端: 123/udp,客戶端: 323/udp

配置檔案: /etc/chrony.conf

安裝包:

yum -y install
chrony

啟動服務並開機自啟

systemctl enable --now chronyd.service

更改配置檔案

[root@CentOS8-8 ~]# vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server ntp.aliyun.com iburst     #設定阿里時間同步
server time1.cloud.tencent.com iburst
server 
210.72.145.44 iburst # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift # Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3 # Enable kernel synchronization of the real-time clock (RTC). rtcsync # Enable hardware timestamping on all interfaces that support it. #hwtimestamp * # Increase the minimum number of selectable sources required to adjust # the system clock. #minsources 2 # Allow NTP client access from local network. allow 10.0.0.0/24 #作為公司內部伺服器更改為同一網段 如果是客戶端不需要更改 # Serve time even if not synchronized to a time source. local stratum 10 #作為伺服器端更改

檢視時間同步詳細資訊

chronyc sources -v

將時間改錯,稍等幾分鐘會自動同步

date -s '-1 year'

作為客戶端

[root@centos7-7 ~]# vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 10.0.0.8 iburst  #指定服務地址

注意:重啟服務

systemctl restart chronyd