openEuler 24.03 SP1 服务器时间同步操作文档(Chrony)
适用范围:
- openEuler 24.03 / 24.03 SP1
- 生产 / 测试服务器
- 云主机 / 物理机
- 数据库、应用、运维节点
一、目标
- 统一服务器时间
- 使用阿里云 NTP
- 启用 chrony 时间同步
- 确保时间同步健康、稳定、可巡检
二、检查当前状态(必做)
timedatectl
正常标准:
System clock synchronized: yes
NTP service: active
如果是 no 或 n/a → 继续往下操作
三、安装并启用 chrony
1️⃣ 安装
dnf install -y chrony
2️⃣ 如果提示 chronyd 被屏蔽(masked)
systemctl unmask chronyd
systemctl daemon-reexec
3️⃣ 启动并设置开机自启
systemctl enable chronyd --now
4️⃣ 确认服务状态
systemctl status chronyd
必须看到:
Active: active (running)
四、配置阿里云 NTP(关键步骤)
编辑配置文件
vi /etc/chrony.conf
修改方法(标准写法)
1️⃣ 注释默认 pool
#pool pool.ntp.org iburst
2️⃣ 紧接着添加阿里云 NTP
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
正确示例(节选)
#pool pool.ntp.org iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
重启服务
systemctl restart chronyd
五、DNS 问题修复(如果域名解析失败)
判断 DNS 是否有问题
ping www.baidu.com
如果提示:
Name or service not known
说明 DNS 异常
临时修复 DNS
cat > /etc/resolv.conf << EOF
nameserver 114.114.114.114
nameserver 8.8.8.8
EOF
永久修复(NetworkManager)
nmcli device status
假设网卡是 ens160:
nmcli connection modify ens160 ipv4.dns "114.114.114.114 8.8.8.8"
nmcli connection modify ens160 ipv4.ignore-auto-dns yes
nmcli connection up ens160
六、强制立即同步时间(必做)
chronyc makestep
七、验证是否同步成功(验收标准)
1️⃣ 查看时间源
chronyc sources -v
必须看到:
^* ntp1.aliyun.com
* 代表当前正在使用的时间源
2️⃣ 查看跟踪状态
chronyc tracking
关键判断点:
Leap status : Normal
System time : < 0.01 seconds
3️⃣ 系统级确认
timedatectl
必须看到:
System clock synchronized: yes
NTP service: active
八、如何判断“时间同步是健康的”(记住这个)
| 检查项 | 正常标准 |
|---|---|
sources -v 有 * |
必须 |
tracking Leap Normal |
必须 |
timedatectl 显示 yes |
必须 |
| 时间偏差 < 10ms | 优秀 |
| Reach ≠ 0 | 正常 |
九、常见误区(必须知道)
1️⃣ ping 不通 ntp.aliyun.com 是正常的
阿里云 NTP 禁 ICMP,但允许 UDP 123
2️⃣ 不要手动 date -s 改时间
chrony 会自动修正
3️⃣ 不要同时运行 ntpd 和 chronyd
只能保留一个(推荐 chrony)
十、生产环境最佳实践(强烈建议)
- 所有服务器使用同一组 NTP
- 数据库、应用、日志服务器统一时间源
- 每月巡检一次:
chronyc tracking
十一、一键初始化命令(可直接用)
dnf install -y chrony && \
systemctl unmask chronyd && \
systemctl daemon-reexec && \
systemctl enable chronyd --now && \
sed -i 's/^pool /#pool /g' /etc/chrony.conf && \
sed -i '/^server ntp1.aliyun.com/d' /etc/chrony.conf && \
sed -i '/^server ntp2.aliyun.com/d' /etc/chrony.conf && \
sed -i '/#pool pool.ntp.org iburst/a server ntp1.aliyun.com iburst\nserver ntp2.aliyun.com iburst' /etc/chrony.conf && \
systemctl restart chronyd && \
chronyc makestep && \
timedatectl
十二、最终验收标准(一句话)
能看到
\*、Leap Normal、System clock synchronized: yes
就说明时间同步已经是生产级别 OK

浙公网安备 33010602011771号