ubuntu时钟同步问题

前提

昨天登录网站2FA的时候遇到了一个问题,说服务器与浏览器的时间相差一分钟无法认证,我服务器查了之后才发现时间不对今天是19号了,电脑显示才12号

linux查看时间命令 date

我有查看了下时区,这是修改之后的了,修改前是使用的时区0000ntp.ubuntu.comntp使用的是官方的服务器无法通信,导致时间无法同步

排查

查看时区配置

1
2
3
4
5
6
7
8
9
aboyzy@exsiubuntu:~$ timedatectl
Local time: Fri 2025-12-19 11:17:57 CST
Universal time: Fri 2025-12-19 03:17:57 UTC
RTC time: Fri 2025-12-19 03:17:56
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

查看服务进程

1
2
3
4
5
6
7
8
9
10
11
12
13
systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/usr/lib/systemd/system/systemd-timesyncd.service; enabled>
Active: active (running) since Fri 2025-12-19 06:00:31 CST; 5h 18min ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 13692 (systemd-timesyn)
Status: "Contacted time server 106.55.184.199:123 (ntp.tencent.com)."
Tasks: 2 (limit: 9436)
Memory: 1.4M (peak: 1.9M)
CPU: 225ms
CGroup: /system.slice/systemd-timesyncd.service
└─13692 /usr/lib/systemd/systemd-timesyncd

修改办法

修改时区

1
2
3
4
5
6
7
8
# 查看所有时区
timedatectl list-timezones

# 设置为亚洲/上海(北京时间)
sudo timedatectl set-timezone Asia/Shanghai

# 启用网络时间同步 没开ntp的记得开ntp
sudo timedatectl set-ntp true

配置完时区发现,tmd官方ntp服务器的速度太慢了,换个阿里云和腾讯的

编辑配置文件:

1
sudo vim /etc/systemd/timesyncd.conf

取消[Time]下的注释并配置

修改内容示例:

1
2
3
4
5
6
[Time]
NTP=ntp.aliyun.com time1.aliyun.com ntp.tencent.com
FallbackNTP=ntp.ubuntu.com
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048

修改后重启

1
2
sudo systemctl restart systemd-timesyncd
sudo systemctl daemon-reload

查看时间同步详细信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
aboyzy@exsiubuntu:~$  timedatectl timesync-status
Server: 106.55.184.199 (ntp.tencent.com)
Poll interval: 34min 8s (min: 32s; max 34min 8s)
Leap: normal
Version: 4
Stratum: 2
Reference: 647A24C4
Precision: 1us (-23)
Root distance: 27.976ms (max: 5s)
Offset: -599us
Delay: 34.400ms
Jitter: 2.854ms
Packet count: 29
Frequency: +40.799ppm

timesyncd.conf参数说明

AI说的

参数 示例值 单位 默认值 作用 说明 推荐配置场景
NTP ntp.aliyun.com time1.aliyun.com - 主要 NTP 服务器列表 1. 指定优先使用的 NTP 服务器 2. 可设置多个,用空格分隔 3. 按顺序尝试连接 4. 支持域名和 IP 地址 生产环境ntp.aliyun.com ntp.tencent.com 国际环境pool.ntp.org time.google.com
FallbackNTP ntp.ubuntu.com - 备用 NTP 服务器 1. 所有主服务器失败时使用 2. 确保最低可用性 3. 通常设置为稳定的公共服务器 通用配置ntp.ubuntu.com 国内用户cn.pool.ntp.org 备用方案time.cloudflare.com
RootDistanceMaxSec 5 5 最大根距离限制 1. 衡量 NTP 服务器准确性 2. 根距离 = 层级距离 + 网络延迟 3. 拒绝超过此值的服务器 4. 越小要求越严格 高精度2 (金融交易) 标准5 (服务器) 宽松10 (桌面/移动)
PollIntervalMinSec 32 32 最小轮询间隔 1. 最短同步时间间隔 2. 必须是 2 的幂 3. 时钟不稳定时使用此频率 4. 防止时钟漂移过大 频繁同步16 (时间敏感应用) 标准32 (大多数服务器) 省电64 (笔记本/移动)
PollIntervalMaxSec 2048 2048 最大轮询间隔 1. 最长同步时间间隔 2. 必须是 2 的幂 3. 时钟稳定时使用此频率 4. 减少网络流量和负载 频繁监控1024 (17分钟) 标准2048 (34分钟) 低负载4096 (68分钟)

