利用chrony和ntp搭建時間同步服務器


利用chrony和ntp搭建時間同步服務器

環境說明

系統版本    CentOS 6.9 x86_64

Network Time Protocol(NTP,網絡時間協議)用於同步它所有客戶端時鍾的服務。NTP服務器將本地系統的時鍾與一個公共的NTP服務器同步然后作為時間主機提供服務,使本地網絡的所有客戶端能同步時鍾。

同步時鍾最大的好處就是相關系統上日志文件中的數據,如果網絡中使用中央日志主機集中管理日志,得到的日志結果就更能反映真實情況。在同步了時鍾的網絡中,集中式的性能監控、服務監控系統能實時的反應系統信息,系統管理員可以快速的檢測和解決系統錯誤。

利用chrony搭建步驟

1、安裝

  1. [root@m01 ~]# yum install chrony –y  

 

2、配置

修改默認配置,設置上游時間服務器地址、允許同步時間的客戶端ip地址范圍

  1. [root@m01 ~]# vim /etc/chrony.conf  
  2. # Use public servers from the pool.ntp.org project.  
  3. # Please consider joining the pool (http://www.pool.ntp.org/join.html).  
  4. server ntp1.aliyun.com iburst       #上游時間服務器的地址  
  5. server ntp2.aliyun.com iburst  
  6. server ntp3.aliyun.com iburst  
  7. server ntp4.aliyun.com iburst  
  8. # Ignore stratum in source selection.  
  9. stratumweight 0  
  10.     
  11. # Record the rate at which the system clock gains/losses time.  
  12. driftfile /var/lib/chrony/drift  
  13.     
  14. # In first three updates step the system clock instead of slew  
  15. # if the adjustment is larger than 10 seconds.  
  16. makestep 10 3  
  17.     
  18. # Enable kernel synchronization of the real-time clock (RTC).  
  19. rtcsync  
  20.     
  21. # Allow NTP client access from local network.  
  22. allow 192.168.1.0/24            #允許同步時間的ip范圍  

 

3、啟動

  1. [root@m01 ~]# /etc/init.d/chronyd start  
  2. Starting chronyd:                                          [  OK  ]  
  3. [root@m01 ~]# netstat -lntup|grep 123       #本地會啟用123端口  
  4. udp        0      0 0.0.0.0:123                 0.0.0.0:*                               21133/chronyd  

 

4、測試

4.1、服務端測試

修改時間同步服務器的時間,等待片刻再查看時間

  1. [root@m01 ~]# date -s 20170514  
  2. Sun May 14 00:00:00 CST 2017  
  3. [root@m01 ~]# date  
  4. Tue May 15 17:52:39 CST 2018  

4.2、在其他機器測試

修改db01的時間,執行命令ntpdate 192.168.1.61(即自己搭建的時間同步服務器的內網ip)同步時間

  1. [root@db01 ~]# date -s 20170501  
  2. Mon May  1 00:00:00 CST 2017  
  3. [root@db01 ~]# ntpdate 192.168.1.61  
  4. 15 May 17:55:18 ntpdate[2414]: step time server 192.168.1.61 offset 32810061.803564 sec  
  5. [root@db01 ~]# date  
  6. Tue May 15 17:55:21 CST 2018  

 

利用ntp搭建步驟

1、安裝

  1. [root@m01 ~]# yum install ntp ntpdate -y

 

