大家好,我是顺亿,今天来和大家聊聊Linux服务器时间同步这块。在实际项目中,服务器时间同步非常重要,今天我们就来详细了解一下Chrony这个工具。
什么是Chrony?
Chrony是NTP(网络时间协议)的一个实现,它可以帮助我们实现系统时钟与NTP服务器同步,或者与参考时钟(比如GPS接收器)同步,甚至是作为NTP服务器向其他计算机提供时间服务。Chrony在各种网络环境下表现都非常稳定,包括网络不稳定、温度变化等情况。
Chrony的安装和配置
# CenOS7默认已安装,Chrony守护程序的默认位置是/usr/sbin/chronyd。命令行实用程序将安装到/usr/bin/chronyc
yum install chrony
# 启动命令
systemctl start chronyd
systemctl status chronyd
systemctl enable chronyd
接下来,我们需要配置Chrony的配置文件:
# /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 配置NTP服务器
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.
# 记录系统时钟获得/丢失时间的速率至drift文件中
driftfile /var/lib/chrony/drift
# Enable kernel synchronization of the real-time clock (RTC).
# 启用RTC(实时时钟)的内核同步
rtcsync
# Allow NTP client access from local network.
# 只允许192.168.网段的客户端进行时间同步
#allow 192.168.0.0/16
# Serve time even if not synchronized to a time source.
# NTP服务器不可用时,采用本地时间作为同步标准
#local stratum 10
# Specify file containing keys for NTP authentication.
# 指定包含NTP验证密钥的文件
#keyfile /etc/chrony.keys
# Specify directory for log files.
# 指定日志文件的目录
logdir /var/log/chrony
# Select which information is logged.
# 将对系统增益或损耗率的估计值以及所做的任何转换记录的更改记录到名为的文件中tracking.log。
#log measurements statistics tracking
# 其他未在默认配置文件的配置项
# 在第一次时钟更新之后,chronyd将检查每次时钟更新的偏移量,它将忽略两次大于1000秒的调整,并退出另一个调整。
maxchange 1000 1 2
# 该rtcfile指令定义中的文件名chronyd可以保存跟踪系统的实时时钟(RTC)的精度相关的参数。
rtcfile /var/lib/chrony/rtc
配置完成后,我们需要重启Chrony守护程序,并查看同步状态:
# 重启chronyd
systemctl restart chronyd
# 查看时间同步源,查看时间同步进度
chronyc sources –v
案例:搭建NTP服务器
假设我们需要搭建一台NTP服务器,同步阿里云时间,当阿里云NTP不可用时,使用本地时间。其他服务器则同步这台服务器上的时间,确保时间一致。
# 搭建服务端
# Centos7.6已默认安装,查看状态
systemctl status chronyd
# 注释其他server开头的配置,添加阿里云NTP公共时间同步服务器
vim /etc/chrony.conf
添加内容
server ntp.aliyun.com iburst
allow 0.0.0.0/0
local stratum 10
# 重启chronyd
systemctl restart chronyd
# 查看时间同步源,查看时间同步进度
chronyc sources –v
# 配置客户端
# Centos7.6已默认安装,查看状态
systemctl status chronyd
# 注释其他server开头的配置,添加本地NTP公共时间同步服务器
vim /etc/chrony.conf
server 192.168.58.201 iburst
# 重启chronyd
systemctl restart chronyd
# 查看时间同步源,查看时间同步进度
chronyc sources –v
通过以上步骤,我们就完成了一个简单的NTP服务器的搭建。需要注意的是,这里只是最基础的配置,实际部署中可能需要根据具体需求进行调整。
好了,今天的内容就到这里,希望对大家有所帮助。我是顺亿,我们下期再见!
想要了解更多关于编程的知识,欢迎关注「趣航编程网」(www.vqhf.com)。
