温馨提示×

ubuntu时间戳如何校对

小樊
87
2026-09-13 05:37:14
栏目: 智能运维

在 Ubuntu 系统中,“时间戳校对”通常包含两个方面:

  1. 系统时间(含时区)是否正确(人看的时间)
  2. 时间戳(如 date +%s 输出的 Unix 时间戳)是否准确

下面按常见场景说明。


一、查看当前时间和时间戳

date

查看系统时间。

date +%s

查看当前 Unix 时间戳(自 1970-01-01 UTC 起的秒数)。

timedatectl

查看时间、时区、NTP 状态等。


二、使用 NTP 自动校对时间(推荐)

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

Ubuntu 通常自带 systemd-timesyncd。

查看状态:

timedatectl status

启用并启动:

sudo timedatectl set-ntp true

检查同步状态:

systemctl status systemd-timesyncd

2. 使用 chrony(服务器推荐)

安装:

sudo apt update
sudo apt install chrony

启动:

sudo systemctl enable --now chrony

查看同步情况:

chronyc tracking

3. 使用 ntpdate(临时手动校对)

sudo apt install ntpdate
sudo ntpdate pool.ntp.org

⚠️ 注意:ntpdate 已逐渐不推荐使用,建议用 NTP 服务长期同步。


三、手动设置时间(不推荐长期使用)

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

设置时区:

sudo timedatectl set-timezone Asia/Shanghai

四、确认时间戳是否正确

date -u +%s

UTC 时间戳。

如果你怀疑“时间戳不对”,常见原因:

  • 时区设置错误
  • 虚拟机暂停后时间不同步
  • BIOS/硬件时钟错误

五、虚拟机特别注意

如果是 VMware / KVM / VirtualBox:

sudo timedatectl set-ntp true

并确保宿主机时间正确。


如果你能说明:

  • 是 系统时间不对 还是 程序里的时间戳不对
  • 是否是 容器 / 虚拟机
  • Ubuntu 版本(lsb_release -a)

我可以给你更精确的校对方案。

0 踩