搭建chrony时间同步服务器

Linux用户对于时间同步,基本上是使用ntpdate和ntpd这两个工具实现的,但是这两个工具已经很古老了,在某些情况下,例如,你的服务器经常开关机,或者很少,偶尔才能连接外部网络,最典型的比如在隔绝的内网中,没有相应时间同步的硬件,ntpd支持就不是很好了。

chrony是redhat开发的,它是网络时间协议的 (NTP) 的另一种实现。centos7/rhel7默认的时间同步工具,在centos6.8之后,老的centos和rhel6系列也添加上了这个工具。Chrony可以同时做为ntp服务的客户端和服务端。默认安装完后有两个程序chronyd和chronyc 。chronyd是一个在系统后台运行的守护进程,chronyc是用来监控chronyd性能和配置其参数程序。

安装

yum install chrony

服务端设置:

  • 配置文件
    vi /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

stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
allow 172.10/16
smoothtime 400 0.01

bindcmdaddress 127.0.0.1
bindcmdaddress ::1

local stratum 8
manual
keyfile /etc/chrony.keys
initstepslew 10 client1 client3 client6
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony

172.10是允许连接到服务器的子网的网段。

  • 防火墙开通端口
firewall-cmd --permanent --add-service=ntp
firewall-cmd --reload

客户端设置:

  • 配置文件
    vi /etc/chrony.conf
server pdc.adagene.cn iburst
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
allow 172.10.10.100
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
local stratum 10
initstepslew 20 pdc.adagene.cn
keyfile /etc/chrony.keys
commandkey 24
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony
log measurements statistics tracking

需要注意的是,我们这里的pdc.adagene.cn是有内网解析的刚才master的的主机名。allow后面的ip地址为master主机的ip地址。

  • 启用chronyd服务即可同步:
    systemctl start chronyd.service
    或者没有启动服务,手动同步(类似ntpdate命令):
    chronyd -q 'server pdc.adagene.cn iburst'
    chronyd -q 'server ntp.ntsc.ac.cn iburst'
    注意:iburst这个参数(上文配置文件中的一样)会让当前服务器的时间即刻和时间服务器同步; 若不加这个参数,客户端时间会逐步和服务器时间对齐而不会立刻修改时间,从而能避免因为时间变化过大导致当前运行程序异常; iburst需按具体场景选择

客户端查询常用命令

  • 查看源服务器状态chronyc sourcestats
[root@template ~]# chronyc sourcestats
210 Number of sources = 1
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
172.10.10.100              4   3     6     +3.856    284.293   +214us    29us
  • 查看详细同步状态chronyc sources -v
[root@template ~]# 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
===============================================================================
^* 172.10.10.100                3   6    17    35  +7984ns[  +25us] +/-   23ms
  • 追踪同步情况chronyc tracking
[root@template ~]# chronyc tracking
Reference ID    : 172.10.10.100 (172.10.10.100)
Stratum         : 4
Ref time (UTC)  : Mon Aug 22 06:58:59 2016
System time     : 0.000012924 seconds fast of NTP time
Last offset     : +0.000033437 seconds
RMS offset      : 0.000071534 seconds
Frequency       : 34.016 ppm slow
Residual freq   : +0.049 ppm
Skew            : 1.057 ppm
Root delay      : 0.039409 seconds
Root dispersion : 0.002063 seconds
Update interval : 64.2 seconds
Leap status     : Normal
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hzw@sirius

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值