常用 NTP 服务器参考表格

服务器 运营商/组织 地区 特点
ntp.aliyun.com 阿里云 中国 国内访问快,稳定性好
ntp.tencent.com 腾讯云 中国 国内节点多,可靠性高
cn.pool.ntp.org NTP Pool 中国 国内公共池,负载均衡
ntp.sjtu.edu.cn 上海交大 中国 教育网,免费公共服务器
time.cloudflare.com Cloudflare 全球 Anycast,延迟低,免费
time.google.com Google 全球 谷歌公共 NTP,稳定
pool.ntp.org NTP Pool 全球 全球公共池,最常用
time.windows.com Microsoft 全球 Windows 默认,兼容性好
ntp.ubuntu.com Ubuntu 全球 Ubuntu 官方,稳定可靠

后续

不知道服务器抽什么风,NTP时间同步不上,改用chrony

首先停用原来的服务,需要root,没有记得加sudo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 systemctl stop systemd-timesyncd
systemctl disable systemd-timesyncd
systemctl status systemd-timesyncd

# 下载 chrony

sudo apt update
sudo apt install chrony
# 启动服务
sudo systemctl enable chrony
sudo systemctl start chrony
sudo systemctl status chrony
# 强制同步(一般不需要)
sudo chronyc makestep

更改配置文件,添加国内更新源

sudo vim /etc/chrony/chrony.conf

我的配置,其实就是只添加了国内的NTP服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usable directives.

# Include configuration files found in /etc/chrony/conf.d.
confdir /etc/chrony/conf.d

# This will use (up to):
# - 4 sources from ntp.ubuntu.com which some are ipv6 enabled
# - 2 sources from 2.ubuntu.pool.ntp.org which is ipv6 enabled as well
# - 1 source from [01].ubuntu.pool.ntp.org each (ipv4 only atm)
# This means by default, up to 6 dual-stack and up to 2 additional IPv4-only
# sources will be used.
# At the same time it retains some protection against one of the entries being
# down (compare to just using one of the lines). See (LP: #1754358) for the
# discussion.
server ntp.aliyun.com iburst

server time.cloud.tencent.com iburst




# About using servers from the NTP Pool Project in general see (LP: #104525).
# Approved by Ubuntu Technical Board on 2011-02-08.
# See http://www.pool.ntp.org/join.html for more information.
# 默认为这个
#pool ntp.ubuntu.com iburst maxsources 4
#pool 0.ubuntu.pool.ntp.org iburst maxsources 1
#pool 1.ubuntu.pool.ntp.org iburst maxsources 1
#pool 2.ubuntu.pool.ntp.org iburst maxsources 2

# Use time sources from DHCP.
sourcedir /run/chrony-dhcp

# Use NTP sources found in /etc/chrony/sources.d.
sourcedir /etc/chrony/sources.d

# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys

# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift

# Save NTS keys and cookies.
ntsdumpdir /var/lib/chrony

# Uncomment the following line to turn logging on.
#log tracking measurements statistics

# Log files location.
logdir /var/log/chrony

# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0

# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can't be used along with the 'rtcfile' directive.
# 这个是用于同步硬件时钟的
rtcsync

# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3

# Get TAI-UTC offset and leap seconds from the system tz database.
# This directive must be commented out when using time sources serving
# leap-smeared time.
leapsectz right/UTC

# 如果你想允许内网 NTP 查询
#allow 10.0.0.0/8
#allow 192.168.0.0/16

常用其他参数

chronyc tracking 查看系统时间偏移

chronyc sources -v 查看时间源

chronyc sourcestats 查看源统计

chronyc makestep 强制立即同步

chronyc activity 查看有多少源可用