2、配置

  1. [root@m01 ~]# vim /etc/ntp.conf  
  2. # For more information about this file, see the man pages  
  3. # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).  
  4.     
  5. driftfile /var/lib/ntp/drift  
  6.     
  7. # Permit time synchronization with our time source, but do not  
  8. # permit the source to query or modify the service on this system.  
  9. restrict default kod nomodify notrap nopeer noquery           
  10. #restrictdefault定義默認訪問規則,nomodify禁止遠程主機修改本地服務器(默認即可)  
  11. restrict -6 default kod nomodify notrap nopeer noquery  
  12. #配置,notrap拒絕特殊的ntpdq捕獲消息,noquery拒絕btodq/ntpdc查詢(默認即可)  
  13. # Permit all access over the loopback interface.  This could  
  14. # be tightened as well, but to do so would effect some of  
  15. # the administrative functions.  
  16. restrict 127.0.0.1  
  17. restrict -6 ::1  
  18. restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap  
  19. #指定的192.168.1.0--192.168.1.254的服務器都可以使用ntp服務器來同步時間  
  20. server 192.168.1.61     #指定本機作為局域網中的ntp服務器  
  21. # Hosts on local network are less restricted.  
  22. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap  
  23.     
  24. # Use public servers from the pool.ntp.org project.  
  25. # Please consider joining the pool (http://www.pool.ntp.org/join.html).  
  26. server ntp1.aliyun.com iburst           #指定上游時間服務器  
  27. server ntp2.aliyun.com iburst  
  28. server ntp3.aliyun.com iburst  
  29. server ntp4.aliyun.com iburst  
  30. 后面內容省略  

 

3、啟動

  1. [root@m01 ~]# /etc/init.d/ntpd start        #啟動  
  2. Starting ntpd:                                             [  OK  ]  
  3. [root@m01 ~]# netstat -lntup|grep 123   #檢查,默認使用123端口  
  4. udp        0      0 192.168.1.61:123            0.0.0.0:*                               21345/ntpd            
  5. udp        0      0 10.0.0.61:123               0.0.0.0:*                               21345/ntpd            
  6. udp        0      0 127.0.0.1:123               0.0.0.0:*                               21345/ntpd            
  7. udp        0      0 0.0.0.0:123                 0.0.0.0:*                               21345/ntpd            
  8. udp        0      0 fe80::20c:29ff:fe1b:6ae:123 :::*                                    21345/ntpd            
  9. udp        0      0 fe80::20c:29ff:fe1b:6b8:123 :::*                                    21345/ntpd            
  10. udp        0      0 ::1:123                     :::*                                    21345/ntpd            
  11. udp        0      0 :::123                      :::*                                    21345/ntpd    

 

4、測試

4.1、服務端執行ntpq -p檢查

  1. [root@m01 ~]# ntpq -p  
  2.      remote           refid      st t when poll reach   delay   offset  jitter  
  3. ==============================================================================  
  4.  192.168.1.61    .INIT.          16 u    -   64    0    0.000    0.000   0.000  
  5. *time5.aliyun.co 10.137.38.86     2 u   59   64    3   23.174   15.665   9.655  
  6. +120.25.115.19   10.137.53.7      2 u   58   64    3   52.023   14.674   9.927  
  7. +203.107.6.88    10.137.55.181    2 u   57   64    3   39.007    4.241   0.497  

當所有遠程服務器(不是本地服務器)的jitter值都為4000,並且reach和dalay的值是0時,就表示時間同步有問題。可能原因有2個:

  1)服務器端的防火牆設置,阻斷了123端口(可以用 iptables -t filter -A INPUT -p udp --destination-port 123 -j ACCEPT 解決)

2)每次重啟ntp服務器之后,大約3-5分鍾客戶端才能與服務端建立連接,建立連接之后才能進行時間同步,否則客戶端同步時間時會顯示

若遇到no server suitable for synchronization found的報錯信息,稍等片刻再測試即可

4.2、在其他機器測試

修改db01的時間,執行命令ntpdate 192.168.1.61(即自己搭建的時間同步服務器的內網ip)同步時間

  1. [root@db01 ~]# date -s 20170501  
  2. Mon May  1 00:00:00 CST 2017  
  3. [root@db01 ~]# ntpdate 192.168.1.61  
  4. 15 May 17:55:18 ntpdate[2414]: step time server 192.168.1.61 offset 32810061.803564 sec  
  5. [root@db01 ~]# date  
  6. Tue May 15 17:55:21 CST 2018  

 

 

博主原創文章,轉載請務必注明出處


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM