Ubuntu时间同步

一、问题背景与诊断

在某些云服务器环境(如本文提到的宿迁服务器)中,运营商可能会屏蔽标准的 NTP 端口(如 UDP 123),导致系统默认的时间同步服务失效。本文将介绍两种解决方案。

1.1 检查当前时间同步状态

首先,使用以下命令查看系统时间与同步状态:

1
timedatectl status

关键输出解读

  • **Local time**:本地时间(时区转换后)。
  • **Universal time**:协调世界时(UTC)。
  • System clock synchronized核心指标。显示 yes 表示已同步,no 表示未同步。
  • **NTP service**:显示当前活跃的时间同步服务(activen/a)。

如果 System clock synchronized 显示为 no,则需要按照本指南进行配置。

1.2 设置正确的时区(可选但推荐)

确保系统时区设置为中国标准时间:

1
sudo timedatectl set-timezone Asia/Shanghai

二、方法一:使用系统自带的 systemd-timesyncd(推荐)

Ubuntu 16.04 及更高版本默认使用 systemd-timesyncd 服务进行时间同步。它轻量、集成度高,是首选方案。

2.1 配置自定义 NTP 服务器

编辑配置文件:

1
sudo nano /etc/systemd/timesyncd.conf

找到 [Time] 部分,取消 NTP= 行的注释,并填入自定义的 NTP 服务器地址。可以指定多个,用空格分隔。

1
2
3
[Time]
NTP=ntp.aliyun.com ntp.tencent.com
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org

常用国内 NTP 服务器

  • 阿里云:ntp.aliyun.com
  • 腾讯云:ntp.tencent.com
  • 国家授时中心:cn.pool.ntp.orgntp.ntsc.ac.cn

2.2 重启服务并启用

1
2
3
4
5
6
# 重启 timesyncd 服务
sudo systemctl restart systemd-timesyncd
# 设置开机自启
sudo systemctl enable systemd-timesyncd
# 查看服务状态
sudo systemctl status systemd-timesyncd

2.3 验证同步结果

等待几分钟后,再次运行 timedatectl status。当 System clock synchronized 变为 yes 时,表示配置成功。

重要提示:同步不是即时的,需要等待服务完成一轮时间同步(通常几分钟内)。


三、方法二:安装并配置 ntp 服务

如果 systemd-timesyncd 因故无法工作(例如,安装了其他时间服务导致冲突),可以安装传统的 ntp 包。

3.1 安装 NTP 软件包

1
2
sudo apt update
sudo apt install ntp

安装过程会自动停止并禁用 systemd-timesyncd 服务。

3.2 配置 NTP 服务器

编辑 NTP 主配置文件:

1
sudo nano /etc/ntp.conf

找到 serverpool 配置行。注释掉默认的 pool *.debian.pool.ntp.org 行,添加自定义的服务器。

1
2
3
4
5
6
7
8
# 默认池(可注释掉)
# pool 0.debian.pool.ntp.org iburst
# pool 1.debian.pool.ntp.org iburst

# 添加自定义服务器(例如阿里云和腾讯云)
server ntp.aliyun.com iburst
server ntp.tencent.com iburst
server cn.pool.ntp.org iburst
  • iburst 选项可以在服务启动时快速进行初始同步。

3.3 重启 NTP 服务

1
2
3
sudo systemctl restart ntp
sudo systemctl enable ntp
sudo systemctl status ntp

3.4 检查 NTP 同步状态

使用 ntpq 工具查看与上游 NTP 服务器的连接状态:

1
ntpq -p

输出关键列解读

  • **remote**:NTP 服务器地址。
  • **refid**:服务器本身参考的上一级时间源。
  • **st**:层级(stratum),数值越小越接近权威时间源。
  • **t**:类型(u=单播,p=池)。
  • **when**:距上次查询的秒数。
  • **poll**:查询间隔(秒)。
  • **reach**:八进制表示的连接成功历史(值 377 表示最近8次查询全部成功)。
  • **delay**:网络延迟(毫秒)。
  • offset:时间偏移量(毫秒),关键指标,绝对值越小越好。
  • **jitter**:时间抖动(毫秒)。

理想状态:至少有一个服务器前有 *(当前主同步源)或 +(良好的备用源),且 reach 值不为 0

3.5 手动强制同步(可选)

如果自动同步不理想,可以手动触发:

1
sudo ntpdate -s ntp.aliyun.com

注意ntpdate 可能与正在运行的 ntpd 服务冲突。更安全的方式是重启 ntp 服务或使用 ntpd -gq(需先停止服务)。


四、故障排除与高级管理

4.1 查看详细的系统日志

1
2
3
4
# 查看 systemd-timesyncd 日志
sudo journalctl -u systemd-timesyncd
# 查看 ntp 服务日志
sudo journalctl -u ntp

4.2 处理服务冲突

如果同时安装了多个时间服务(如 chronyntpsystemd-timesyncd),它们会相互冲突。确保只启用一个:

1
2
3
4
# 禁用 systemd-timesyncd(如果使用 ntp)
sudo systemctl disable --now systemd-timesyncd
# 禁用 ntp(如果使用 systemd-timesyncd)
sudo systemctl disable --now ntp

4.3 检查硬件时钟(RTC)设置

确保硬件时钟设置为 UTC,避免双系统时间混乱:

1
2
3
4
# 查看当前设置
timedatectl | grep “RTC in local TZ”
# 如果显示 “yes”,则设置为 UTC
sudo timedatectl set-local-rtc 0

4.4 防火墙配置

如果使用自定义防火墙(如 ufw),确保放行 NTP 流量(UDP 123端口):

1
sudo ufw allow out 123/udp

五、总结与建议

特性 systemd-timesyncd (方法一) ntp (方法二)
复杂度 简单,集成于 systemd 稍复杂,功能更全
配置 编辑 /etc/systemd/timesyncd.conf 编辑 /etc/ntp.conf
控制 systemctl systemctlntpq
适用场景 大多数桌面和服务器,简单同步 需要更精确时间、复杂网络或作为 NTP 服务器

操作流程建议

  1. 首选方法一:配置 systemd-timesyncd。它更现代、更简单。
  2. 如果方法一失败:再考虑安装 ntp。安装前可尝试卸载冲突软件:sudo apt remove ntp chrony
  3. 验证:无论哪种方法,最终都以 timedatectl status 显示 System clock synchronized: yes 为准。
  4. 耐心等待:时间同步服务需要周期运行,配置后请等待数分钟再验证。