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

相关推荐

  • diff命令比较两个文件的不同

    [语法]: diff [参数] 文件1 文件2-b 将一串空格或TAB 转换成一个空格或TAB-e 生成一个编辑角本,作为ex 或ed 的输入可将文件1 转换成文件2[例子]:diff file1 file2diff -b file1 file2diff -e file1 file2 >edscriptdiff 命令的常用参数参数 说明a 将所有文件当…

    2023年11月29日
    12700
  • ARM服务器快速测速

    下载speedtest镜像[arm64] 项目地址:https://github.com/cdhtlr/MikroTik-Speedtest/ root@nanopik2-s905:~# docker run -itd –name speedtest -p 8080:80 -e ‘MAX_KB=1000’ -e ‘THRESHOLD_MBPS=1.0’ -…

    2023年1月20日
    31200
  • linux下如何mount挂载lvm磁盘

    磁盘如果是普通的分区,比如:ntfs、ext3等,可以直接用 mount -t <type>来制定文件系统类型来挂载,但有时候磁盘是采用的lvm卷的分区,要挂载这类磁盘,就应该按照以下方式: a. 扫描逻辑卷: b. 激活扫描到的卷: c. 挂载 注:/dev/VolGroup00文件夹下面可能会有几个:lvm_root、lvm_swap、lvm…

    2023年7月20日
    19100
  • 关于磁盘IO的那些事

    关于磁盘IO的那些事 一、背景 需要把生产上一份clickhouse的备份数据到测试环境上,心想也就59G 的大小,应该很快吧,殊不知压缩就搞了3个多小时,于是好奇看下磁盘IO的一些参数,分享给大家学习参考 在分析存储性能之前,需要先了解存储性能定义的三个核心指标:IOPS、Throughput、await 指标 说明 IOPS 每秒进行的IO操作次数 Th…

    2023年4月11日
    20600
  • linux手误rm后恢复文件

    误删文件还原可以分为两种情况 今天只分析文件被删除后,相关进程还存在的情况 这种一般是有活动的进程存在持续标准输入或输出,到时文件被删除后,进程PID依旧存在。这也是有些服务器删除一些文件但是磁盘不释放的原因 案例演示 创建一个文件 用tail -f打开rumenz.txt文件 目的是让rumenz.txt被删除后,进程依然存在 新开一个终端删除rumenz…

    2023年10月17日
    30300

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信