方法一:使用 timedatectl

查看同步状态

1
timedatectl status

启用 NTP 同步

1
sudo timedatectl set-ntp true

这会启用 systemd-timesyncd 服务,自动与外部 NTP 服务器同步时间。

方法二:安装 NTP 服务

适用于需要更精细控制的场景。

1. 安装 NTP

1
2
sudo apt-get update
sudo apt-get install ntp

2. 配置 NTP 服务器

编辑配置文件:

1
sudo nano /etc/ntp.conf

添加或修改 NTP 服务器:

1
2
3
4
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool.ntp.org

3. 重启服务

1
sudo systemctl restart ntp

4. 查看同步状态

1
ntpq -p

两种方法对比

方法 优点 缺点
timedatectl 系统自带,简单快速 功能相对简单
NTP 服务 功能强大,可精细配置 需要额外安装

注意事项

  • 确保防火墙允许 UDP 123 端口(NTP 端口)
  • 受限网络环境需配置特定 NTP 服务器
  • 两种方法不要同时使用,选择其一即可