首页 文章详情

服务部署-Chrony时间同步服务

开源Linux | 267 2021-06-13 09:51 0 0 0
UniSMS (合一短信)
关注「开源Linux」,选择“设为星标”
回复「学习」,有我为您特别筛选的学习资料~

一、软件介绍

Chrony 是一个开源的自由软件,它能帮助你保持系统时钟与时钟服务器同步,因此让你的时间保持精确。它由两个程序组成,分别是 chronyd 和 chronyc。

工作模式

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

  • chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

软件优势

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

二、安装启用

CentOS7 已经默认安装有 Chrony 工具,其既可作时间服务器服务端,也可作客户端。而且性能比 ntp 要好很多、配置简单、管理方便。

  • [1] 安装启动
# 安装服务
yum install -y chrony

# 启动服务
systemctl start chronyd.service

# 设置开机自启动,默认就是enable的
systemctl enable chronyd.service
  • [2] 防火墙配置
# 因NTP使用123/UDP端口协议,所以允许NTP服务即可
firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload
chronyc -a makestep
  • [3] 设置时区
# 查看日期时间、时区及NTP状态
timedatectl

# 查看时区列表
timedatectl list-timezones

# 修改时
timedatectl set-timezone Asia/Shanghai

# 修改日期时间
timedatectl set-time "2015-01-21 11:50:00"

# 设置完时区后,强制同步下系统时钟
chronyc -a makestep
  • [4] 图形化界面
yum -y install system-config-date

三、主要配置

当 Chrony 启动时,它会读取 /etc/chrony.conf 配置文件中的设置,配置内容格式和 ntpd 服务基本相似。

  • 主要配置选项
  • 这里使用的是网络上提供的时间服务,如果本局域网内有对时服务开启的话,通过将上面的几条serer记录删除,增加指定局域网内的对时服务器并restart chrony服务即可。
[root@localhost ~]# cat /etc/chrony.conf |grep -v ^#|grep -v ^$
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
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony

四、查看状态

  • [1] 检查 ntp 源服务器状态
[root@localhost ~]# chronyc sourcestats
210 Number of sources = 3
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
dns.sjtu.edu.cn             4   3   302      6.440     90.221    +13ms   694us
dns1.synet.edu.cn           0   0     0      0.000   2000.000     +0ns  4000ms
202.118.1.130               7   5   323     -0.174      7.323  -8406ns   303us
  • [2] 检查 ntp 详细同步状态
[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
||                                                /   xxxx = adjusted offset,
||         Log2(Polling interval) -.             |    yyyy = measured offset,
||                                  \            |    zzzz = estimated error.
||                                   |           |
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^- dns.sjtu.edu.cn               3   7    13    20    +11ms[  +11ms] +/-   98ms
^? dns1.synet.edu.cn             0   8     0   10y     +0ns[   +0ns] +/-    0ns
^* 202.118.1.130                 2   6   377   125   -122us[ -305us] +/-   31ms
  • [3] 设置硬件时间
# 硬件时间默认为UTC
$ timedatectl set-local-rtc 1

# 启用或关闭NTP时间同步
$ timedatectl set-ntp yes|flase

# 校准时间服务器
$ chronyc tracking

五、工具使用

可以通过运行 chronyc 命令来修改设置,以下是常用命令。

  • accheck

检查 NTP 访问是否对特定主机可用

  • activity

该命令会显示有多少 NTP 源在线/离线

  • add server

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

  • clients

在客户端报告已访问到服务器

  • delete

手动移除 NTP 服务器或对等服务器

  • settime

手动设置守护进程时间

  • tracking

显示系统时间信息

  • help

可以查看更多 chronyc 的交互命令

[root@localhost ~]# chronyc
chrony version 1.29.1
Copyright (C) 1997-2003, 2007, 2009-2013 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> activity
200 OK
3 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
chronyc> help
Commands:
accheck <address> : Check whether NTP access is allowed to <address>
activity : Check how many NTP sources are online/offline
add peer <address> ... : Add a new NTP peer
add server <address> ... : Add a new NTP server
allow [<subnet-addr>] : Allow NTP access to that subnet as a default
allow all [<subnet-addr>] : Allow NTP access to that subnet and all children
burst <n-good>/<n-max> [<mask>/<masked-address>] : Start a rapid set of measurements
clients : Report on clients that have accessed the server
.......

文章作者:Escape

链接:https://escapelife.github.io/posts/9587ae84.html

整理:微信公众号,开源Linux

关注「开源Linux」加星标,提升IT技能

good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter