服务器 > 服务器 > Linux

Linux服务器Chrony时间同步配置与校准的操作指南

13人参与 2026-08-05 Linux

chrony 不是某个标准缩写,一般就作为软件套件名称使用,写作 chrony

它的作用可以理解为:

chrony 是 linux 上用于时间同步的一套 ntp 实现。

官方对它的定义是:chrony 是 network time protocol,也就是 ntp 网络时间协议 的一种实现,可以用来同步系统时间,也可以作为 ntp 服务端给其他机器提供时间服务。

一、背景说明

生产环境中,服务器时间不准确会影响日志排查、备份任务、监控告警、数据库审计等工作。本次操作用于将 linux 服务器统一同步到公司内网 ntp 时间服务器,并在同步完成后写入硬件时钟 rtc。

说明:本文中的 ip 地址均为示例地址,实际操作时请替换为现场真实地址。

示例环境:

目标服务器ip:192.168.100.10
内网ntp服务器ip:192.168.100.1

二、检查当前系统时间

登录目标服务器后,先查看当前系统时间:

date

示例:

2026年 06月 23日 星期二 07:44:23 cst

如果发现服务器时间与实际时间存在明显偏差,例如快了或慢了几分钟,需要继续检查时间同步配置。

三、检查 ntp 服务器网络连通性

先确认目标服务器能否访问内网 ntp 服务器:

ping -c 4 192.168.100.1

正常结果类似:

64 bytes from 192.168.100.1: icmp_seq=1 ttl=63 time=0.100 ms
64 bytes from 192.168.100.1: icmp_seq=2 ttl=63 time=0.095 ms
64 bytes from 192.168.100.1: icmp_seq=3 ttl=63 time=0.102 ms
64 bytes from 192.168.100.1: icmp_seq=4 ttl=63 time=0.098 ms

如果出现:

destination host unreachable
100% packet loss

说明目标服务器到 ntp 服务器网络不可达,需要先检查网关、路由、防火墙、vlan 或 ntp 服务器状态。

需要注意:

检查项协议/端口说明
ping 连通性测试icmp 协议不使用 tcp/udp 端口
ntp 时间同步udp 123 端口chrony/ntp 实际同步时间使用

也就是说,ping 只是基础网络连通性检查,不代表 ntp 服务一定正常。
如果网络策略较严格,需要确认目标服务器到 ntp 服务器的 udp 123 端口 已放通。

四、检查时间同步服务

查看 chronyd 服务状态:

systemctl status chronyd --no-pager

正常应看到:

active: active (running)

如果 chronyd 未启动,可以执行:

systemctl enable chronyd
systemctl start chronyd

五、查看当前 chrony 配置

查看当前配置的时间源:

grep -ne '^\s*(server|pool)' /etc/chrony.conf

可能看到类似默认公网 ntp 源:

3:server 0.centos.pool.ntp.org iburst
4:server 1.centos.pool.ntp.org iburst
5:server 2.centos.pool.ntp.org iburst
6:server 3.centos.pool.ntp.org iburst

如果公司要求统一使用内网 ntp,建议将时间源统一调整为公司内网 ntp 服务器。

六、备份配置文件

修改前先备份:

cp -a /etc/chrony.conf /etc/chrony.conf.bak.$(date +%f_%h%m%s)

确认备份文件:

ls -lh /etc/chrony.conf.bak*

七、配置内网 ntp 服务器

编辑配置文件:

vim /etc/chrony.conf

将原有公网 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

新增公司内网 ntp 服务器:

server 192.168.100.1 iburst prefer

参数说明:

参数说明
server指定时间同步服务器
iburst服务启动后快速同步
prefer优先使用该时间源

也可以使用命令方式添加:

grep -q '^server 192.168.100.1' /etc/chrony.conf || sed -i '3iserver 192.168.100.1 iburst prefer' /etc/chrony.conf

如需批量注释默认公网源,可执行:

sed -i -e 's/^(server[[:space:]]+[0-3]\.centos\.pool\.ntp\.org[[:space:]]+iburst)/#\1/' /etc/chrony.conf

八、检查配置结果

执行:

grep -ne '^\s*(server|pool)|^#server' /etc/chrony.conf

正常结果示例:

3:server 192.168.100.1 iburst prefer
4:#server 0.centos.pool.ntp.org iburst
5:#server 1.centos.pool.ntp.org iburst
6:#server 2.centos.pool.ntp.org iburst
7:#server 3.centos.pool.ntp.org iburst

