时间同步 (chrony)
精准的系统时间对于日志记录、证书验证、分布式系统协调等场景至关重要。EL 系发行版默认使用 chrony 作为 NTP 客户端和服务端,取代了传统的 ntpd。本文将介绍 chrony 的安装配置、NTP 源管理、时区设置以及硬件时钟同步。
chrony 与 ntpd 的区别
Section titled “chrony 与 ntpd 的区别”| 特性 | chrony | ntpd |
|---|---|---|
| 同步速度 | 快速收敛,适合间歇性网络 | 收敛较慢,需要持续连接 |
| 资源占用 | 低内存、低 CPU | 相对较高 |
| 虚拟机支持 | 优秀,适应时钟漂移 | 一般 |
| EL 默认 | EL 7+ 默认 | EL 6 默认,EL 8+ 已移除 |
| 精度 | 微秒级 | 毫秒级 |
在 EL 8/9 中,ntpd 已从默认仓库移除,chrony 是唯一推荐的 NTP 实现。
安装 chrony
Section titled “安装 chrony”EL 系发行版通常默认已安装 chrony。如果没有,可以手动安装:
sudo dnf install chrony -ysudo systemctl start chronydsudo systemctl enable chronydsudo systemctl status chronyd查看同步状态
Section titled “查看同步状态”chronyc sources — 查看 NTP 源
Section titled “chronyc sources — 查看 NTP 源”chronyc sources -v输出中各列含义:
^*— 当前正在使用的源(已同步)^+— 合格的备选源^-— 不合格的源^?— 连接中或状态未知
chronyc tracking — 查看同步详情
Section titled “chronyc tracking — 查看同步详情”chronyc tracking关注以下字段:
- System time — 系统时钟与 NTP 的偏移量
- Last offset — 上一次调整的偏移量
- RMS offset — 偏移的均方根值
- Frequency — 时钟频率偏差 (ppm)
chronyc sourcestats — 查看源统计
Section titled “chronyc sourcestats — 查看源统计”chronyc sourcestats -v配置 NTP 服务器
Section titled “配置 NTP 服务器”chrony 的主配置文件位于 /etc/chrony.conf。
查看默认配置
Section titled “查看默认配置”cat /etc/chrony.conf修改 NTP 服务器
Section titled “修改 NTP 服务器”默认配置通常指向发行版的 NTP 池。可以根据需要修改为其他服务器:
sudo cp /etc/chrony.conf /etc/chrony.conf.baksudo vi /etc/chrony.conf常见的 NTP 服务器配置示例:
# 使用国内 NTP 服务器server ntp.aliyun.com iburstserver ntp.tencent.com iburstserver ntp.ntsc.ac.cn iburst
# 或使用全球 NTP 池pool 2.pool.ntp.org iburst
# 记录时钟漂移driftfile /var/lib/chrony/drift
# 允许系统时钟在前三次更新时大幅跳跃makestep 1.0 3
# 启用内核时钟同步rtcsync
# 日志目录logdir /var/log/chrony关键指令说明:
server— 指定单个 NTP 服务器pool— 指定 NTP 服务器池(自动解析多个地址)iburst— 初始同步时发送突发请求,加快同步速度makestep— 允许在指定条件下大幅调整时间(参数:阈值秒数 + 允许次数)rtcsync— 每 11 分钟将系统时间写入硬件时钟
sudo systemctl restart chronydchronyc sources配置为 NTP 服务器
Section titled “配置为 NTP 服务器”如果需要让本机为内网其他主机提供时间同步服务:
echo 'allow 192.168.1.0/24' | sudo tee -a /etc/chrony.confsudo systemctl restart chronydsudo firewall-cmd --permanent --add-service=ntpsudo firewall-cmd --reload时区管理 (timedatectl)
Section titled “时区管理 (timedatectl)”timedatectl 是 systemd 提供的时间和时区管理工具。
查看当前时间信息
Section titled “查看当前时间信息”timedatectl status列出可用时区
Section titled “列出可用时区”timedatectl list-timezonestimedatectl list-timezones | grep Asiasudo timedatectl set-timezone Asia/Shanghaisudo timedatectl set-timezone UTC启用 / 禁用 NTP 同步
Section titled “启用 / 禁用 NTP 同步”sudo timedatectl set-ntp truesudo timedatectl set-ntp false手动设置时间
Section titled “手动设置时间”在禁用 NTP 同步后,可以手动设置系统时间:
sudo timedatectl set-time '2026-03-25 14:30:00'硬件时钟同步
Section titled “硬件时钟同步”硬件时钟(RTC)是主板上的独立时钟,系统关机后仍然运行。
查看硬件时钟
Section titled “查看硬件时钟”sudo hwclock --show同步系统时钟到硬件时钟
Section titled “同步系统时钟到硬件时钟”sudo hwclock --systohc同步硬件时钟到系统时钟
Section titled “同步硬件时钟到系统时钟”sudo hwclock --hctosys设置硬件时钟使用 UTC
Section titled “设置硬件时钟使用 UTC”sudo hwclock --systohc --utc在配置了 rtcsync 指令后,chrony 会自动每 11 分钟将系统时间同步到硬件时钟,通常不需要手动操作。
强制立即同步
Section titled “强制立即同步”如果系统时间偏差较大,需要立即校正:
sudo chronyc makestep该命令会立刻将系统时间调整为 NTP 源的时间,无论偏差大小。
常见故障排查
Section titled “常见故障排查”无法同步时间
Section titled “无法同步时间”systemctl is-active chronydsudo firewall-cmd --list-services | grep ntpchronyc activity查看 chrony 日志
Section titled “查看 chrony 日志”journalctl -u chronyd --no-pager -n 50检查时间偏移量
Section titled “检查时间偏移量”chronyc tracking | grep "System time"偏移量(offset)应该在毫秒或微秒级别。如果偏移量过大(超过数秒),可以使用 chronyc makestep 强制同步。
- 保持 chrony 为默认时间服务:不要在 EL 8/9 上安装 ntpd
- 使用地理位置较近的 NTP 源:减少网络延迟,提高同步精度
- 配置多个 NTP 源:至少配置 3-4 个源以确保冗余
- 使用 iburst 选项:加快初始同步速度
- 保留 makestep 配置:允许启动时进行大幅时间调整
- 启用 rtcsync:保持硬件时钟同步
- 监控时间偏移:将
chronyc tracking纳入监控系统