温馨提示×

如何同步ubuntu时间戳

小樊
44
2026-07-27 14:17:54
栏目: 智能运维

在 Ubuntu 系统中同步时间戳(系统时间)通常有两种常见方式:使用 systemd-timesyncd(推荐)使用 ntp/chrony。下面按常见场景说明。


一、使用 systemd-timesyncd(Ubuntu 默认,推荐)

Ubuntu 16.04 及以后版本默认使用 systemd-timesyncd 进行时间同步。

1. 查看时间同步状态

timedatectl status

正常输出中应包含:

System clock synchronized: yes
NTP service: active

2. 启用并启动时间同步

sudo timedatectl set-ntp true

如果未启动:

sudo systemctl enable systemd-timesyncd
sudo systemctl start systemd-timesyncd

3. 手动同步一次(可选)

sudo systemctl restart systemd-timesyncd

4. 查看同步日志

journalctl -u systemd-timesyncd

二、使用 NTP(传统方式)

1. 安装 NTP

sudo apt update
sudo apt install ntp

2. 启动并设置开机自启

sudo systemctl enable ntp
sudo systemctl start ntp

3. 手动同步

sudo ntpdate -u pool.ntp.org

三、使用 Chrony(服务器/高精度场景推荐)

1. 安装 Chrony

sudo apt install chrony

2. 启动服务

sudo systemctl enable chrony
sudo systemctl start chrony

3. 查看同步状态

chronyc tracking
chronyc sources

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

1. 设置系统时间

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

2. 写入硬件时钟

sudo hwclock --systohc

五、常见问题排查

1. 虚拟机时间漂移

  • 启用 时间同步客户端
  • VMware / VirtualBox 安装增强工具
  • 使用 chrony 效果更好

2. 时区不正确

timedatectl list-timezones
sudo timedatectl set-timezone Asia/Shanghai

3. 时间总是不同步

timedatectl set-ntp false
sudo ntpdate pool.ntp.org
sudo timedatectl set-ntp true

六、快速检查清单 ✅

timedatectl status
date

如果你能说明:

  • Ubuntu 版本
  • 物理机 / 虚拟机 / 容器
  • 是否联网

我可以给你更精确的同步方案。

0