对于大多数现代 Linux 发行版,安装并启用 chronyd 服务,配置可靠的 NTP 服务器地址后重启服务,即可实现时间同步。
先说结论:chrony 是当前 Linux 主流的时间同步方案,配置重点在于指定上游服务器并确保网络可达。
- 适合:CentOS 8/RHEL 8 及 Ubuntu 20.04 等较新系统默认或推荐组件
- 先准备:确认服务器能访问公网 UDP 123 端口,或内网有可靠时间源
- 验收:通过 chronyc tracking 查看偏移量,确认状态为同步
命令速用版
# 安装 chrony(根据发行版选择)
yum install chrony -y
# 或
apt install chrony -y
# 启动并设置开机自启
systemctl enable `--now` chronyd
# 立即同步一次(可选)
chronyc -a makestep
# 查看同步状态
chronyc tracking为什么会这样
服务器硬件时钟存在自然漂移,长期运行会导致时间偏差。chrony 相比传统的 ntpd,在网络不稳定或虚拟机环境中校正速度更快。它通过守护进程 chronyd 后台运行,定期与配置的时间服务器交互,逐步调整系统时钟。
分步处理
1. 安装服务
如果系统未预装,使用包管理器安装。公共资料中没有看到可靠的量化数据表明安装耗时,通常秒级完成。
2. 配置文件
编辑/etc/chrony.conf,找到 server 或 pool 行。建议保留默认池配置,或添加可信的内网时间服务器。
pool 2.centos.pool.ntp.org iburst
# 或者指定具体服务器
server ntp.aliyun.com iburstiburst 参数能加速初始同步,建议保留。
3. 重启服务
修改配置后需重启服务生效。
systemctl restart chronyd怎么验证是否生效
使用 chronyc 工具查看状态。
chronyc sources -v
chronyc trackingsources 命令中,某行前方标记 * 表示当前正在使用的同步源,+ 表示候选源。tracking 命令中 Leap status 应为 Normal,System time 偏移量应较小。
常见坑
- 防火墙拦截:确保出站 UDP 123 端口未被安全组或防火墙阻止。
- 服务冲突:不要同时运行 ntpd 和 chronyd,两者会争夺时间控制权。
- 虚拟机环境:部分虚拟化平台建议开启宿主机时间同步配合,避免底层漂移过大。
- 硬件时钟:重启后若时间仍不对,可能需要将系统时间写入硬件时钟(hwclock -w)。
参考来源
- Chrony Project, "Chrony Documentation", https://chrony-project.org/doc/
- Red Hat, "Configuring NTP Using the chrony Suite", https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_system_time/using-chrony-to-configure-system-time_configuring-system-time