linux时间同步工具chrony的配置和时间设置的相关说明

目录

目录

介绍

1.搭建ntp服务器

 2.配置ntp客户端

3.其他设置

 4.客户端无法进行时间同步


介绍

目前比较流行的时间同步工具有ntpd和chrony,ntpd采用123/UDP端口通信,chrony采用323/UDP端口通信。Centos7以上版本默认安装chrony服务来同步时间,而Centos6则默认安装ntpd服务。如果chrony作为时间同步的服务器,如下图,Chrony默认listen端口是323,通过 323 端口与 chrond 交互,可监控 chronyd 的性能并在运行时更改各种操作参数;chronyd 在后台静默运行并通过 123 端口与时间服务器定时同步时间。机器2作为时间服务器,则需要开放123/udp给客户端服务器。

选择 chrony 是有原因的,相比 ntpd , 它有如下的优点:

1、时间同步的速度比 ntpd 更快

2、chrony 很好的处理了同步延迟以及网络延迟

3、即使出现网络降级,chrony 仍然能正常工作

4、本地机器可以作为时间服务器,其他机器从这台服务器上同步时间

# 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
server ntp.aliyun.com iburst
#server ntp-timer iburst minpoll 2 maxpoll 6
#iburst:在初次连接该服务器时,快速发送一组请求包(默认是 4 个)以加速同步。
#默认的 poll 时间间隔范围是:minpoll = 6(64 秒)maxpoll = 10(1024 秒)
#minpoll 太小(如 2)会造成频繁同步,占用更多网络资源,建议只在需要高精度、可靠网络环境下设置。
#常见推荐值:稳定系统:minpoll 6 maxpoll 10 
#时间敏感系统:minpoll 4 maxpoll 6 
#实时或嵌入式系统:minpoll 2 maxpoll 4


# Record the rate at which the system clock gains/losses time.

# 记录系统时钟获得/丢失时间的速率至drift文件中
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
# 默认情况下,chronyd通过减慢或加快时钟速度来逐渐调整时钟。如果时钟与实际时间偏差太大,则需要很长时间才能纠正错误。这种方法叫做步进时钟(时间跳变)。
# 此处表示如果时间偏差调整值大于1000秒(16分钟),则立即调整系统时钟,Chrony 允许在前 10 次更新中立即调整系统时钟。超过 10 次后,即使时间偏差超过 1000 秒,Chrony 也将采用逐步微调的方式来同步时间。
makestep 1000 10
#可选的,如果不允许时间偏差。则使用下面这句。当时间偏差超过 1 秒时,chronyd 将立即调整系统时钟,且这种调整次数不受限制。
#makestep 1.0 -1

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

# Enable hardware timestamping on all interfaces that support it.
#通过使用hwtimestamp指令启用硬件时间戳
#hwtimestamp eth0
#hwtimestamp eth1
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
#语法相同,指令也可以多次使用。
#只允许192.168.网段的客户端进行时间同步
#allow 192.168.0.0/16
#拒绝192.168.网段的客户端进行时间同步
#deny 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

卸载原来的ntp同步的服务

#1)ubuntu

#如果存在ntpd服务,则禁止
if systemctl status ntpd|grep -q -E "running|enabled";then
    systemctl disable ntpd --now
fi

#如果存在ntp服务,则禁止,且卸载
if systemctl status ntp|grep -q "Loaded";then
    systemctl disable ntp --now
    apt-get remove -y --purge ntp && sleep 2
    apt -y autoremove&&sleep 1
fi

#如果存在systemd-timesyncd,则卸载
if systemctl status systemd-timesyncd.service|grep -q "Loaded";then
    systemctl disable systemd-timesyncd.service
    apt-get remove -y --purge systemd-timesyncd && sleep 2
    apt -y autoremove && sleep 1
    
fi

apt install -y chrony 
systemctl enable chronyd.service --now
timedatectl set-ntp true

#2)ctyunos

#如果存在ntpd服务,则禁止
if systemctl status ntpd|grep -q -E "running|enabled";then
    systemctl disable ntpd --now
fi

#如果存在ntp服务,则禁止,且卸载
if systemctl status ntp|grep -q "Loaded";then
    systemctl disable ntp --now
    apt-get remove -y --purge ntp && sleep 2
    apt -y autoremove&&sleep 1
fi

#如果存在systemd-timesyncd,则卸载
if systemctl status systemd-timesyncd.service|grep -q "Loaded";then
    #systemd-timesyncd 是 systemd 主包的一部分,卸载 systemd 会破坏整个系统
    systemctl stop systemd-timesyncd.service
    systemctl disable systemd-timesyncd.service
    systemctl mask systemd-timesyncd.service # 屏蔽服务,防止被其他程序重新启用 
fi

yum install -y chrony 
systemctl enable chronyd.service --now
timedatectl set-ntp true

1.搭建ntp服务器

# Centos7.6已默认安装,查看状态
systemctl status  chronyd
# 注释其他server开头的配置,添加阿里云NTP公共时间同步服务器
vim /etc/chrony.conf

