Linux 时间同步的流程
一、问题时间
RTC时间、系统时间(UTC)和本地时间的关系如下:
-
RTC时间(硬件时钟):
- 显示为UTC时间格式:02:50:35/02:51:28
- 由主板电池供电,独立于系统运行12
- 通常存储UTC时间(Linux默认配置)45
-
系统时间:
- Universal time(UTC时间):02:50:33/02:51:20
- Local time(本地时间):10:50:33/10:51:20(UTC+8时区转换结果)56
- 系统启动时从RTC读取UTC时间初始化,之后由内核维护17
-
时间偏差分析:
- 实际时间16:51与本地时间10:51相差6小时
- 可能原因:
- RTC被错误设置为本地时间而非UTC(但timedatectl显示\"RTC in local TZ: no\"排除了这种情况)68
- NTP同步未生效(显示\"NTP synchronized: no\")910
- 硬件时钟电池供电异常导致RTC时间漂移211
真实时间 (16:51 CST) ├─ 应映射为→ UTC时间 (08:51) │ ├─ RTC应存储为→ 08:51 (UTC) │ └─ 系统UTC时间应显示为→ 08:51 │ └─ 时区转换后→ 16:51 CST └─ 当前异常状态: RTC存储为→ 02:51 (UTC) 系统UTC时间→ 02:51 时区转换后→ 10:51 CST
二、systemd-timesyncd 轻量级方案
📌 适用场景:桌面环境/轻负载服务器,精度需求 ±100ms 以内13
# 1. 安装与验证
sudo apt install systemd-timesyncd # Debian/Ubuntu
sudo yum install systemd-timesyncd # CentOS/RHEL:ml-citation{ref=\"5,9\" data=\"citationList\"}
timedatectl status # 检查服务状态
# 2. 配置核心参数(/etc/systemd/timesyncd.conf)
[Time]
NTP=ntp.aliyun.com ntp.tencent.com # 首选NTP服务器
FallbackNTP=ntp.ubuntu.com # 备用服务器
RootDistanceMaxSec=5 # 最大允许时间偏差
PollIntervalMinSec=32 # 最小轮询间隔:ml-citation{ref=\"4,6\" data=\"citationList\"}
# 3. 生效配置
sudo systemctl daemon-reload # 重载配置
sudo systemctl restart systemd-timesyncd
sudo systemctl enable systemd-timesyncd # 设置开机自启:ml-citation{ref=\"7\" data=\"citationList\"}
# 4. 验证同步状态
timedatectl | grep \"synchronized\" # 显示 yes 即成功
journalctl -u systemd-timesyncd -f # 实时查看同步日志:ml-citation{ref=\"6,9\" data=\"citationList\"}
三、chronyd 高精度方案
📌 适用场景:虚拟机/集群环境/不稳定网络,精度需求 ±1ms110
# 1. 安装与启动
sudo yum install chrony # CentOS/RHEL
sudo apt install chrony # Debian/Ubuntu
sudo systemctl enable --now chronyd:ml-citation
# 2. 优化配置(/etc/chrony.conf)
server ntp.aliyun.com iburst # iburst 加速初始同步
server cn.pool.ntp.org iburst
driftfile /var/lib/chrony/drift # 时钟漂移记录
makestep 1.0 3 # 偏差>1秒时强制步进调整
allow 192.168.0.0/24 # 授权内网同步(服务端需启用):ml-citation{ref=\"8,10\" data=\"citationList\"}
# 3. 防火墙放行
sudo firewall-cmd --add-service=ntp --permanent
sudo firewall-cmd --reload:ml-citation
# 4. 管理指令
chronyc tracking # 查看时间偏差(关注 Last offset)
chronyc sources -v # 检查NTP源质量
chronyc makestep # 强制立即同步:ml-citation
四、相关操作实例
[root@localhost tmp]# systemctl status chronyd ntp● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2025-07-21 16:13:14 CST; 2 days ago Docs: man:chronyd(8) man:chrony.conf(5) Process: 669 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS) Process: 576 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 598 (chronyd) Tasks: 1 Memory: 1.1M CGroup: /system.slice/chronyd.service └─598 /usr/sbin/chronyd[root@localhost tmp]# systemctl restart chronyd[root@localhost tmp]# systemctl status chronyd ntp● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2025-07-24 11:01:22 CST; 5s ago Docs: man:chronyd(8) man:chrony.conf(5) Process: 5159 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS) Process: 5156 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 5158 (chronyd) Tasks: 1 Memory: 436.0K CGroup: /system.slice/chronyd.service └─5158 /usr/sbin/chronydJul 24 11:01:22 localhost.localdomain systemd[1]: Stopped NTP client/server.Jul 24 11:01:22 localhost.localdomain systemd[1]: Starting NTP client/server...Jul 24 11:01:22 localhost.localdomain chronyd[5158]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYN... +DEBUG)Jul 24 11:01:22 localhost.localdomain chronyd[5158]: Frequency -222219.096 +/- 109.213 ppm read from /var/lib/chrony/driftJul 24 11:01:22 localhost.localdomain systemd[1]: Started NTP client/server.Unit ntp.service could not be found.Hint: Some lines were ellipsized, use -l to show in full.[root@localhost tmp]# timedatectl status Local time: Thu 2025-07-24 17:01:42 CST Universal time: Thu 2025-07-24 09:01:42 UTC RTC time: Thu 2025-07-24 09:01:41 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yesNTP synchronized: yes RTC in local TZ: no DST active: n/a[root@localhost tmp]# chronyc trackingReference ID : D21C8204 (time.nju.edu.cn)Stratum : 2Ref time (UTC) : Thu Jul 24 09:01:26 2025System time : 3.836876392 seconds slow of NTP timeLast offset : +0.561115086 secondsRMS offset : 0.561115086 secondsFrequency : 222219.094 ppm slowResidual freq : +181675.438 ppmSkew : 109.213 ppmRoot delay : 0.041683048 secondsRoot dispersion : 8.131237984 secondsUpdate interval : 3.1 secondsLeap status : Normal
五、方案对比与选型指南
特性
systemd-timesyncd
chronyd
精度
±100ms
±1ms
资源占用
极低(轻量级守护进程)
中等(需常驻内存)1
配置文件
/etc/systemd/timesyncd.conf
/etc/chrony.conf
适用场景
日常办公终端
K8s集群/数据库服务器12
网络适应性
依赖稳定网络
支持断网续同步110
关键命令
timedatectl status
chronyc tracking
⚠️ 关键注意
- 二者禁止同时启用,会冲突导致同步失败3
- 若硬件时钟电池老化,需更换后重新配置5
- 企业级集群建议部署专用NTP服务器1415
此手册覆盖主流场景的完整配置流程,systemd-timesyncd 满足基础需求,chronyd 则为高精度/复杂环境首选方案。