Centos7安装配置Chrony

记录一下chrony部署配置

介绍

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

它由两个程序组成:

  • chronyd 是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
  • chronyc 提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作

chrony即可作为时间服务器服务端,也可以作为客户端

环境

ip 系统 角色
172.16.88.26 CentOS 7.6 内网NTP Server
172.16.88.27 CentOS 7.6 内网NTP Client

基础配置

1
2
3
4
#关闭防火墙和selinux
systemctl stop firewalld && systemctl disable firewalld
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
setenforce 0

安装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 使用rpm -qa |grep chrony查看系统是否已安装chrony,可看到默认已安装chrony的包
#如果没有安装环境 用下面命令安装
yum -y install chrony
#启动与停止
systemctl start chronyd.service
systemctl stop chronyd.service
#开机自启动
systemctl enable chronyd.service

配置

NTP Server配置

172.16.88.26

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#yum安装的chrony默认配置文件为/etc/chrony.conf 
vim /etc/chrony.conf 

......
# These servers were defined in the installation:
#server 3.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst  #注释掉原来的地址
#添加阿里云ntp
server ntp1.aliyun.com iburst    
server ntp2.aliyun.com iburst 
server ntp3.aliyun.com iburst 

# 记录系统时钟获得/丢失时间的速率至drift文件中
driftfile /var/lib/chrony/drift

#如果系统时钟的偏移量大于一秒,则允许在前三次更新中步进调整系统时钟
makestep 1.0 3

#启用实时时钟(RTC)的内核同步。
rtcsync

#允许从本地网络访问 NTP 客户端。
allow 172.16.0.1/16


#在公网NTP服务器不可用时,使用本地时间来作为同步标准
local stratum 10

#指定包含 NTP 身份验证密钥的文件。
keyfile /etc/chrony.keys

#指定存放日志文件的目录
logdir /var/log/chrony

### 注意:详细指令参数可以使用命令 man chrony.conf查看


#######修改后重启chronyd服务
systemctl restart chronyd.service
systemctl status chronyd.service

NTP Client配置

172.16.88.27

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#yum安装的chrony默认配置文件为/etc/chrony.conf 
vim /etc/chrony.conf 

......
# These servers were defined in the installation:
#server 3.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst  #注释掉原来的地址
server 172.16.88.26 iburst   #添加ntp server地址172.16.88.26

# 记录系统时钟获得/丢失时间的速率至drift文件中
driftfile /var/lib/chrony/drift

#如果系统时钟的偏移量大于一秒,则允许在前三次更新中步进调整系统时钟
makestep 1.0 3

#启用实时时钟(RTC)的内核同步。
rtcsync

#在公网NTP服务器不可用时,使用本地时间来作为同步标准
local stratum 10

#指定包含 NTP 身份验证密钥的文件。
keyfile /etc/chrony.keys

#指定存放日志文件的目录
logdir /var/log/chrony

### 注意:详细指令参数可以使用命令 man chrony.conf查看


#######修改后重启chronyd服务
systemctl restart chronyd.service
systemctl status chronyd.service

操作

查看时间同步源信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
chronyc sources -v

210 Number of sources = 2

  .-- 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
===============================================================================
^+ 120.25.115.20                 2   6   377     3  +2401us[+2401us] +/-   24ms
^* 203.107.6.88                  2   6   377    68  -6205us[-6198us] +/-   19ms

查看时间同步源状态

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
chronyc sourcestats -v
210 Number of sources = 2
                             .- 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
==============================================================================
120.25.115.20              18  11   913     -1.137      2.554  +2261us   761us
203.107.6.88               16   8   847     -0.069      2.054  -2006us   508us

说明:

^* #已同步 ^? #未同步

常用命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#查看时间同步源:
chronyc sources -v
立即手工同步
chronyc -a makestep
#查看时间同步源状态:
chronyc sourcestats -v
#设置硬件时间, 硬件时间默认为UTC:
timedatectl set-local-rtc 1
#启用NTP时间同步:
timedatectl set-ntp yes
#校准时间服务器(use):
#如果该命令返回结果为 Leap status : Normal,则代表同步过程正常。
chronyc tracking   
最后需要注意的是,配置完/etc/chrony.conf后,需重启chrony服务,否则可能会不生效

问题处理

启动问题

服务启动后,使用systemctl status chronyd.service查看时,显示Could not open IPv6 command socket : Address family not supported by protocol。此问题是由于服务器没有ipv6地址导致(我这里是服务器禁用了ipv6)。

解决办法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
vim /usr/lib/systemd/system/chronyd.service

......
###添加-4 使主机名仅解析为ipv4地址,并且仅创建ipv4套接字
ExecStart=/usr/sbin/chronyd -4 $OPTIONS


#######修改后重启chronyd服务
systemctl daemon-reload
systemctl restart chronyd.service
systemctl status chronyd.service

参考链接

问题处理

Buy me a coffee
支付宝
微信
0%