配置chrony时间同步服务

一、chrony简介

chrony 的优势:
更快的同步,从而最大程度减少了时间和频率误差,对于并非全天 24 小时运行的虚拟计算机而言非常有用
能够更好地响应时钟频率的快速变化,对于具备不稳定时钟的虚拟机或导致时钟频率发生变化的节能技术而言非常有用
在初始同步后,它不会停止时钟,以防对需要系统时间保持单调的应用程序造成影响
在应对临时非对称延迟时(例如,在大规模下载造成链接饱和时)提供了更好的稳定性
无需对服务器进行定期轮询,因此具备间歇性网络连接的系统仍然可以快速同步时钟
chrony官网:https://chrony.tuxfamily.org
chrony官方文档:https://chrony.tuxfamily.org/documentation.html

二、安装chrony

1、安装chrony

yum install chrony

两个主要程序:chronyd和chronyc

chronyd:后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿

chronyc:命令行用户工具,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可在一台不同的远程计算机上工作

服务unit文件: /usr/lib/systemd/system/chronyd.service

监听端口: 323/udp,123/udp

配置文件: /etc/chrony.conf

配置文件详解

server :指明时间服务器地址,iburst 选项当服务器可达时,发送一个八个数据包而不是通常的一个数据包。 包间隔通常为2秒,可加快初始同步速度

driftfile : 根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中,会在重启后为系统时钟作出补偿

rtcsync :启用内核模式,系统时间每11分钟会拷贝到实时时钟(RTC)

allow NETADD/NETMASK 自己作为ntp服务器,让客户端来同步自己的时间

allow all:允许所有客户端主机;

deny NETADDR/NETMASK

deny all:拒绝所有客户端;

cmdallow / cmddeny : 可以指定哪台主机可以通过chronyd使用控制命令

bindcmdaddress : 允许chronyd监听哪个接口来接收由chronyc执行的命令

makestep : 通常chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在调整期大于某个域值时调整系统时钟

local stratum 10 :即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端

2、配置chrony服务

systemctl start  chronyd
systemctl enable chronyd
systemctl status chronyd

3、修改配置文件,添加时间源

[root@localhost ~]# 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
##添加两个时间源,分别是阿里云和东北大学的时间同步服务器,iburst表示加急
server time1.aliyun.com iburst
server time.neu.edu.cn 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.
# chronyd根据需求减慢或加速时间调整,
# 在某些情况下系统时钟可能漂移过快,导致时间调整用时过长。
# 该指令强制chronyd调整时期,大于某个阀值时(例如1秒)步进调整系统时钟。
# 只有在因chronyd启动时间超过指定的限制时(可使用负值来禁用限制)没有更多时钟更新时才生效。
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
# 将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(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

# Allow NTP client access from local network.
# 指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
#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

4、测试
重启服务

systemctl restart chronyd
##显示当前chronyd正在同步的时间源的信息

在这里插入图片描述
三、相关命令

chronyc [OPTION]... [COMMAND]...

##查看帮助
chronyc help

##显示有多少NTP源在线/离线
[root@localhost ~]# chronyc activity
200 OK
2 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

强制同步系统时间(可以在)

[root@localhost ~]# chronyc -a makestep
200 OK

检查是否对特定主机可访问当前服务器

[root@localhost ~]# chronyc accheck 192.168.145.130
208 Access allowed
[root@localhost ~]# chronyc accheck 192.168.145.2
208 Access allowed
[root@localhost ~]# chronyc accheck 10.10.10.102
208 Access allowed

显示当前时间源的同步信息

[root@localhost ~]# chronyc sources 
210 Number of sources = 2
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 203.107.6.88                  2   6    77     3    -13ms[  -89ms] +/-   89ms
^- time.neu.edu.cn               1   6    77     1  -6618us[-6618us] +/-  133ms
##加v表示显示详细信息
[root@localhost ~]# 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               
===============================================================================
^* 203.107.6.88                  2   6    77    10    -13ms[  -89ms] +/-   89ms
^- time.neu.edu.cn               1   6    77     7  -6618us[-6618us] +/-  133ms

显示当前时间源的同步统计信息

