1. 程式人生 > >Linux服務器時間同步配置

Linux服務器時間同步配置

時間同步 時間服務器

以CentOS7 做時間服務器,其他服務器(Centos 6、RHEL7)同步該服務器時間

RHEL 7、CentOS 7 默認的網絡時間協議 為Chrony

本教程時間服務器服務端主機名:CentOS7-Node3 IP:192.168.231.130

客戶端1主機名:RHEL7-Node1

客戶端2主機名:CentOS6-Node2

服務端:

先查詢下chrony服務狀態

[root@CentOS7-Node3 ~]# systemctl status chronyd
Unit chronyd.service could not be found.

如果查詢沒有該服務,可yum安裝

[root@CentOS7-Node3 ~]# yum install -y chrony
Loaded plugins: fastestmirror
base                                                                                                          | 3.6 kB  00:00:00     
extras                                                                                                        | 3.4 kB  00:00:00     
updates                                                                                                       | 3.4 kB  00:00:00     
Loading mirror speeds from cached hostfile
--------------------------------------------------------------省略部分過程-----------------------------------------------------------------------
Running transaction
  Installing : chrony-3.2-2.el7.x86_64                                                                                           1/1 
  Verifying  : chrony-3.2-2.el7.x86_64                                                                                           1/1 

Installed:
  chrony.x86_64 0:3.2-2.el7                                                                                                          

Complete!

啟動chrony服務

[root@CentOS7-Node3 ~]# systemctl start chronyd

查詢狀態

[root@CentOS7-Node3 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-06-07 10:20:28 CST; 2s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 6031 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 6027 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 6029 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─6029 /usr/sbin/chronyd

Jun 07 10:20:28 CentOS7-Node3 systemd[1]: Starting NTP client/server...
Jun 07 10:20:28 CentOS7-Node3 chronyd[6029]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +...DEBUG)
Jun 07 10:20:28 CentOS7-Node3 chronyd[6029]: Initial frequency -29.914 ppm
Jun 07 10:20:28 CentOS7-Node3 systemd[1]: Started NTP client/server.
Jun 07 10:20:33 CentOS7-Node3 chronyd[6029]: Selected source 37.218.240.198
Jun 07 10:20:33 CentOS7-Node3 chronyd[6029]: System clock wrong by -3.020089 seconds, adjustment started
Jun 07 10:20:30 CentOS7-Node3 chronyd[6029]: System clock was stepped by -3.020089 seconds
Hint: Some lines were ellipsized, use -l to show in full.

作為時間服務器做如下配置:

 [root@CentOS7-Node3 ~]# vim /etc/chrony.conf
25 # Allow NTP client access from local network.
26 allow 192.168.231.0/24
27 
28 # Serve time even if not synchronized to a time source.
29 local stratum 10


三臺服務器時間,CentOS7-Node3為正常時間

技術分享圖片


客戶端配置:

客戶端1:

將 server 0.rhel.pool.ntp.org iburst ---server 3.rhel.pool.ntp.org iburst 刪除或註釋

新增\修改配置:

server 192.168.231.130 iburt

allow 192.168.231.0/24

[root@RHEL7-Node1 ~]# vim /etc/chrony.conf
  1 # Use public servers from the pool.ntp.org project.  
  2 # Please consider joining the pool (http://www.pool.ntp.org/join.html).  
  3 #server 0.rhel.pool.ntp.org iburst  
  4 #server 1.rhel.pool.ntp.org iburst  
  5 #server 2.rhel.pool.ntp.org iburst  
  6 #server 3.rhel.pool.ntp.org iburst  
  7 server 192.168.231.130 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 # Enable kernel RTC synchronization. 
  15 rtcsync 
  16  
  17 # In first three updates step the system clock instead of slew 
  18 # if the adjustment is larger than 10 seconds. 
  19 makestep 10 3 
  20  
  21 # Allow NTP client access from local network. 
  22 allow 192.168.231.0/24

修改完成後,保存,重啟chrony服務即可生效

[root@RHEL7-Node1 ~]# systemctl restart chronyd


客戶端2 CentOS6-Node2

CentOS 6 為NTP

執行 ntpdate 192.168.231.130

[root@CentOS6-Node2 ~]# ntpdate 192.168.231.130
 7 Jun 16:59:34 ntpdate[1563]: the NTP socket is in use, exiting

若出現上述錯誤,將ntp服務停止,再執行ntpdate 192.168.231.130

[root@CentOS6-Node2 ~]# service ntpd stop
Shutting down ntpd:                                        [  OK  ]
[root@CentOS6-Node2 ~]# ntpdate 192.168.231.130
 7 Jun 17:00:19 ntpdate[1578]: step time server 192.168.231.130 offset -13589998.9
54539 sec[root@CentOS6-Node2 ~]# service ntpd status
ntpd is stopped
[root@CentOS6-Node2 ~]# date
Thu Jun  7 17:00:37 CST 2018

隨後在計劃任務中添加時間同步

[root@CentOS6-Node2 ~]# crontab -e
0-59/10 * * * * /usr/sbin/ntpdate 192.168.231.130

10分鐘同步一次


現在查看下三臺服務器時間是一致的了

技術分享圖片








Linux服務器時間同步配置