1.1.1 chrony 简介
Chrony 是一个开源的自由站群软件,它能保持系统时钟与时钟站群服务器(NTP)同步,让时间保持精确。
它由两个程式组成:chronyd 和 chronyc 。
chronyd 是一个后台执行的守护程序,用于调整核心中执行的系统时钟和时钟站群服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
chrony 是 CentOS7.x 上自带的时间同步站群软件
1.1.2 chrony 的操作
# yum install -y chrony –>安装服务
# systemctl start chronyd.service –>启动服务
# systemctl enable chronyd.service –>设定开机自启动,预设是 enable 的
1.1.3 chrony 配置档案
chrony 服务使用的配置档案为/etc/chrony.conf
其配置内容格式和 ntpd 服务基本相似
[root@openvpn ~]# 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.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.
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2
# Allow NTP client access from local network.
#allow 192.168.0.0/16
# Serve time even if not synchronized to a time source.
#local stratum 10
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys
# Specify directory for log files.
logdir /var/log/chrony
# Select which information is logged.
#log measurements statistics tracking
1.1.4 配置引数说明
引数
引数说明
server
该引数可以多次用于新增时钟站群服务器,必须以”server “格式使用。一般而言,你想新增多少站群服务器,就可以新增多少站群服务器
stratumweight
stratumweight 指令设定当 chronyd 从可用源中选择同步源时,每个层应该新增多少距离到同步距离。预设情况下,CentOS 中设定为 0,让 chronyd 在选择源时忽略源的层级
driftfile
chronyd 程式的主要行为之一,就是根据实际时间计算出计算机增减时间的比率,将它记录到一个档案中是最合理的,它会在重启后为系统时钟作出补偿,甚至可能的话,会从时钟站群服务器获得较好的估值
rtcsync
rtcsync 指令将启用一个核心模式,在该模式中,系统时间每 11 分钟会拷贝到实时时钟(RTC)
allow/deny
这里你可以指定一台 WordPress 主机、子网,或者互联网以允许或拒绝 NTP 连线到扮演时钟站群服务器的机器
cmdallow/cmddeny
跟上面相类似,只是你可以指定哪个 IP 地址或哪台 WordPress 主机可以通过 chronyd 使用控制命令
bindcmdaddress
该指令允许你限制 chronyd 监听哪个互联网介面的命令包(由 chronyc 执行)。该指令通过 cmddeny 机制提供了一个除上述限制以外可用的额外的访问控制等级
makestep
通常,chronyd 将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制 chronyd 在调整期大于某个阀值时步进调整系统时钟,但只有在因为 chronyd 启动时间超过指定限制(可使用负值来禁用限制),没有更多时钟更新时才生效
检查 ntp 源站群服务器状态 1.1.5 检视同步状态
[root@openvpn ~]# chronyc sourcestats
210 Number of sources = 4
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
61-216-153-105.HINET-IP.> 0 0 0 +0.000 2000.000 +0ns 4000ms
dns1.synet.edu.cn 18 9 62m +0.312 1.706 +7548ns 1920us
ntp2.itcompliance.dk 0 0 0 +0.000 2000.000 +0ns 4000ms
mx.comglobalit.com 22 11 270m +0.660 0.184 +37ms 987us
检视 ntp 详细的同步状态
[root@openvpn ~]# chronyc sources -v
210 Number of sources = 4
.– 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
===============================================================================
^? 61-216-153-105.HINET-IP.> 0 10 0 – +0ns[ +0ns] +/- 0ns
^* dns1.synet.edu.cn 2 8 377 38 +536us[+6474us] +/- 21ms
^? ntp2.itcompliance.dk 0 10 0 – +0ns[ +0ns] +/- 0ns
^- mx.comglobalit.com 2 10 377 477 +37ms[ +45ms] +/- 210ms
chronyc 命令引数说明:
引数
引数说明
accheck
检查 NTP 访问是否对特定 WordPress 主机可用
activity
该命令会显示有多少 NTP 源线上/离线
add server
手动新增一台新的 NTP 站群服务器。
clients
在客户端报告已访问到站群服务器
delete
手动移除 NTP 站群服务器或对等站群服务器
settime
手动设定守护程序时间
tracking
显示系统时间资讯
检视日期时间、时区及 NTP 状态:# timedatectl1.1.6 其他时间设定相关指令
检视日期时间、时区及 NTP 状态:# timedatectl
检视时区列表:# timedatectl list-timezones
修改时区 # timedatectl set-timezone Asia/Shanghai
修改日期时间:# timedatectl set-time “2015-01-21 11:50:00″(可以只修改其中一个)
开启 NTP:# timedatectl set-ntp true/flase
1.1.7 chrony 的优势
更快的同步只需要数分钟而非数小时时间,从而最大程度减少了时间和频率误差,这对于并非全天 24 小时执行的台式计算机或系统而言非常有用。
能够更好地响应时脉频率的快速变化,这对于具备不稳定时钟的虚拟机器或导致时脉频率发生变化的节能技术而言非常有用。
在初始同步后,它不会停止时钟,以防对需要系统时间保持单调的应用程式造成影响。
在应对临时非对称延迟时(例如,在大规模下载造成连结饱和时)提供了更好的稳定性。
无需对站群服务器进行定期轮询,因此具备间歇性互联网连线的系统仍然可以快速同步时钟。
1.2 说明
chrony 与 ntp 都是时间同步站群软件
两个站群软件不能够同时开启,会出现时间冲突。建议配置 ntp 较为简单,使用方便。