server ntp.aliyun.com iburst
#makestep <threshold> <limit>
#threshold单位为秒。当本地时钟与 NTP 时间源的偏差超过这个值时,允许立即跳变。<limit>:允许 step 的最大次数,或设为 -1 表示无限次。
makestep 1.0 -1
allow 0.0.0.0/0 #或者allow all
local stratum 10
# 重启chronyd
systemctl restart   chronyd
# 查看时间同步源,查看时间同步进度
chronyc sources –v

 2.配置ntp客户端

参考上图,客户端与服务端的区别就在于把自己的server指定为服务主机,而服务端的server就指向上游(外网)NTP服务器。

Chrony默认listen端口是323, 如果有其他client想要通过123端口访问,需要将server配置为NTP的server(listen 123),这里只需要allow 子网即可。默认情况下不允许任何客户端访问,即 chronyd 纯粹作为 NTP 客户端运行。 如果使用了 allow 指令,chronyd 将既是其服务器的客户端,又是其他客户端的服务器。

# Centos7.6已默认安装,查看状态
systemctl status  chronyd
# # 注释其他server开头的配置,添加本地NTP公共时间同步服务器
vim /etc/chrony.conf
server 192.168.66.71 iburst
makestep 1.0 10
# 重启chronyd
systemctl restart  chronyd
# 查看时间同步源,查看时间同步进度
chronyc -n sources –v

3.其他设置

#服务器防火墙设置
firewall-cmd --add-port=123/udp --permanent
#或者firewall-cmd --add-service=ntp --permanent 
firewall-cmd --reload

#服务器和客户端的时区设置
timedatectl set-timezone Asia/Shanghai
timedatectl set-ntp yes
#设置系统时间的时间日期,当NTP service: active状态,则应该使用date命令"Failed to set time: Automatic time synchronization is enabled"
timedatectl set-time "2025-01-16 09:58:10"
timedatectl set-time "15:58:30"
timedatectl set-time "2015-11-20"

#查看时间
date #系统时间system time(Wall Time)
date -s "2022-11-15 10:50:00" && hwclock --systohc
hwclock --show #硬件时间(RTC)
#当系统时钟与硬件时钟不一致时,可进行同步到同一时间
hwclock --systohc    #以系统时钟为准,同步硬件时钟
hwclock --hctosys    #以硬件时钟为准,同步系统时钟

#如果是12小时制,想改为24小时制
update-locale LC_TIME=C.UTF-8

#dns
223.5.5.5       # 阿里DNS
223.6.6.6       # 阿里DNS
180.76.76.76    #百度DNS
8.8.8.8         # google域名服务器
114.114.114.114 # 是国内第一个、全球第三个开放的DNS服务地址,又称114DNS

#ntp
ntp.aliyun.com     # 阿里NTP授时服务器地址
ntp.tencent.com    # 腾讯云NTP授时服务器地址
time.edu.cn        # 教育网内的授时服务器地址
timedatectl

               Local time: Mon 2024-04-01 16:24:49 CST
           Universal time: Mon 2024-04-01 08:24:49 UTC
                 RTC time: Mon 2024-04-01 08:11:55
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
Local time           本地时间 通常为 RTC + 时区
Universal time       系统时间一直是UTC
RTC time             硬件时钟的时间,一般也 BIOS 时间
Time zone            时区, set-timezone就是设置的这个
NTP enabled          是否开启ntp时间同步
NTP synchronized     NTP同步是否完成
RTC in local TZ      是否设置RTC时间,set-local-rtc 选项就是控制该处
DST active           夏令时

 4.客户端无法进行时间同步

如果服务器之上存在防火墙,这时客户端向服务器请求时间不会有任何的响应,在客户端机器输入 chronyc sources 命令可以看到 Reach 字段的值为 0 ,表示客户端无法收到服务器的响应了。因此先排查防火墙问题。

如果排除了防火墙的问题的话,可能需要使用一些工具(tcpdump、nc)检查下客户端是否有收到服务器的数据包。

5.时间同步功能正确,但是无法立即同步

重启chronyd服务并不能之间同步,它会根据配置中的阈值进行跳步调整。如果没有超过该阈值则是逐步微调(slew)。

makestep <threshold> <limit>

因此我们需要手动进行强制同步时间,命令如下

chronyc  -a makestep

扩展:逐步微调的方式

chronyd 通过逐步微调(slew)方式来校准系统时钟,即通过细微调整时钟的运行速度,使其逐渐与参考时间源同步。这种方法避免了时间的突然跳跃,确保系统时间的平滑过渡。

每秒偏差调整量:

在逐步微调模式下,chronyd 调整系统时钟的速度,以每秒 0.5 毫秒(ms/s)的速率进行校正。这意味着,每经过 2000 秒,系统时钟将被调整 1 秒。例如,如果系统时钟与参考时间源存在 600 秒的偏差,chronyd 将需要约 14 天的时间来完全消除这一偏差。

注意事项:

  • 逐步微调适用于系统时间与参考时间偏差在 128 毫秒至 600 秒之间的情况。

  • 对于超过 600 秒的偏差,可能需要手动干预或配置 chronyd 以更快地同步时间。

总而言之,chronyd 的逐步微调方式通过细微调整系统时钟的速度,确保时间同步的平滑性和系统的稳定性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值