1. NTP時間同步協議
1.1 NTP協議簡介
- NTP(Network Time Protocol,網絡時間協議)
- NTP是用來使網絡中的各個計算機時間同步的一種協議
- 可以使用nepdate命令來向服務同步時間
ntpdate 10.0.0.201
1.2 ntpd和chrony實現時間同步的方式
- ntpd實現時間同步的思路:把時間的周期縮短,也就是讓秒針轉動的速度變快,以追上時間服務器的時間。
- chrony實現時間同步的思路:也是讓時間周期縮短,但是它相比於ntpd,它是直接轉動時針,所以能更快的將時間同步。
2. chrony服務
2.1 chrony服務簡介
- chrony is a versatile implementation of the Network Time Protocol (NTP).
- It can synchronize the system clock with NTP servers, reference clocks (e.g. GPS receiver), and manual input using wristwatch and keyboard.
- It can also operate as an NTPv4 (RFC 5905) server and peer to provide a time service to other computers in the network.
2.2 程序環境
1)安裝
- 直接 yum install chrony 即可
2)監聽的端口
- 123/udp:兼容ntp服務監聽在udp的123端口上
- 323/udp:chrony服務本身監聽在udp的323端口上
3)程序結構
- 配置文件:/etc/chrony.conf
- 主程序文件:chronyd
- unit file:chronyd.service
4)工具程序
- chronyc(交互式環境)
- sources:查看連接的時間服務器
- sourcestats:查看當前時間同步是否正常
- tracking:校准時間服務器
2.3 配置文件 /etc/chrony.conf
1)配置項
- server:指明時間服務器地址
- allow NETADDRNETMASK
- allow all:允許所有 客戶端主機
- deny NETADDR/NETMASK
- deny all:拒絕所有客戶端
- bindcmdaddress:命令管理接口監聽的地址
- local stratum 10:即使自己未能通過網絡時間服務器同步到時間,也允許將本地時間作為標准時間授時給其它客戶端(建議關閉)
- rtcsync:將啟用一個內核模式,在該模式下,系統時間每11分鍾會拷貝到實時時鍾(RTC)
2)配置示例
### 指明時間服務地址 #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst server 10.0.0.201 iburst ### 根據實際時間計算出服務器增減時間的比率,然后記錄到一個文件中,在系統重啟后為系統做出最佳時間補償調整 # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift # chronyd根據需求減慢或加速時間調整, # 在某些情況下系統時鍾可能漂移過快,導致時間調整用時過長 # 該指令強制chronyd調整時期,大於某個閥值時步進調整系統時鍾 # 只有在因chronyd啟動時間超過指定的限制時(可使用負值來禁用限制)沒有更多時鍾更新時才生效 # Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3 ### 啟用內核模式,系統時間每11分鍾會拷貝到實時時鍾(RTC) # Enable kernel synchronization of the real-time clock (RTC). rtcsync ### 通過使用hwtimestamp指令啟用硬件時間戳 # 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 192.168.0.0/16 ### 設置當自己未能通過網路時間服務同步到時間,也允許將本地時間作為標准時間授時給其他客戶端(建議關閉) # Serve time even if not synchronized to a time source. #local stratum 10 # 指定包含NTP驗證密鑰的文件 # Specify file containing keys for NTP authentication. #keyfile /etc/chrony.keys # 指定日志文件的目錄 # Specify directory for log files. logdir /var/log/chrony # Select which information is logged. #log measurements statistics tracking
3. 思維導圖
- 參考鏈接:
- https://www.cnblogs.com/lizhaoxian/p/11260041.html
- https://www.centos.bz/2018/03/centos-7-chrony-設置服務器集群系統時間同步/