问题描述
在 Windows 和 Ubuntu 双系统环境下,每次切换系统后时间都会错乱 8 小时。这是因为两个系统对硬件时钟(RTC)的理解不同:
- Windows :默认把 RTC 当作本地时间
- Ubuntu :默认把 RTC 当作 UTC 时间
这就导致每次切换系统,时间都会相差你所在时区的偏移量(中国是 +8 小时)。
网上很多教程只让你改 timedatectl set-local-rtc 1,但 Ubuntu 24.04 之后 systemd-timesyncd 在国内经常因为 ntp.ubuntu.com 被异常解析而失效,导致重启后时间又乱了。
本文将提供一劳永逸的解决方案,包括手动修正、硬件时钟回写、以及使用国内 NTP 服务器自动定时同步。
环境说明
| 系统 | 版本 |
|---|---|
| Ubuntu | 24.04 / 26.04 |
| Windows | 10 / 11 |
完整解决步骤
第一步:让 Ubuntu 把 RTC 当本地时间
这一步让 Ubuntu 与 Windows 的认知对齐------都把硬件时钟当本地时间。
bash
sudo timedatectl set-local-rtc 1 --adjust-system-clock
执行后可以用下面命令验证:
bash
timedatectl status
看到 RTC in local TZ: yes 就说明成功了。
第二步:禁用失效的 systemd-timesyncd
Ubuntu 默认的 systemd-timesyncd 使用 ntp.ubuntu.com,在国内经常被 DNS 污染或解析异常,导致时间同步失效。我们直接禁用它,换上更可靠的方案。
bash
sudo systemctl disable --now systemd-timesyncd
第三步:安装必要工具
Ubuntu 24.04 默认不带 ntpdate 和 hwclock,需要手动安装:
bash
sudo apt update
sudo apt install -y ntpdate util-linux-extra
验证安装:
bash
which ntpdate # 应输出 /usr/sbin/ntpdate
which hwclock # 应输出 /usr/sbin/hwclock
第四步:立即用国内 NTP 服务器精校一次时间
bash
sudo ntpdate -u ntp.aliyun.com
如果输出类似 adjust time server 203.107.6.88 offset ...,说明同步成功。
第五步:把校正后的系统时钟回写到硬件时钟
bash
sudo hwclock --systohc --localtime
这一步非常关键------确保硬件时钟被更新为正确时间,下次冷启动 Windows 也不会偏差。
第六步:创建自动定时同步服务
手动同步只能管一次,我们还需要让它每隔一段时间自动同步。
6.1 创建 systemd 服务文件
bash
sudo tee /etc/systemd/system/ntpdate-sync.service << 'EOF'
[Unit]
Description=Sync system time via ntpdate (CN NTP servers)
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/ntpdate -u ntp.aliyun.com ntp.tencent.com cn.pool.ntp.org
ExecStartPost=/usr/sbin/hwclock --systohc --localtime
EOF
6.2 创建 systemd 定时器文件
bash
sudo tee /etc/systemd/system/ntpdate-sync.timer << 'EOF'
[Unit]
Description=Run ntpdate-sync every hour
[Timer]
OnBootSec=2min
OnUnitActiveSec=1h
Persistent=true
Unit=ntpdate-sync.service
[Install]
WantedBy=timers.target
EOF
说明:
OnBootSec=2min:开机 2 分钟后执行一次OnUnitActiveSec=1h:之后每隔 1 小时执行一次Persistent=true:如果错过了执行时间(如关机期间),开机后立即补执行
第七步:启用定时器并重载 systemd
bash
sudo systemctl daemon-reload
sudo systemctl enable --now ntpdate-sync.timer
第八步:验证
查看定时器状态
bash
systemctl list-timers ntpdate-sync.timer
预期输出类似:
NEXT LEFT LAST PASSED UNIT
Sun 2026-05-03 12:05:00 CST 59min left Sun 2026-05-03 11:05:00 CST 15s ago ntpdate-sync.timer
查看最近执行日志
bash
journalctl -u ntpdate-sync.service --no-pager -n 10
正常日志应包含:
Starting Sync system time via ntpdate (CN NTP servers)...
adjust time server xx:ip offset ...
Started Sync system time via ntpdate.
常见问题排查
1. 报错 status=203/EXEC
原因:systemd 找不到可执行文件。
解决:
bash
# 确认 ntpdate 已安装且路径正确
which ntpdate
# 如果无输出,重新安装
sudo apt install -y ntpdate
如果路径存在但仍报 203,可能是 systemd 安全限制导致,解除限制即可:
bash
sudo systemctl edit ntpdate-sync.service
输入以下内容后保存:
ini
[Service]
PrivateTmp=false
ProtectSystem=no
然后重载:
bash
sudo systemctl daemon-reload
sudo systemctl restart ntpdate-sync.service
2. ntpdate 同步失败
bash
sudo ntpdate -u ntp.aliyun.com
如果报 no server suitable for synchronization found,检查网络连接,或更换 NTP 服务器:
bash
sudo ntpdate -u cn.pool.ntp.org
sudo ntpdate -u ntp.tencent.com
3. 切回 Windows 后时间仍差 8 小时
通常是 RTC 没被正确回写,手动执行一次:
bash
sudo ntpdate -u ntp.aliyun.com
sudo hwclock --systohc --localtime
总结
| 步骤 | 命令 | 作用 |
|---|---|---|
| 对齐 RTC 认知 | timedatectl set-local-rtc 1 --adjust-system-clock |
让 Ubuntu 把硬件时钟当本地时间 |
| 禁用自带同步 | systemctl disable --now systemd-timesyncd |
停用境内失效的 timesyncd |
| 安装工具 | apt install ntpdate util-linux-extra |
安装 ntpdate 和 hwclock |
| 手动校时 | ntpdate -u ntp.aliyun.com |
立即同步系统时钟 |
| 回写 RTC | hwclock --systohc --localtime |
将系统时间写入硬件时钟 |
| 创建定时任务 | systemd service + timer | 每小时自动同步一次 |
完成以上配置后,无论是冷启动还是热切换,Windows 和 Ubuntu 的时间都将始终保持一致,彻底告别双系统时间错乱的烦恼。
一键收藏,下次装双系统直接用! 有问题欢迎评论区交流。