Centos7服务器通过Chrony设置时间同步 + timedatectl命令的使用

小虾米 阅读:171 2022-06-06 14:11:52 评论:0

1. timedatectl命令的使用

查看时间、时区和NTP状态

[root@kudu1 ~]# timedatectl 
      Local time: 三 2022-03-16 10:16:03 CST 
  Universal time: 三 2022-03-16 02:16:03 UTC 
        RTC time: 三 2022-03-16 02:16:04 
       Time zone: Asia/Shanghai (CST, +0800) 
     NTP enabled: yes 
NTP synchronized: yes 
 RTC in local TZ: no 
      DST active: n/a 
[root@kudu1 ~]# 

查看时区列表

[root@kudu1 ~]# timedatectl list-timezones | grep -E "Asia/S.*" 
Asia/Sakhalin 
Asia/Samarkand 
Asia/Seoul 
Asia/Shanghai 
Asia/Singapore 
Asia/Srednekolymsk 
[root@kudu1 ~]# 

修改时区

[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl set-timezone Asia/Shanghai 
[root@kudu1 ~]#  

NTP服务的开启(true) / 关闭(false),用于控制是否使用ntpd或Chrony同步服务

[root@kudu1 ~]# timedatectl set-ntp false 
[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl  
      Local time: 三 2022-03-16 10:27:42 CST 
  Universal time: 三 2022-03-16 02:27:42 UTC 
        RTC time: 三 2022-03-16 02:27:43 
       Time zone: Asia/Shanghai (CST, +0800) 
     NTP enabled: no 
NTP synchronized: yes 
 RTC in local TZ: no 
      DST active: n/a 
[root@kudu1 ~]#  

修改Local time日期时间,需先关闭NTP服务。可以修改日期、时间、日期时间

[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl set-time 2021-10-01 
[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl 
      Local time: 五 2021-10-01 00:00:04 CST 
  Universal time: 四 2021-09-30 16:00:04 UTC 
        RTC time: 四 2021-09-30 16:00:05 
       Time zone: Asia/Shanghai (CST, +0800) 
     NTP enabled: no 
NTP synchronized: no 
 RTC in local TZ: no 
      DST active: n/a 
[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl set-time 12:00:00 
[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl 
      Local time: 五 2021-10-01 12:00:02 CST 
  Universal time: 五 2021-10-01 04:00:02 UTC 
        RTC time: 五 2021-10-01 04:00:02 
       Time zone: Asia/Shanghai (CST, +0800) 
     NTP enabled: no 
NTP synchronized: no 
 RTC in local TZ: no 
      DST active: n/a 
[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl set-time "2022-03-16 10:32:00" 
[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl 
      Local time: 三 2022-03-16 10:32:02 CST 
  Universal time: 三 2022-03-16 02:32:02 UTC 
        RTC time: 三 2022-03-16 02:32:03 
       Time zone: Asia/Shanghai (CST, +0800) 
     NTP enabled: no 
NTP synchronized: no 
 RTC in local TZ: no 
      DST active: n/a 
[root@kudu1 ~]#  

设置硬件时间,即RTC时间,为本地时间(1) / UTC时间(0)

[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl set-local-rtc 0 
[root@kudu1 ~]#  

2. Chrony设置时间同步的使用

chrony包含两个程序

  1. chronyd是一个守护程序
  2. chronyc是一个命令行程序,用于监视chronyd并在运行时更改各种操作参数

2.1 安装

在服务端和客户端,安装Chrony

[root@kudu1 ~]# yum -y install chrony 
[root@kudu1 ~]# 

2.2 配置

修改/etc/chrony.conf

服务端配置

注释以下几行

#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 ntp.aliyun.com iburst 
server ntp.tuna.tsinghua.edu.cn iburst 

修改如下

allow 192.168.23.0/16 

客户端配置
注释以下几行

#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 192.168.23.121 iburst 

2.3 启动

在服务端和客户端,启动Chrony

[root@kudu1 ~]#  
[root@kudu1 ~]# systemctl start chronyd 
[root@kudu1 ~]#  
[root@kudu1 ~]# systemctl enable chronyd 
[root@kudu1 ~]# 

查看linux是否开启NTP同步

[root@kudu1 ~]#  
[root@kudu1 ~]# timedatectl  
      Local time: 三 2022-03-16 11:00:25 CST 
  Universal time: 三 2022-03-16 03:00:25 UTC 
        RTC time: 三 2022-03-16 03:00:25 
       Time zone: Asia/Shanghai (CST, +0800) 
     NTP enabled: yes 
NTP synchronized: yes 
 RTC in local TZ: no 
      DST active: n/a 
[root@kudu1 ~]# 

2.4 chronyc相关命令使用

查看ntp_servers

[root@kudu1 ~]# chronyc sources -v 
210 Number of sources = 2 
 
  .-- 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                
=============================================================================== 
^* 203.107.6.88                  2   6   377    26    +29ms[  +33ms] +/-   78ms 
^? 2402:f000:1:416:101:6:6:>     0   8     0     -     +0ns[   +0ns] +/-    0ns 
[root@kudu1 ~]# 

查看ntp_servers状态

[root@kudu1 ~]# chronyc sourcestats -v 
210 Number of sources = 2 
                             .- 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 
============================================================================== 
203.107.6.88                9   6   330     +4.032    134.944    +62us  8669us 
2402:f000:1:416:101:6:6:>   0   0     0     +0.000   2000.000     +0ns  4000ms 
[root@kudu1 ~]#  

查看ntp_servers是否在线

[root@kudu1 ~]# chronyc activity -v 
200 OK 
2 sources online 
0 sources offline 
0 sources doing burst (return to online) 
0 sources doing burst (return to offline) 
0 sources with unknown address 
[root@kudu1 ~]#  

查看ntp详细信息

[root@kudu1 ~]#  
[root@kudu1 ~]# chronyc tracking -v 
Reference ID    : CB6B0658 (203.107.6.88) 
Stratum         : 3 
Ref time (UTC)  : Wed Mar 16 03:22:40 2022 
System time     : 0.001107589 seconds fast of NTP time 
Last offset     : -0.000784305 seconds 
RMS offset      : 0.003900274 seconds 
Frequency       : 15.156 ppm fast 
Residual freq   : +0.123 ppm 
Skew            : 85.192 ppm 
Root delay      : 0.064712383 seconds 
Root dispersion : 0.008965795 seconds 
Update interval : 64.6 seconds 
Leap status     : Normal 
[root@kudu1 ~]# 

标签:linux
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

搜索
排行榜
关注我们

一个IT知识分享的公众号