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安装noVNC,以Web方式交付VNC远程连接

    什么是noVNC noVNC 是一个 HTML5 VNC 客户端,采用 HTML 5 WebSockets, Canvas 和 JavaScript 实现,noVNC 被普遍用在各大云计算、虚拟机控制面板中,比如 OpenStack Dashboard 和 OpenNebula Sunstone 都用的是 noVNC。 noVNC 采用 WebSockets…

    2024年5月8日
    1.0K00
  • 使用openssl自签发泛域名证书

    使用openssl自签发泛域名证书 openssl自签发泛域名(通配符)证书 首先要有openssl工具,如果没有那么使用如下命令安装: [root@sre ~]# yum install -y openssl openssl-devel 修改openssl.cnf配置文件 具体修改如下 [root@sre ~]# vim /etc/pki/tls/open…

    Linux系统 2023年1月31日
    1.4K00
  • Linux系统下配置双网卡只能ping通一个解决方案

    解决办法:第一步:找到两块网卡名称,再执行关闭反向路由检查(命令中第二及第三项根据实际网卡名来替换) 第二步:配置完之后你会发现双IP都能ping通了,但如服务器重启后将会默认开启,所以还要执行下一步骤来防止每次开机自动关闭反向路由检查,将以上命令加入 /etc/rc.local 即可。 其他:如涉及到此文件权限可以给/etc/rc.local文件添加执行权…

    2023年5月5日
    1.9K00
  • 如何升级vCenter(VCSA)补丁?

    VCSA的升级分为repo源和iso镜像两种方式,由于使用repo源进行升级只支持HTTPS和FTPS协议,需要额外搭建环境,略复杂,本文介绍使用iso镜像方式升级。 一、下载补丁: 1、登录VMware官网下载最新的补丁包:(需要注册VMware会员) 下载地址:https://my.vmware.com/cn/group/vmware/patch#sea…

    2023年2月4日
    1.0K00
  • WordPress升级更新方法详解 –

    为什么Wordpress要升级? 修复上一版本所存在的功能样式BUG,增加新的网站功能,修复网站漏洞,及时打补丁 所以WP更新要准时,而且版本也不要相隔太远,否则升级就有可能出错。而且升级前一定要先把网站备份下来 准备工作:  备份!!!! 1、备份数据库 mysqldump -uroot -p -B wordpress > /root/wo…

    2023年2月27日
    1.2K00

发表回复

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

在线咨询: QQ交谈

邮件:712342017@qq.com

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

关注微信