linux服务器时间同步失败解决方案
本文核心围绕 linux 服务器时间同步失败的问题展开讨论,以 ubuntu 和 centos 系统为例,给予几种测试和解决方法
常见错误
timed out waiting for reply from ... (...)
chronyd: no suitable source for synchronisation
ntp 被屏蔽
如果服务器服务器出现了时间同步失败的问题,首先应考虑是否是服务器商屏蔽了 ntp 信息包,导致系统无法同步时间。一般情况下,可发送工单开展询问以做确认。
客服可能会给予一个可用的 ntp 服务器地址,接下来更改同步时间的软件设定,改为使用该地址同步就可以。
ubuntu
ubuntu 18.04 默认使用 timesyncd 来同步时间。
编辑配置文件:
sudo vim /etc/systemd/timesyncd.conf
替换 ntp 服务器,例如为 ntp.ubuntu.com:
/etc/systemd/timesyncd.conf
[time]
ntp=ntp.ubuntu.com
保存文件后执行:
sudo systemctl daemon-reload
sudo timedatectl set-ntp off
sudo timedatectl set-ntp on
sudo systemctl status systemd-timesyncd
设定时区为 utc 的方法:
sudo dpkg-reconfigure tzdata
centos
centos 8 默认使用 chronyd 来同步时间。
编辑配置文件:
vim /etc/chrony.conf
更换 ntp 服务器,例如为 2.centos.pool.ntp.org:
/etc/chrony.conf
pool 2.centos.pool.ntp.org iburst
也可手动更新:
设定时区为 utc 的方法:
sudo timedatectl set-timezone utc
其他方法
除更换为可用 ntp 服务器外,还有其他方式,例如使用 htpdate 等,可自行搜索使用方法。
以下介绍此外一种简便方法,适用于时间精度要求不高的场景,示例中系统客户名为 sammy:
mkdir -p ~/scripts
vim ~/scripts/time.sh
在文件中添加如下内容:
#!/bin/bash
curl http://time.akamai.com/?iso | xargs date -s
hwclock -w
或者添加如下内容( 国内适用),也要安装 jq 软件包:
设定每5min执行一次:
sudo crontab -e
在 cron 文件顶部中添加:
mailto=""
*/5 * * * * /bin/bash /home/sammy/scripts/time.sh
查看执行日志:
journalctl _comm=crond
【文章原创作者:大丰网页设计 http://www.1234xp.com/dafeng.html 复制请保留原URL】阅读本文的人还阅读:
新加坡服务器双向cn2精品带宽,不限流量,延时低至50ms,快速稳定!