Ubuntu 22.04 安装配置 chrony 时间同步服务指南

在 Ubuntu 22.04 上安装配置 chrony 时间同步服务:apt 安装 chrony、禁用 systemd-timesyncd、编辑 /etc/chrony/chrony.conf 配置时间源、systemctl 启停、chronyc sources/tracking 状态检查,以及搭建内网 NTP 服务器减少外网依赖。chrony 4.2 实测

时间同步与服务NTPchrony时间同步2026/09/29

Ubuntu 22.04 安装配置 chrony 时间同步服务指南

目标:在 Ubuntu 22.04 上安装并配置 chrony(chronyd),同步系统时间到上游 NTP 服务,并支持搭建内网 NTP 服务器。Ubuntu 22.04 默认用 systemd-timesyncd 做基础同步;当你需要完整 NTP 服务端能力、内网时间源或多上游聚合时,用 chrony 替换。本文在 Ubuntu 22.04.5(chrony 4.2)实测。

环境信息

  • 系统:Ubuntu 22.04.5 LTS(x86_64)
  • 软件:chrony 4.2(chronyd (chrony) version 4.2)
  • 配置目录:/etc/chrony/,主配置 /etc/chrony/chrony.conf
  • 服务名:chrony(注意与 RHEL 系的 chronyd 不同)
  • 默认上游:pool ntp.ubuntu.com iburst 等 Ubuntu 官方池
  • 二进制:/usr/sbin/chronyd

一、安装 chrony

sudo apt update
sudo apt install -y chrony

💡 Ubuntu 默认安装的 systemd-timesyncd 只做基础客户端同步。chrony 提供完整服务端/客户端能力。安装后建议禁用并停掉 systemd-timesyncd,避免其占用的 123 端口之争:

sudo systemctl disable --now systemd-timesyncd

提示:chrony 与 ntp 包不可同时启用(都占用 UDP 123);本机若之前装过 ntp,需 systemctl stop ntp && systemctl disable ntp,再启用 chrony。


二、配置时间源

主配置文件 /etc/chrony/chrony.conf,默认自带 Ubuntu 官方池:

pool ntp.ubuntu.com        iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2

可按需追加内网/国内源:

pool ntp.aliyun.com iburst
server 192.168.1.10 iburst

常用指令:

指令作用
pool <域> iburst maxsources N上游时钟池,maxsources 限制从池使用的源数量
server <域/IP> iburst指定单一上游
allow <网段>允许给内网网段提供时间服务(内网 NTP Server)
local stratum 10上游不可达时用本机时钟兜底

三、启动并开机自启

sudo systemctl enable --now chrony
systemctl is-active chrony     # active
sudo systemctl restart chrony  # 修改配置后重启

四、检查同步状态(NTP 同步状态检查)

4.1 查看同步源

chronyc sources -v

回显中的源状态标记:

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
  • ^* = 当前选定最佳同步源;^+ = 候选;^? = 暂不可用(隔离网络/外网不可达时常见)

4.2 查看跟踪信息

chronyc tracking

输出含 Stratum(本机层级)、当前参考源(Reference ID)、System time 偏移等。接入上游一般为 2~3;无上游并配置 local stratum 10 时显示 10,此时本机充当内网时间源。

4.3 立即校准

chronyc makestep

正常回显 200 OK,用于需要立刻步进系统时间时。

4.4 查看时间同步总状态

timedatectl

systemd-timesyncd.service 显示 active 时才走它;chrony 接管后由 chrony 提供 NTP 客户端功能(timedatectl 的 NTP service: active 仍会显示)。


五、搭建内网 NTP 服务器

假设内网 NTP 服务器 IP 为 192.168.1.10,网段 192.168.1.0/24。

5.1 服务端配置

在 /etc/chrony/chrony.conf 末尾追加:

allow 192.168.1.0/24
local stratum 10
  • allow:允许该网段客户端请求时间;
  • local stratum 10:外网不通时以本机为兜底时间源,保证内网可同步。

重启:

sudo systemctl restart chrony

5.2 放行防火墙

sudo ufw allow 123/udp
sudo ufw reload

验证监听:

ss -lunp | grep ':123'

5.3 客户端指向内网服务器

内网客户端(同样装 chrony)注释外网 pool、指向内网服务器:

# pool ntp.ubuntu.com iburst
server 192.168.1.10 iburst

重启并检查:

sudo systemctl restart chrony
chronyc sources -v      # 应出现 ^* 指向 192.168.1.10
chronyc tracking

这样内网节点统一从 192.168.1.10 取时,仅该服务器访问外网,减少外网依赖并保证内网时间一致。


六、常见问题

  • All sources have been dropped:上游不可达,检查 UDP 123 放行与外网可达性;内网场景改用 server <内网NTP> + local stratum 10。
  • chrony 与 ntp 端口冲突:二选一并停用另一个/另一个服务(本文分别见 chrony 与《Ubuntu 22.04 配置 ntpd 传统时间同步服务指南》)。
  • chronyd: command not found:二进制在 /usr/sbin/chronyd,root 会话若 PATH 不含可全路径调用,或确认已 apt install chrony。
  • systemd-timesyncd 与 chrony 并存:安装 chrony 后 systemctl disable --now systemd-timesyncd,避免时序紊乱。

七、卸载

sudo apt remove -y chrony
sudo systemctl enable --now systemd-timesyncd   # 需要时恢复默认
Ubuntu 22.04 安装配置 chrony 时间同步服务指南 - Quzuo