确认只有内网 ntp 服务器处于启用状态。

九、重启 chrony 服务

执行:

systemctl restart chronyd

查看服务状态:

systemctl status chronyd --no-pager

确认状态为:

active: active (running)

十、验证是否同步成功

查看当前时间同步源:

chronyc sources -n -v

正常结果应类似:

ms name/ip address         stratum poll reach lastrx last sample
===============================================================================
^* 192.168.100.1                 3   6    37     8   +1083ns[ +163us] +/- 143ms

重点看第一列:

标识含义
^*当前正在使用的时间源
^?配置了但不可达
^x时间源异常

如果看到:

^* 192.168.100.1

说明当前服务器已经成功使用内网 ntp 服务器同步时间。

十一、查看整体同步状态

执行:

chronyc tracking

正常结果示例:

reference id    : c0a86401 (192.168.100.1)
stratum         : 4
system time     : 0.000034372 seconds fast of ntp time
last offset     : +0.000162035 seconds
leap status     : normal

如果 system time 只差毫秒或微秒级,说明时间已经基本同步。

再执行:

timedatectl

重点确认:

ntp enabled: yes
ntp synchronized: yes

十二、时间偏差较大时手动校准

如果服务器时间快了或慢了几十秒、几分钟,在确认 ntp 源正常后,可以执行:

chronyc makestep

然后再次检查:

date
chronyc tracking
timedatectl

如果日志中出现类似内容:

system clock wrong by -129 seconds, adjustment started
system clock was stepped by -129 seconds

说明系统时间已经被校准。

十三、写入硬件时钟 rtc

配置里有这一行的话:

rtcsync

正常是自动写入 rtc

ntp/chrony 同步系统时间
        ↓
系统时间稳定后
        ↓
自动同步到硬件时钟 rtc

是否需要手动写入rtc最简单判断

执行:

timedatectl

看到下面这样就正常:

system clock synchronized: yes
ntp service: active
rtc in local tz: no
universal time 和 rtc time 基本一致

若不一致,再手动写入:

系统时间校准完成后,建议将当前正确的系统时间写入硬件时钟:

hwclock --systohc

查看硬件时钟:

hwclock --show

这样可以避免服务器重启后,又从错误的硬件时间起步。

系统时间与硬件时钟区别如下:

类型查看命令作用
系统时间datelinux 当前运行时使用的时间
ntp 时间chronyc tracking 网络时间源提供的标准时间
硬件时钟 rtchwclock --show服务器关机或重启后保留的底层时间

十四、最终验证

最后统一执行:

date
chronyc sources -n -v
chronyc tracking
timedatectl
hwclock --show

确认以下结果:

^* 192.168.100.1
ntp synchronized: yes
system time 偏差为毫秒或微秒级

如果以上结果正常,说明服务器时间同步配置完成。

十五、操作总结

本次时间同步处理流程如下:

1. 查看当前系统时间
2. ping 测试内网 ntp 服务器基础连通性
3. 确认 ntp 使用 udp 123 端口
4. 检查 chronyd 服务状态
5. 备份 /etc/chrony.conf
6. 配置内网 ntp 服务器
7. 注释原公网 ntp 源
8. 重启 chronyd 服务
9. 使用 chronyc sources 验证当前时间源
10. 使用 chronyc tracking 查看时间偏差
11. 必要时执行 chronyc makestep 手动校准
12. 使用 hwclock --systohc 写入硬件时钟 rtc

完成以上操作后,服务器时间会统一同步到公司内网 ntp 服务器,日志、备份任务、监控告警等时间线也会更加准确。

以上就是linux服务器chrony时间同步配置与校准的操作指南的详细内容,更多关于linux chrony时间同步配置与校准的资料请关注代码网其它相关文章!

(0)

您想发表意见!!点此发布评论

推荐阅读

Ubuntu系统下修改SSH默认端口的完整操作指南

08-05

Linux系统下新硬盘挂载的完整操作指南

08-05

在Linux系统中修改主机名的完整流程

08-05

Linux中生产环境系统资源限制的完整配置指南

08-06

Linux从闹钟到高精度定时器的实现指南

08-06

Linux系统下文件创建全攻略:从touch到vim的实战场景解析

08-06

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论