CentOS 使用 chrony 实现时间同步
·
一、 同步公网的时间源
1、安装chrony
使用yum命令安装chrony
[root@localhost ~]# yum install chrony -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package chrony.x86_64 0:3.4-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================================================================================
Installing:
chrony x86_64 3.4-1.el7 base 251 k
Transaction Summary
==================================================================================================================================================================================================================
Install 1 Package
Total download size: 251 k
Installed size: 491 k
Downloading packages:
chrony-3.4-1.el7.x86_64.rpm | 251 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : chrony-3.4-1.el7.x86_64 1/1
Verifying : chrony-3.4-1.el7.x86_64 1/1
Installed:
chrony.x86_64 0:3.4-1.el7
Complete!
[root@localhost ~]#
2、查看chrony的重要配置文件
[root@localhost ~]# rpm -ql chrony
/etc/chrony.conf #chrony的主配置文件
/usr/bin/chronyc #chronyc是一个命令行交互式接口程序,可用于监视chronyd的性能,并在运行时更改各种操作参数。
/usr/lib/systemd/system/chronyd.service #CentOS 版本对应的unit file
/usr/libexec/chrony-helper
/usr/sbin/chronyd #chronyd是一个可以在启动时启动的守护程序,它既可以充当服务端进程也可以充当服务端进程
3、修改chrony配置文件
[root@localhost ~]# vi /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 ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com 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
4、查看修改了的配置文件
[root@localhost ~]# egrep -v "^#|^$" /etc/chrony.conf
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
5、启动chony
# 启动chony
[root@localhost ~]# systemctl start chronyd
# 设置开机自启
[root@localhost ~]# systemctl enable chronyd
# 查看开机自启是否设置成功
[root@localhost ~]# systemctl list-unit-files | grep chronyd
chronyd.service enabled
# 查看chony服务状态
[root@localhost ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-12-09 14:21:57 CST; 33min ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Main PID: 583 (chronyd)
CGroup: /system.slice/chronyd.service
└─583 /usr/sbin/chronyd
Dec 09 14:21:57 localhost.localdomain systemd[1]: Starting NTP client/server...
Dec 09 14:21:57 localhost.localdomain chronyd[583]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
Dec 09 14:21:57 localhost.localdomain chronyd[583]: Frequency 32.982 +/- 25.228 ppm read from /var/lib/chrony/drift
Dec 09 14:21:57 localhost.localdomain systemd[1]: Started NTP client/server.
Dec 09 14:22:06 localhost.localdomain chronyd[583]: Selected source 139.199.215.251
Dec 09 14:22:06 localhost.localdomain chronyd[583]: System clock wrong by -1.611447 seconds, adjustment started
Dec 09 14:22:04 localhost.localdomain chronyd[583]: System clock was stepped by -1.611447 seconds
Dec 09 14:22:07 localhost.localdomain chronyd[583]: Source 111.230.189.174 replaced with 94.130.49.186
[root@localhost ~]#
6、查看时间是否同步完成
1)以交互方式查看同步的时间源
[root@localhost ~]# chronyc
chrony version 3.4
Copyright (C) 1997-2003, 2007, 2009-2018 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and
you are welcome to redistribute it under certain conditions. See the
GNU General Public License version 2 for details.
chronyc> sources
210 Number of sources = 4
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^- undefined.hostname.local> 2 8 122 553 +20ms[ +20ms] +/- 213ms
^? 47.241.41.246 0 9 0 - +0ns[ +0ns] +/- 0ns
^* 202.118.1.130 1 8 377 165 +4284us[+6182us] +/- 39ms
^- stratum2-1.ntp.led01.ru.> 2 8 77 40 -15ms[ -15ms] +/- 216ms
chronyc>
^表示服务,*代表同步成功,?代表未同步
2)以交互方式查看同步是否正常
[root@localhost ~]# chronyc
chrony version 3.4
Copyright (C) 1997-2003, 2007, 2009-2018 Richard P. Curnow and others
chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and
you are welcome to redistribute it under certain conditions. See the
GNU General Public License version 2 for details.
chronyc> sourcestats
210 Number of sources = 4
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
undefined.hostname.local> 13 5 29m -0.950 56.859 -13ms 21ms
47.241.41.246 0 0 0 +0.000 2000.000 +0ns 4000ms
202.118.1.130 25 17 34m +0.157 16.586 +39us 13ms
stratum2-1.ntp.led01.ru.> 21 12 37m +10.401 34.861 +2433us 25ms
chronyc>
3)以非交互式方式查看时间同步资源信息
[root@localhost ~]# 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
===============================================================================
^- undefined.hostname.local> 2 9 41 49 +21ms[ +21ms] +/- 140ms
^? 47.241.41.246 2 9 1 110 -20ms[ -20ms] +/- 137ms
^* 202.118.1.130 1 9 376 689 -2478us[-3190us] +/- 48ms
^- stratum2-1.ntp.led01.ru.> 2 9 376 825 -47ms[ -48ms] +/- 163ms
[root@localhost ~]#
^表示服务,*代表同步成功,?代表未同步
4)开启NTP时间同步
[root@localhost ~]# timedatectl # 查看现有状态
Local time: Thu 2021-12-09 15:21:18 CST
Universal time: Thu 2021-12-09 07:21:18 UTC
RTC time: Thu 2021-12-09 07:21:18
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no # NTP可能尚未启用
NTP synchronized: no # 尚未完成过一次NTP同步
RTC in local TZ: no
DST active: n/a
[root@localhost ~]# timedatectl set-ntp yes # 启用NTP同步
[root@localhost ~]# timedatectl
timedatectl
Local time: Thu 2021-12-09 15:21:33 CST
Universal time: Thu 2021-12-09 07:21:33 UTC
RTC time: Thu 2021-12-09 07:21:33
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes # NTP已经启用
NTP synchronized: yes # 已经完成NTP同步
RTC in local TZ: no
DST active: n/a
[root@localhost ~]#
二、 设置内网主机作为 ntp 服务器
可能不是所有的机器都可以连接外网,这时候我们就需要搭建一个简单的ntp时间服务器。这里我们假设我们的ntp服务器IP为192.168.191.10,客户端的IP段为192.168.192.0/24
1、修改服务端的chrony配置文件
[root@localhost ~]# vi /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 ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com 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
# 指定允许的客户端网段来当前时间服务器节点同步时间,我们可以使用deny all拒绝所有客户端。
allow 192.168.192.0/24 # 表示192网段的IP可以过来同步时间
# 注意,如果主机位是0的话可以简写,比如下面的地址可以简写为"127/8",不过建议大家还是写完整,可读性更强。
# 如果上面使用server字段配置的时间服务器同步时间失败,默认情况下当前时间服务器是不会向客户端同步时间的,这是因为担心当前节点的时间不准确(因为当前节点没有和定义中的server时间服务器进行同步),如果我们想要在server指定的时间服务器同步失败的情况下依旧返回当前时间服务器的时间给客户端,需要开启该参数,这一项参数配置在生产环境中还是相当危险的,因此建议大家在server字段中指定互联网的网络时间,否则可能会出现整个集群时间都错的的一致!
local stratum 10
# 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
2、修改客户端的chrony配置文件
[root@localhost ~]# vi /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 192.168.191.10 iburst
# server ntp2.aliyun.com iburst
# server ntp3.aliyun.com 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
更多推荐
所有评论(0)