04 - 时间同步配置(systemd-timesyncd / chrony)
前提条件
- 已完成 Debian 12 Bookworm 最小化安装 + apt 源优化
- 系统能正常联网(可访问
pool.ntp.org或国内 NTP 服务器) - 以 root 身份登录,或 ops 用户执行
sudo -i切换至 root 环境 - 虚拟机环境:VMware Workstation Pro(建议关闭虚拟机时间同步,避免干扰)
VMware 时间同步冲突
VMware 默认通过 VMware Tools 强制同步宿主机时间给虚拟机,与系统内部 NTP 服务产生冲突,可能导致时间跳变、日志时序混乱、证书校验失败。生产环境中必须关闭 VMware 时间同步,让 Debian 自己管理时间。
详细步骤
1. 关闭 VMware 的时间同步
方式一(图形界面): - 虚拟机 → VM → Settings → Options → VMware Tools - 取消勾选 Synchronize guest time with host
方式二(编辑 .vmx 文件,关机状态下操作):
tools.syncTime = "FALSE"
time.synchronize.continue = "FALSE"
time.synchronize.restore = "FALSE"
time.synchronize.resume.disk = "FALSE"
time.synchronize.shrink = "FALSE"
2. 检查当前时间状态
timedatectl status
date
timedatectl list-timezones | grep Asia/Shanghai
3. 方案一:systemd-timesyncd(默认、轻量,推荐学习 / 中小型环境)
Debian 12 默认已安装,启用服务:
systemctl enable --now systemd-timesyncd
配置国内 NTP 服务器(使用 drop-in 文件,不修改原始配置):
systemctl edit systemd-timesyncd
输入以下内容:
[Time]
NTP=ntp.aliyun.com cn.pool.ntp.org time1.aliyun.com
FallbackNTP=pool.ntp.org
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048
保存后重启服务并验证:
systemctl restart systemd-timesyncd
timedatectl status
timedatectl timesync-status
journalctl -u systemd-timesyncd -n 30
检查点
timedatectl status 输出中:
NTP service: activeSystem clock synchronized: yesTime zone: Asia/Shanghai (CST, +0800)
4. 方案二:chrony(推荐生产环境、大型集群、高精度需求)
安装 chrony(安装后会自动禁用 systemd-timesyncd):
apt update
apt install -y chrony
备份并修改配置:
cp /etc/chrony/chrony.conf /etc/chrony/chrony.conf.bak
vim /etc/chrony/chrony.conf
推荐配置(替换 server 部分):
# 国内高精度 NTP 服务器
server ntp.aliyun.com iburst
server time1.aliyun.com iburst
server time2.aliyun.com iburst
server cn.pool.ntp.org iburst
# 允许本地子网客户端同步(视需求开启)
#allow 192.168.1.0/24
# 本地 stratum 10 作为备用
local stratum 10
重启并验证:
systemctl restart chrony
systemctl enable chrony
chronyc tracking
chronyc sources -v
检查点
chronyc tracking 输出中 System time offset 在毫秒级以内,Leap status: Normal。
chronyc sources -v 中至少一个源标记为 *(当前选中源)。
5. 设置时区
# 命令行一步设置
timedatectl set-timezone Asia/Shanghai
# 或交互式选择
dpkg-reconfigure tzdata
# 依次选择:Asia → Shanghai
6. 最终验证
date
timedatectl
# 如果使用 chrony
chronyc tracking
检查点
date 输出时间与实际时间误差在 1 秒以内,时区显示 CST(+0800)。
实践任务
- 关闭 VMware Tools 的时间同步功能
- 配置
systemd-timesyncd使用阿里云 NTP 服务器,验证同步成功 - (可选进阶)安装 chrony,配置至少 3 个国内服务器,对比
chronyc tracking输出 - 将系统时区更改为
Asia/Shanghai并确认date输出正确
自测问题
1. 为什么生产环境中要关闭 VMware 的 guest 时间同步?
VMware Tools 的时间同步机制会在检测到宿主机与虚拟机时间偏差超过阈值时强制跳变虚拟机时间,这与 NTP 的渐进式调整(slewing)冲突。强制跳变会导致:日志时序混乱(同一事件出现两次或跳过时间段)、SSL/TLS 证书验证失败(证书有效期检查依赖精确时间)、定时任务执行混乱(cron/systemd-timer 可能重复触发或跳过)。应由系统内置 NTP 客户端统一管理时间。
2. systemd-timesyncd 和 chrony 的主要适用场景区别是什么?
systemd-timesyncd 是轻量级 SNTP 客户端,只实现基本的时间同步,内存占用极小(约 1–2 MB),适合开发测试环境、单机服务器、对时间精度要求不高的场景。chrony 是完整的 NTP 实现,支持更高精度(亚毫秒级)、更好的网络抖动处理、可作为 NTP 服务器为内网其他机器提供时间服务,适合生产服务器、金融系统、需要高精度时间或需要本地 NTP 服务器的集群环境。
3. 如何判断 NTP 同步是否正常工作(至少说出两种命令)?
timedatectl status # 查看 NTP service 状态和 System clock synchronized
timedatectl timesync-status # 查看上次同步时间、服务器地址、精度
chronyc tracking # chrony 用户:查看偏移量和同步状态
journalctl -u systemd-timesyncd -n 20 # 查看同步日志
4. iburst 参数在 chrony/ntp 配置中的作用是什么?
iburst 指示 NTP 客户端在首次连接或重新连接时,立即发送一组 8 个快速查询包(间隔约 2 秒),而不是等待正常的 64 秒轮询间隔。这使系统在启动或网络恢复后能在数秒内快速完成初始时间同步,之后恢复正常轮询间隔。对于系统启动时间精度要求高的场景(如数据库主从同步、日志聚合平台)尤其重要。
总结
时间同步是系统日志、证书验证、集群一致性等功能的基础。Debian 12 默认的 systemd-timesyncd 足以满足大多数场景;生产级高可用环境推荐 chrony + 国内 NTP 池。关闭虚拟化软件的时间同步干预是避免时间跳变的第一步。