前提
昨天登录网站2FA的时候遇到了一个问题,说服务器与浏览器的时间相差一分钟无法认证,我服务器查了之后才发现时间不对今天是19号了,电脑显示才12号
linux查看时间命令 date
我有查看了下时区,这是修改之后的了,修改前是使用的时区0000和ntp.ubuntu.comntp使用的是官方的服务器无法通信,导致时间无法同步
排查
查看时区配置
1 | aboyzy@exsiubuntu:~$ timedatectl |
查看服务进程
1 | systemctl status systemd-timesyncd |
修改办法
修改时区
1 | # 查看所有时区 |
配置完时区发现,mtd官方ntp服务器的速度太慢了,换个阿里云和腾讯的
编辑配置文件:
1 | sudo vim /etc/systemd/timesyncd.conf |
取消[Time]下的注释并配置
修改内容示例:
1 | [Time] |
修改后重启
1 | sudo systemctl restart systemd-timesyncd |
查看时间同步详细信息
1 | aboyzy@exsiubuntu:~$ timedatectl timesync-status |
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 |
全球 | 谷歌公共 NTP,稳定 | |
pool.ntp.org |
NTP Pool | 全球 | 全球公共池,最常用 |
time.windows.com |
Microsoft | 全球 | Windows 默认,兼容性好 |
ntp.ubuntu.com |
Ubuntu | 全球 | Ubuntu 官方,稳定可靠 |