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

相关推荐

  • DNS服务器搭建与正反解析配置

    DNS服务介绍 DNS(Domain Name System–域名系统),是因特网的一项服务。它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网。是一个应用层的协议DNS使用TCP和UDP端口53。 DNS是一个分布式数据库,命名系统采用层次的逻辑结构,如同一颗倒置的树,这个逻辑的树形结构称为域名空间,由于DNS划分了域名空间,所以…

    2024年5月15日
    61300
  • yum 安装部署 docker-ce

    一、yum安装Docker 本案例要求配置本地yum源并安装Docker: 准备1台虚拟机,IP为192.168.137.100, 网络设置为NAT模式 安装docker 关闭防火墙 关闭SELINUX 实现此案例需要按照如下步骤进行。   步骤一:配置yum源 1、Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提…

    Linux系统 2023年2月8日
    80500
  • linux双向重定向之tee命令

    tee的作用是将一份标准输入多重定向,一份重定向到标准输出/dev/stdout,然后还将标准输入重定向到每个文件FILE中。 在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >rumenz.txt,这时不能看到输出了,如果我们既想把输出保存到文件中,又想在屏幕上看到输出内容,就可以使用tee命令了. tee流程示意图 命令语法 > tee…

    2023年9月20日
    80900
  • CentOS7搭建LNMP环境教程

    一、安装nginx 创建 nginx.repo 文件 安装配置nginx 配置文件详解 启动并验证 二、安装MariaDB 安装mariadb 使用Mysql需先初始化密码 三、安装PHP 更新yum源 执行下面的命令安装所需的包 启动php-fpm 创建测试文件 访问服务器,如果出现如下界面表示安装完成! 这样Lnmp环境就快速搭建完成了!!!

    2022年11月15日
    1.2K00
  • 系统之眼!Linux系统性能监控工具Glances

    一、Glances介绍 glances是一个基于python语言开发,可以为linux或者UNIX性能提供监视和分析性能数据的功能。glances在用户的终端上显示重要的系统信息,并动态的进行更新,让管理员实时掌握系统资源的使用情况,而动态监控并不会消耗大量的系统资源,比如CPU资源,通常消耗小于2%,glances默认每两秒更新一次数据。同时glances…

    2023年8月15日
    1.0K00

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信