简介
Chrony是一个开源的自由软件,像CentOS 7或基于RHEL 7操作系统,已经是默认服务,默认配置文件在 /etc/chrony.conf 它能保持系统时间与时间服务器(NTP)同步,让时间始终保持同步。相对于NTP时间同步软件,占据很大优势。其用法也很简单。
安装Chrony
yum install -y chrony
配置Chrony
vim /etc/chrony.conf
- 将原有配置的4个server注释掉,加入新的一行server,作为时钟同步的主机,该主机可以为任意局域网内的主机。
- 打开allow配置的注释,修改为指定子网,或者网络以允许NTP连接到扮演时钟服务器的机器。
- 将 local stratum 10 这行的注释打开
# 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
server 172.16.176.11
# Ignore stratum in source selection.
stratumweight 0
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
# Enable kernel RTC synchronization.
rtcsync
# In first three updates step the system clock instead of slew
# if the adjustment is larger than 10 seconds.
makestep 10 3
# Allow NTP client access from local network.
allow 172.16.0.0/16
# Listen for commands only on localhost.
#bindcmdaddress 127.0.0.1
#bindcmdaddress ::1
# Serve time even if not synchronized to any NTP server.
local stratum 10
keyfile /etc/chrony.keys
# Specify the key used as password for chronyc.
commandkey 1
# Generate command key if missing.
generatecommandkey
# Disable logging of client accesses.
noclientlog
# Send a message to syslog if a clock adjustment is larger than 0.5 seconds.
logchange 0.5
logdir /var/log/chrony
#log measurements statistics tracking
分发配置文件
将/etc/chrony.conf文件分发到所有主机上,所有主机使用一样的配置即可。
scp /etc/chrony.conf mini2:/etc/
scp /etc/chrony.conf mini3:/etc/
启动并加入开机自启
# 开机自启
systemctl enable chronyd.service
# 重启服务
systemctl restart chronyd.service
# 查看服务状态
systemctl status chronyd.service
防火墙设置
如果防火墙处于关闭状态,请忽略并跳过
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload
查看当前系统时区
[root@mini1 ~]# timedatectl
Local time: 一 2019-11-25 17:45:00 CST
Universal time: 一 2019-11-25 09:45:00 UTC
RTC time: 一 2019-11-25 09:45:00
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
查看时间同步源
[root@mini1 ~]# 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
===============================================================================
^? mini1
查看时间同步源状态
[root@mini1 ~]# chronyc sourcestats -v
210 Number of sources = 1
.- 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
==============================================================================
mini1 2 0 64 +0.000 2000.000 -5470ns 4000ms
校准时间同步服务
[root@mini1 ~]# chronyc tracking
Reference ID : 7F7F0101 ()
Stratum : 10
Ref time (UTC) : Mon Nov 25 09:46:34 2019
System time : 0.000000000 seconds fast of NTP time
Last offset : +0.000000000 seconds
RMS offset : 0.000000000 seconds
Frequency : 2.468 ppm fast
Residual freq : +0.000 ppm
Skew : 0.000 ppm
Root delay : 0.000000000 seconds
Root dispersion : 0.000000000 seconds
Update interval : 0.0 seconds
Leap status : Normal