Linux-chrony设置集群同步时间

简介

Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确。

Chrony分为守护进程chronyd,用于调整内核中运行的系统时间和时间服务器同步 以及 用于监控性能并进行多样化的配置的chronyc


环境信息

[root@suhw ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
[root@suhw ~]# uname -a
Linux suhw 3.10.0-1062.4.3.el7.x86_64 #1 SMP Wed Nov 13 23:58:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

安装

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

设置开机启动

[root@suhw ~]# systemctl enable chronyd.service
[root@suhw ~]# systemctl restart chronyd.service
[root@suhw ~]# systemctl status chronyd.service
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-06-04 10:12:10 CST; 4s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 11520 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 11516 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 11518 (chronyd)
    Tasks: 1
   Memory: 804.0K
   CGroup: /system.slice/chronyd.service
           └─11518 /usr/sbin/chronyd

Jun 04 10:12:10 suhw systemd[1]: Starting NTP client/server...
Jun 04 10:12:10 suhw chronyd[11518]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Jun 04 10:12:10 suhw systemd[1]: Started NTP client/server.

防火墙设置

$ firewall-cmd --add-service=ntp --permanent
$ firewall-cmd --reload

NTP使用123/UDP端口协议,所以允许NTP服务即可。


配置参数

完整参数介绍参考:https://chrony.tuxfamily.org/doc/3.4/chrony.conf.html

默认配置在/etc/chrony.conf中,参考如下

# server 用于添加 ntp 服务器
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

# 记录系统时钟增益/损失时间的速率
driftfile /var/lib/chrony/drift

# 允许跳跃式校时 如果在前 3 次校时中时间差大于 1.0s
makestep 1.0 3

# 同步时间至 RTC
rtcsync

# 在支持它的所有接口上启用硬件时间戳。
#hwtimestamp *

# 增加调整系统时钟所需的最小可选源数。
#minsources 2

# 允许那些客户端访问 NTP
#allow 192.168.0.0/16

# 即使未同步到时间源,也会提供时间。
#local stratum 10

# 指定 NTP 认证的 key 存在的文件
#keyfile /etc/chrony.keys

# 为日志文件指定目录
logdir /var/log/chrony

# 选择要被记录的信息
#log measurements statistics tracking


Server

server hostname [option]…

默认配置中默认使用四组官方的NTP服务器

0.centos.pool.ntp.org
1.centos.pool.ntp.org
2.centos.pool.ntp.org
3.centos.pool.ntp.org

iburst是为了缩短初始同步所花费的时间,该参数一般为server的默认选项。


driftfile

根据实际时间计算修正值,并将补偿参数记录在该指令指定的文件里,默认为 driftfile var/lib/chrony/drift

[root@suhw ~]# cat /var/lib/chrony/drift 
            7.843162             0.028786

makestep

用法:makestep threshold limit

根据需要通过加速或减慢时钟来逐渐校正任何时间偏移。例如:

makestep 1.0 3

就表示当头三次校时,如果时间相差 1.0s, 则跳跃式校时。


rtcsync

rtcsync指令会将内核时间与RTC( Real_Time Clock)进行同步

RTC:实时时钟芯片


allow / deny

这里你可以指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器。例如

allow 192.168.4.5
deny 192.168/16

commandkey

如果在/etc/chrony.conf中指定generatecommandkey参数,那么将 在/etc/chrony.keys中生成一个密码,或者也可以自己手动在chrony.keys中添加指定密码。commandkey后跟的数字就代表使用chrony.keys中的哪个密码,默认值为1。


local stratum 10

由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端,所以服务器从高到低级别可以设定为1-16。0层为高精度计时设备,从1层开始,每层设备的时间都向前一层设备同步时间。(stratum 2stratum 1同步时间,以此类推)

为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器。此处的就设置的是当前本地NTP服务器的层数。


bindaddress

chronyd监听NTP请求的socket绑定到计算机的本地地址。



配置示例

以下示例中 ntp server机器地址为 10.91.156.174client10.47.119.96

设置本地作为NTP server

例如要将本地设置为NTP服务器。该机器地址为10.91.156.174

修改配置文件/etc/chrony.conf

# 配置 本地 作为 ntp 服务器
server 127.0.0.1 iburst

# 计算时间补偿
driftfile /var/lib/chrony/drift

# 如果前三次校时相差大于1s,则使用跳跃校时来校正时间
makestep 1.0 3

# 同步时间到 RTC
rtcsync

# 日志存放在 /var/log/chrony
logdir /var/log/chrony

bindaddress ::

# 表示所有人都可访问本机提供的ntp服务
allow

重启服务

[root@ntp-server ~]# systemctl restart chronyd

查看ntp服务器状态

[root@ntp-server ~]# chronyc sourcestats
210 Number of sources = 1
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
localhost                   2   0    65     +0.000   2000.000  -6105ns  4000ms

设置NTP server

将第二台10.47.119.96NTP server设置为第一台机器

[root@ntp-client ~]# cat /etc/chrony.conf 
server 10.91.156.174 iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
keyfile /etc/chrony.keys
bindaddress ::
allow

同样重启生效



chronyc常用用法

注:详细解释参考man chronyc

交互式使用

chronyc可以交互式使用,直接输入chronyc后,输入对应命令即可

[root@ntp-server ~]# chronyc
chrony version 3.4
Copyright (C) 1997-2003, 2007, 2009-2018 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY.  This is free software, and
you are welcome to redistribute it under certain conditions.  See the
GNU General Public License version 2 for details.

chronyc> sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* localhost                    10   9   377  117m  -6818ns[  -14us] +/-   32us
chronyc> 


查看NTP server信息

[root@ntp-client ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 10.91.156.174                11   6   377    42   -645us[-1177us] +/-   14ms

检查NTP server状态

显示当前在线和脱机的源

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

检查NTP是否对特定主机可用

[root@ntp-server ~]# chronyc accheck 10.47.119.96
208 Access allowed

获取ntp client

此命令显示通过NTP或命令端口访问服务器的客户端列表

[root@ntp-server ~]# chronyc clients
Hostname                      NTP   Drop Int IntL Last     Cmd   Drop Int  Last
===============================================================================
localhost                      30      0   8   -    49       0      0   -     -
10.47.119.96                   17      0   6   -    37       0      0   -     -


查看系统时钟性能

[root@ntp-client ~]# chronyc tracking
# 同步到的 ntp server 信息
Reference ID    : 0A5B9CAE (10.91.156.174)
# 所在层数
Stratum         : 12
Ref time (UTC)  : Fri Jun 05 03:07:36 2020
System time     : 0.000000380 seconds fast of NTP time
Last offset     : -0.000264585 seconds
RMS offset      : 0.001242722 seconds
Frequency       : 37.658 ppm slow
Residual freq   : -0.189 ppm
Skew            : 2.847 ppm
Root delay      : 0.021106269 seconds
Root dispersion : 0.004897448 seconds
Update interval : 64.3 seconds
Leap status     : Normal



查看时间同步命令

timedatectl

查看系统时区信息

[root@suhw ~]# timedatectl status
      Local time: Thu 2020-06-04 17:34:02 CST
  Universal time: Thu 2020-06-04 09:34:02 UTC
        RTC time: Thu 2020-06-04 09:35:04
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

查看可用时区

[root@suhw ~]# timedatectl list-timezones | grep Shanghai
Asia/Shanghai

设置系统时区

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

可以先将本地服务器时区设置为Asia/Shanghai,再启动NTP服务


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章