首页 > 技术文章 > 时钟同步 chrony

fyusac 2020-06-11 18:50 原文

一、简介

chrony 是NTP(Network Time Protocol ,网络时间协议)的另一种实现方式,相比ntpd,可以更快更准确地同步系统时钟,最大程度地减少时间和频率误差

chrony包括两个核心程序:chronyd是一个守护进程,用于调整内核中运行的系统时钟与NTP服务器同步;chronyc是一个命令行界面程序,用于监视chronyd的性能以及进行各项配置。

二、安装与配置
[root@host240 ~]# yum -y install  chrony 

配置文件 vim /etc/chrony.conf

 Custom source

# Use public servers from the pool.ntp.org project.
# 添加的时钟同步服务器
server ha-vip iburst
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
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

# Record the rate at which the system clock gains/losses time.
# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,方便在系统重启后为系统做出最佳的时间补偿调整
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
# 如果系统时钟的误差小于1秒,则通过逐渐增大或减慢时钟来进行调整
# 如果系统时钟的偏移量超过1秒,则允许系统时钟在前三次更新时允许步进时间(时间跳变)
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
# 启动硬件时钟RTC的内核同步
rtcsync

# Enable hardware timestamping on all interfaces that support it.
# 通过使用hwtimestamp指令启用硬件时间戳
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# 指定允许NTP客户端的地址范围,以允许或拒绝连接到时钟源的机器
# Allow NTP client access from local network.
#allow 192.168.0.0/16
allow all		#允许所有客户端

# 即使自己未能通过网络时间服务器同步到时间,也允许将本地时间作为标准时间授时给其它客户端
# Serve time even if not synchronized to a time source.
local stratum 10

# Specify file containing keys for NTP authentication.
# 指定包含NTP身份验证的文件
#keyfile /etc/chrony.keys

# Specify directory for log files.
# 日志文件的目录
logdir /var/log/chrony

# 选择日志要记录的信息
# Select which information is logged.
#log measurements statistics tracking
# 拒绝的客户端
deny host216
三、使用方式

服务端修改配置文件

allow all #指定允许访问的客户端地址范围

客户端修改配置文件

server 服务端地址 iburst

服务端和客户端重启服务

systemctl restart chronyd.service
四、常用命令
# 查看时钟源
[root@host216 ~]# chronyc sources -v
210 Number of sources = 5

  .-- 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
===============================================================================
^? 10.46.157.240                10   6     1    20    +68us[  +68us] +/-  353us
# 查看日期时间、时区及 NTP 状态
[root@host216 ~]# timedatectl
      Local time: Thu 2020-06-11 18:41:39 CST
  Universal time: Thu 2020-06-11 10:41:39 UTC
        RTC time: Thu 2020-06-11 10:41:38
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
#查看时钟源状态
[root@host216 ~]# chronyc sourcestats -v
210 Number of sources = 5
                             .- 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
==============================================================================
10.46.157.240               2   0    65     +0.000   2000.000  -5528ns  4000ms

# 查看时钟源是否在线
[root@host216 ~]# chronyc activity -v
200 OK
# 查看 ntp 详细信息
[root@host216 ~]# chronyc tracking -v
Reference ID    : 0A2E9DF0 (10.46.157.240)
Stratum         : 11
Ref time (UTC)  : Thu Jun 11 09:40:56 2020
System time     : 0.000014615 seconds fast of NTP time
Last offset     : +0.000018921 seconds
RMS offset      : 0.000018921 seconds
Frequency       : 68.835 ppm fast
Residual freq   : -0.480 ppm
Skew            : 0.092 ppm
Root delay      : 0.000887929 seconds
Root dispersion : 0.000064294 seconds
Update interval : 64.5 seconds
Leap status     : Normal
#查看时区列表
[root@host216 ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
... ...
Asia/Shanghai
# 修改时区
timedatectl set-timezone Asia/Shanghai

https://www.cnblogs.com/jhxxb/p/11526098.html

推荐阅读