Chrony 不是某个标准缩写 ,一般就作为软件套件名称使用,写作 chrony。
它的作用可以理解为:
chrony 是 Linux 上用于时间同步的一套 NTP 实现。
官方对它的定义是:chrony 是 Network Time Protocol,也就是 NTP 网络时间协议 的一种实现,可以用来同步系统时间,也可以作为 NTP 服务端给其他机器提供时间服务。
chrony是网络时间协议 (NTP) 的一种多功能实现。
https://chrony-project.org/?utm_source
一、背景说明
生产环境中,服务器时间不准确会影响日志排查、备份任务、监控告警、数据库审计等工作。本次操作用于将 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
这样可以避免服务器重启后,又从错误的硬件时间起步。
系统时间与硬件时钟区别如下:
| 类型 | 查看命令 | 作用 |
|---|---|---|
| 系统时间 | date |
Linux 当前运行时使用的时间 |
| NTP 时间 | chronyc tracking |
网络时间源提供的标准时间 |
| 硬件时钟 RTC | hwclock --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 服务器,日志、备份任务、监控告警等时间线也会更加准确。