[root@localhost ~]# chronyc sourcestats 
210 Number of sources = 2
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
203.107.6.88                9   6   524   -282.705   1115.902    -16ms    65ms
time.neu.edu.cn             9   5   521   +265.222   1210.449   +177ms    76ms
##加-v显示详细信息
[root@localhost ~]# 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
==============================================================================
203.107.6.88                9   6   524   -282.705   1115.902    -17ms    65ms
time.neu.edu.cn             9   5   521   +265.222   1210.449   +178ms    76ms

手动添加一台新的NTP服务器

##添加一台复旦大学的时间同步服务器
[root@localhost ~]# chronyc add server ntp.fudan.edu.cn
200 OK
[root@localhost ~]# chronyc sources -v
210 Number of sources = 3

  .-- 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               
===============================================================================
^* 203.107.6.88                  2   6   377    41   +139ms[ +143ms] +/-  179ms
^- time.neu.edu.cn               1   6   377   103    +38ms[  +42ms] +/-   92ms
^? 10.108.68.100                 0   7     0     -     +0ns[   +0ns] +/-    0ns
##临时添加,重启服务会失效,若想永久生效可以写入配置文件中

报告已访问本服务器的客户端列表

[root@localhost ~]# chronyc clients 
Hostname                      NTP   Drop Int IntL Last     Cmd   Drop Int  Last
===============================================================================

显示系统的时钟性能参数

[root@localhost ~]# chronyc tracking
Reference ID    : CB6B0658 (203.107.6.88)
Stratum         : 3
Ref time (UTC)  : Wed Aug 18 12:14:04 2021
System time     : 0.013744237 seconds fast of NTP time
Last offset     : +0.011746962 seconds
RMS offset      : 0.093312807 seconds
Frequency       : 3.476 ppm fast
Residual freq   : +32.767 ppm
Skew            : 95.284 ppm
Root delay      : 0.124015316 seconds
Root dispersion : 0.157856569 seconds
Update interval : 65.7 seconds
Leap status     : Normal

其他时间相关命令
设置时区

##查看当前系统时区:
[root@localhost ~]# timedatectl
      Local time: Wed 2021-08-18 20:31:45 CST
  Universal time: Wed 2021-08-18 12:31:45 UTC
        RTC time: Wed 2021-08-18 12:31:45
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

##查看所有可用的时区:
[root@localhost ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
Africa/Bujumbura
....

##查看上海时区:
[root@localhost ~]#  timedatectl list-timezones |  grep  "Asia/Shanghai"
Asia/Shanghai
##设置当前系统为Asia/Shanghai上海时区:
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai

##修改日期时间:
[root@localhost ~]# timedatectl set-time “2021-08-18 10:30:00”
##开启NTP: 
[root@localhost ~]# timedatectl set-ntp true/flase
  • 6
    点赞
  • 93
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
配置chrony时间同步,你可以按照以下步骤进行操作: 1. 安装chrony:使用适合你的Linux发行版的包管理器,如apt、yum或dnf,安装chrony软件包。 2. 配置chrony:编辑chrony配置文件,通常位于`/etc/chrony.conf`。你可以使用任何文本编辑器打开该文件。 3. 添加时间服务器:在配置文件中找到或添加server行,指定要同步时间服务器。例如: ``` server time1.example.com server time2.example.com ``` 你可以根据需要添加多个时间服务器,并按优先级排序。 4. 配置本地时钟源:如果你的系统有本地硬件时钟源,可以添加本地时钟源的配置。例如: ``` refclock PPS /dev/pps0 lock GPS ``` 这是一个使用GPS接收器提供的脉冲信号同步本地时钟的示例。 5. 配置访问控制:根据需要,可以在配置文件中添加访问控制规则,以限制哪些主机可以访问chrony服务。例如: ``` allow 192.168.0.0/24 ``` 这将允许192.168.0.0/24网段内的主机访问chrony服务。 6. 保存并关闭文件:完成配置后,保存并关闭配置文件。 7. 启动chrony服务:启动或重启chrony服务,以使配置生效。使用适合你的Linux发行版的命令,如systemctl或service。例如: ``` sudo systemctl start chronyd ``` 8. 验证时间同步:使用`chronyc sources`命令检查时间服务器的同步状态。例如: ``` chronyc sources ``` 该命令将显示已配置时间服务器和它们的同步状态。 通过按照上述步骤进行配置,你可以使用chrony实现时间同步。请确保你的系统能够访问指定的时间服务器,并且网络连接正常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值