Linux 时间同步 Chrony

Linux 时间同步 Chrony

Chrony是NTP(Network Time Protocol,网络时间协议,服务器时间同步的一种协议)的另一种实现,与ntpd不同,它可以更快且更准确地同步系统时钟,最大程度的减少时间和频率误差。

在CentOS6中,默认是使用ntpd来同步时间的,但ntpd同步时间并不理想,有可能需要数小时来同步时间。而且ntp也已经很老了。所以在Centos7中换成了chrony来实现时间同步。chrony并且兼容ntpd监听在udp123端口上,自己则监听在udp的323端口上。

如果在chrony配置文件中指定了ntp服务器的地址,那么chrony就是一台客户端,会去同步ntp服务器的时间,如果在chrony配置了允许某些客户端来向自己同步时间,则chrony也充当了一台服务器,所以,安装了chrony即可充当客户端也可以充当服务端。一般我们只需要做客户端就好,不需要对外提供时间同步功能。

一、chrony是两个用来维持计算机系统时钟准确性的程序,这两个程序命名为chronyd和chronyc。

chronyd是一个在系统后台运行的守护进程。主要用于调整内核中运行的系统时间和时间服务器同步,他根据网络上其他时间服务器时间来测量本机时间的偏移量从而调整系统时钟。对于孤立系统,用户可以手动周期性的输入正确时间(通过chronyc)。在这两种情况下,chronyd决定计算机快慢的比例,并加以纠正。chronyd实现了NTP协议并且可以作为服务器或客户端。
chronyc是用来监控chronyd性能和配置其参数的用户界面。他可以控制本机及其他计算机上运行的chronyd进程。

服务unit文件: /usr/lib/systemd/system/chronyd.service
监听端口: 323/udp,123/udp
配置文件: /etc/chrony.conf

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

与其它时间同步软件的对比:https://chrony.tuxfamily.org/comparison.html

三、chrony 的说明

yum -y install chrony                    #安装chrony        

systemctl enable chronyd                 #设置开机自启chrony
systemctl start chronyd

chrony.conf 默认配置


vim /etc/chrony.conf
# 使用 pool.ntp.org 项目中的公共服务器。以server开,理论上想添加多少时间服务器都可以。
# 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
# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# 如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进。
# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
makestep 1.0 3

# 启用实时时钟(RTC)的内核同步。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# 通过使用 hwtimestamp 指令启用硬件时间戳
# 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

# 指定 NTP 客户端地址,以允许或拒绝连接到扮演时钟服务器的机器
# 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

# 指定包含 NTP 身份验证密钥的文件。
# 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

四、chrony的使用

ntp 服务器:

1.服务端配置

chrony.conf 修改两处

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server s1a.time.edu.cn iburst
server ntp.aliyun.com iburst

# Allow NTP client access from local network.
allow 192.168.8.0/24

开启同步

systemctl enable chronyd
systemctl restart chronyd

# 查看时间同步状态
timedatectl status
# 开启网络时间同步
timedatectl set-ntp true

2.客户端配置

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.8.5 iburst

# Allow NTP client access from local network.
allow 192.168.8.5

开启同步

systemctl enable chronyd
systemctl restart chronyd

# 查看时间同步状态
timedatectl status
# 开启网络时间同步
timedatectl set-ntp true

五、chrony其他命令

chronyc 用法

# 查看 ntp_servers
chronyc sources -v

# 查看 ntp_servers 状态
chronyc sourcestats -v

# 查看 ntp_servers 是否在线
chronyc activity -v

# 查看 ntp 详细信息
chronyc tracking -v

修改时区

# 查看日期时间、时区及 NTP 状态
timedatectl

# 查看时区列表
timedatectl list-timezones
timedatectl list-timezones | grep -E "Asia/S.*" # 修改时区
timedatectl set-timezone Asia/Shanghai

# 修改日期时间(可以只修改其中一个)
timedatectl set-time "2019-09-19 15:50:20" # 开启 NTP
timedatectl set-ntp true/flase

文章来源:https://www.cnaaa.net,转载请注明出处:https://www.cnaaa.net/archives/6578

(0)
郭靖的头像郭靖
上一篇 2023年1月5日 下午4:53
下一篇 2023年1月6日 下午5:35

相关推荐

  • CentOS 7搭建本地yum源和局域网yum源

    看到这个都是情非得已的,话不多说,直接盘 搭建本地yum源 本地yum源,就意味着只有搭建该yum源的这台服务器能使用,其它的服务器都不能使用该yum源。搭建本地yum源的所有步骤如下: #准备好一个centos 的镜像,我这里是CentOS-7-x86_64-DVD-1810.iso # 在/local-yum目录创建挂载镜像的文件夹 # 将iso镜像挂载…

    2023年5月4日
    28300
  • 解决Composer Installing dependencies from lock file

    1、问题描述 2、原因 这是因为不匹配composer.json要求的版本。提示我的PHP 7版本太高,不符合composer.json需要的版本,但是在PHP 7下应该也是可以运行的,composer可以设置忽略版本匹配。 3、解决方案 composer install –ignore-platform-reqs 或者 composer update -…

    2023年2月14日
    53500
  • Grafana如何自定义告警消息

    以下是一个使用pushgateway获取磁盘健康状态的报警消息。 可以看到,grafana默认的模板有很多无用内容。 获取数据 在grafana的报警预览中,只有state和info两个列显示。 info内容看起来是一个键值对类型的数据,其实只是一个字符串,无法从中获取我们想要的键值对。 我们通过go templete语法,提取字符串里的数据。 将该内容填写…

    2022年12月8日
    2.3K00
  • Zabbix的Mysql数据库内存占用高问题分析与处理

    问题:1、Zabbix内存使用率高;2、MariaDB没有开启独享表空间 ibdata1是InnoDB的共有表空间,默认情况下会把表空间存放在一个文件ibdata1中,会造成这个文件越来越大. 原因1:使用InnoDB共享表空间存储数据 参数innodb_file_per_table,控制innodb引擎采用共享表空间存储还是独立表空间存储。 参数innod…

    2022年12月30日
    57100
  • Ubuntu系统如何配置镜像源

    我们在使用Linux系统时,一般来说都是需要配置一个源地址才能直接使用下载的命令来安装软件的,若你购买的是云服务器,正常来讲是服务器厂商配置好了源的,但是也不缺乏有少量的机器源会出现问题,导致安装软件不成功,如下图所示,那么我们就需要手动去配置了 第一步:替换原有的源 第二步:安装证书 如果安装失败,重新安装即可

    2022年6月14日
    76100

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

在线咨询: QQ交谈

邮件:712342017@qq.com

工作时间:周一至周五,8:30-17:30,节假日休息

关注微信