温馨提示×

如何校准ubuntu时间戳

小樊
81
2026-10-24 19:43:15
栏目: 智能运维

在 Ubuntu 中,“时间戳”通常和**系统时间(日期时间)**以及 硬件时钟(RTC) 有关。下面按常见场景教你如何校准。


一、使用 NTP 自动校准(推荐)

1. 使用 systemd-timesyncd(Ubuntu 默认)

# 查看时间同步状态
timedatectl status

# 开启自动时间同步
sudo timedatectl set-ntp true

如果 System clock synchronized: yes,说明已校准。


2. 使用 chrony(更精确,适合服务器)

sudo apt update
sudo apt install chrony
sudo systemctl enable --now chrony

查看同步状态:

chronyc tracking

二、手动校准时间(无网络时)

1. 手动设置系统时间

sudo date -s "2026-01-21 15:30:00"

2. 将系统时间写入硬件时钟

sudo hwclock --systohc

三、校准硬件时钟(RTC)

查看硬件时间

sudo hwclock --show

从网络时间同步后写回硬件

sudo hwclock --systohc

从硬件时间恢复系统时间

sudo hwclock --hctosys

四、时区设置(很重要)

如果时间戳“不对”,很可能是时区问题:

# 查看当前时区
timedatectl

# 设置时区(示例:中国)
sudo timedatectl set-timezone Asia/Shanghai

五、验证时间戳

date +%s        # Unix 时间戳(秒)
date            # 当前系统时间

六、常见问题排查

NTP 不生效

systemctl status systemd-timesyncd
journalctl -u systemd-timesyncd

虚拟机时间漂移

  • 开启宿主机时间同步
  • 或安装 open-vm-tools / hyperv-daemons

如果你是指:

  • 程序里的时间戳不对
  • Docker / 容器时间不对
  • 日志时间戳错乱

可以告诉我具体场景,我可以给你更精确的方案。

0 踩