Centos7 chrony 时间同步管理工具

chrony 是什么?

Chrony是一个开源的自由软件,像CentOS 7或基于RHEL 7操作系统,已经是默认服务,默认配置文件在 /etc/chrony.conf 它能保持系统时间与时间服务器(NTP)同步,让时间始终保持同步。相对于NTP时间同步软件,占据很大优势。其用法也很简单。

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

ntp 时间同步的缺点

  1. ntpd 有一个自我保护设置: 如果本机与上源时间相差太大, ntpd 不运行. 所以新设置的时间服务器一定要先 ntpdate 从上源取得时间初值, 然后启动 ntpd服务。ntpd服务 运行后, 先是每64秒与上源服务器同步一次, 根据每次同步时测得的误差值经复杂计算逐步调整自己的时间, 随着误差减小, 逐步增加同步的间隔. 每次跳动, 都会重复这个调整的过程.
  2. 如果时间时间和ntp 时间相差特别多的化需要手动同步一下时间,所以要么你把时间设置回去,要么先做一个手动同步,以减少同步时间

chrony 的优势

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

chrony 的使用

安装

系统默认已经安装,如过没有安装

yum install chrony -y

启动与停止

systemctl start chronyd.service
systemctl stop chronyd.service

开机自启动
systemctl enable chronyd.service

防火墙设置

firewall-cmd --add-service=ntp --permanent
firewall-cmd --reload

配置文件说明

cat /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
pool.ntp.org项目中的公共服务器。以server开,理论上你想添加多少时间服务器都可以。

# 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

# 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  // 将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)
# 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
local stratum 10

// 指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
allow 0.0.0.0/0  
allow ::/0

配置内网 chrony 时间服务器

在server 端配置文件 etc/chrony.conf 修改如下,

server 10.15.10.66 iburst

其他客户端配置文件指明如下,删除或注释公用的ntp 服务器

server 10.15.10.66 iburst # 指明

重启服务chronyd

查看同步状态

chronyc sources -v

Centos7 chrony 时间同步管理工具

查看时间同步源状态:

chronyc sourcestats -v

检查 同步状态
timedatectl

当 NTP synchronized: yes
设置硬件时间

硬件时间默认为UTC:

timedatectl set-local-rtc 1

启用NTP时间同步:
timedatectl set-ntp yes

校准时间服务器:
chronyc tracking

参考博客:https://www.cnblogs.com/zhoul/p/9924656.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章