linux系列之使用chrony代替ntp同步時間

chronyd //ntp服務,是一個進程,需要百度一下,瞭解一下
使用chrony代替ntp同步時間 : https://blog.csdn.net/qq_36330643/article/details/77868337
https://blog.csdn.net/hnhuangyiyang/article/details/52711631
https://blog.csdn.net/linuxprobe18/article/details/80460068 //參考此3個鏈接

[root@lb01 ~]# yum install chrony -y
[root@lb01 ~]# rpm -qa|grep chrony
chrony-2.1.1-2.el6_8.x86_64
[root@lb01 ~]# cat /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 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst

# Ignore stratum in source selection.
stratumweight 0

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# In first three updates step the system clock instead of slew
# if the adjustment is larger than 10 seconds.
makestep 10 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Allow NTP client access from local network.
#allow 192.168/16

# Serve time even if not synchronized to any NTP server.
#local stratum 10

# Specify file containing keys for NTP and command authentication.
keyfile /etc/chrony.keys

# Specify key number for command authentication.
commandkey 1

# Generate new command key on start if missing.
generatecommandkey

# Disable logging of client accesses.
noclientlog

# Send message to syslog when clock adjustment is larger than 0.5 seconds.
logchange 0.5

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

Linux centos7 下使用timedatectl命令時間時區操作詳解:https://www.cnblogs.com/zhi-leaf/p/6282301.html
CentOS 6 時間,時區,設置修改及時間同步: https://blog.csdn.net/testcs_dn/article/details/39803919
在centos7上執行 timedatectl set-ntp true //使用網絡時間同步
在CentOS 6版本,時間設置有date、hwclock命令,從CentOS 7開始,使用了一個新的命令timedatectl。

[root@lb01 ~]# date +%F\ %T 
2018-12-07 23:31:37
[root@lb01 ~]# grep '^[a-Z]' /etc/chrony.conf
server ntp1.aliyun.com iburst
server time.nist.gov iburst
server time.windows.com iburst              //增加以上三個
stratumweight 0
driftfile /var/lib/chrony/drift
makestep 10 3
rtcsync
allow 192.168.10.0/16                  //允許這個網段的來同步自己的時間
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony

[root@lb01 ~]# service chronyd start
Starting chronyd:                                          [  OK  ]
[root@lb01 ~]# service chronyd status
chronyd (pid  1823) is running...
[root@lb01 ~]# chronyc -a makestep                          //設置完時區後,強制同步下系統時鐘:
200 OK
200 OK
[root@lb01 ~]# chkconfig chronyd on  
[root@lb01 ~]# date +%F\ %T                 //此時的時間與當時時間是準的         
2018-12-07 15:38:44
[root@lb01 ~]#  ps -ef|grep chrony
chrony     1823      1  0 15:34 ?        00:00:00 /usr/sbin/chronyd -u chrony
root       1849   1707  0 15:46 pts/0    00:00:00 grep --color=auto chrony
[root@lb01 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1562/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1638/master         
tcp        0      0 :::22                       :::*                        LISTEN      1562/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1638/master         
udp        0      0 127.0.0.1:323               0.0.0.0:*                               1823/chronyd        
udp        0      0 ::1:323                     :::*                                    1823/chronyd  
[root@lb01 ~]# chronyc sources -v   //驗證服務端是否能同步時間
210 Number of sources = 3

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 120.25.115.20                 2   8   377   496  -2413us[-2900us] +/-   22ms
^? time-a-g.nist.gov             0   9     0   10y     +0ns[   +0ns] +/-    0ns
^- 52.168.138.145                2   8   377    97    +16ms[  +16ms] +/-  182ms
以上服務器端配置完成

lb01作爲服務器端 1b02 作爲客戶端,來同步他的時間
[root@lb02 ~]# date +%F\ %T          //此時時間是不準的
2018-12-07 23:41:17
[root@lb02 ~]# yum -y install chrony
[root@lb02 ~]# grep '^[a-Z]' /etc/chrony.conf    
server 192.168.10.160 iburst                  //添加這個
stratumweight 0
driftfile /var/lib/chrony/drift
makestep 10 3
rtcsync
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony
[root@lb02 ~]# service chronyd status
chronyd is stopped
[root@lb02 ~]# service chronyd start
Starting chronyd:                                          [  OK  ]
[root@lb02 ~]# chkconfig chronyd on
[root@lb02 ~]# chronyc sources -v               //驗證同步的源
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.10.160                0   8     0   10y     +0ns[   +0ns] +/-    0ns

———————————————————————————————————————————————————————————————————————————————————————————————————————————————

7.常用命令

查看時間同步源:

$ chronyc sources -v

[root@lb02 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.10.160                0   8     0   10y     +0ns[   +0ns] +/-    0ns

查看時間同步源狀態:

$ chronyc sourcestats -v
[root@lb02 ~]# chronyc sourcestats -v
210 Number of sources = 1
                             .- Number of sample points in measurement set.
                            /    .- Number of residual runs with same sign.
                           |    /    .- Length of measurement set (time).
                           |   |    /      .- Est. clock freq error (ppm).
                           |   |   |      /           .- Est. error in freq.
                           |   |   |     |           /         .- Est. offset.
                           |   |   |     |          |          |   On the -.
                           |   |   |     |          |          |   samples. \
                           |   |   |     |          |          |             |
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
192.168.10.160              0   0     0     +0.000   2000.000     +0ns  4000ms

設置硬件時間

硬件時間默認爲UTC:

$ timedatectl set-local-rtc 1

啓用NTP時間同步:

$ timedatectl set-ntp yes

校準時間服務器:

$ chronyc tracking
[root@lb02 ~]# chronyc tracking
Reference ID    : 0.0.0.0 ()
Stratum         : 0
Ref time (UTC)  : Thu Jan  1 00:00:00 1970
System time     : 0.000000000 seconds fast of NTP time
Last offset     : +0.000000000 seconds
RMS offset      : 0.000000000 seconds
Frequency       : 0.000 ppm fast
Residual freq   : +0.000 ppm
Skew            : 0.000 ppm
Root delay      : 0.000000 seconds
Root dispersion : 0.000000 seconds
Update interval : 0.0 seconds
Leap status     : Not synchronised

最後需要注意的是,配置完/etc/chrony.conf後,需重啓chrony服務,否則可能會不生效。
————————————————————————————————————————————————————————————————————————————————————————————————————————————————

**centos7系統**
[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.1.1503 (Core) 
 24  yum install chrony -y
   25  rpm -qa|grep chrony
   26  date
   27  cat /etc/chrony.conf
   28  grep '^[a-Z]' /etc/chrony.conf
   29  vim /etc/chrony.conf
   30  grep '^[a-Z]' /etc/chrony.conf
   32  systemctl start chronyd
   33  systemctl status chronyd
   34  chronyc -a makestep 
   35  systemctl enable chronyd
   36  date +%F\ %T  
   37  date -s 18:00:00             //我設置的錯誤的時間
   38  date +%F\ %T  
   39  ps -ef|grep chrony
   40  netstat -lntup
   41  chronyc sources -v
   42  date +%F\ %T  
   43  chronyc -a makestep  
   [root@localhost ~]# timedatectl status
      Local time: Mon 2018-12-10 18:01:32 CST
  Universal time: Mon 2018-12-10 10:01:32 UTC
        RTC time: Mon 2018-12-10 10:43:59
        Timezone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no              //表示,還沒有進行時間同步
 RTC in local TZ: no
      DST active: n/a
[root@localhost ~]# date +%F\ %T           //此時的時間是正確的        
2018-12-10 18:44:57
[root@localhost ~]# timedatectl status      
      Local time: Mon 2018-12-10 18:45:44 CST
  Universal time: Mon 2018-12-10 10:45:44 UTC
        RTC time: Mon 2018-12-10 10:45:43
        Timezone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes                  //表示已經同步上了
 RTC in local TZ: no
      DST active: n/a

重新搞了個從的服務器(客戶端)
修改了主服務器的配置,然後重啓了chronyd服務
[root@localhost ~]# grep '^[a-Z]' /etc/chrony.conf    
server ntp1.aliyun.com iburst
server time.nist.gov iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow 192.168.10.0/16
logdir /var/log/chrony

在從服務器上直接安裝配置好
[root@localhost ~]# grep '^[a-Z]' /etc/chrony.conf 
server 192.168.10.129 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
[root@localhost ~]# timedatectl      
      Local time: Mon 2018-12-10 18:56:19 CST
  Universal time: Mon 2018-12-10 10:56:19 UTC
        RTC time: Mon 2018-12-10 10:56:19
        Timezone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes           //成功了,時間和主服務器上是相同的
 RTC in local TZ: no
      DST active: n/a
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章