侧边栏壁纸
博主头像
休闲猫

这个博主懒得的跟猪一样,别指望更新!!!

  • 累计撰写 14 篇文章
  • 累计创建 6 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

centos 时间同步

休闲猫
2023-06-07 / 0 评论 / 0 点赞 / 1749 阅读 / 0 字
温馨提示:
本文最后更新于2025-07-09,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

安装ntpd,ntpdate服务

 yum -y install ntp ntpdate

检测时间同步命令是否执行

ntpdate cn.pool.ntp.org

时间同步成功运行如下所示:

 3 Mar 08:55:43 ntpdate[21343]: adjust time server 119.28.206.193 offset -0.000707 sec

若出现以下输出 则代表同步失败

3 Mar 08:35:20 ntpdate[19405]: the NTP socket is in use, exiting

且有时候还会出现如下错误:

提示:no server suitable for synchronization found

解决方案如下:

即可成功运行
#先停止ntpd服务
service ntpd stop
#再执行上述命令
ntpdate -u cn.pool.ntp.org
#将硬件时钟设置为当前的系统时间
/sbin/hwclock -w

配置定时脚本

可以通过crontab命令设置每隔10分钟定时运行。
同步脚本time_sync.sh撰写如下:
#! /bin/bash
source /etc/profile
source ~/.bash_profile
#网络时间同步
service ntpd stop
sleep 5
ntpdate -u cn.pool.ntp.org
/sbin/hwclock -w

使用crontab设置10分钟同步一次

配置crontab:
crontab -e

新增一行定时任务

#每10分钟同步一次时间
*/10 * * * * sh /root/Desktop/crontbl_sh/time_sync.sh

查看定时脚本执行情况

看 /var/log/cron.log这个文件就可以,可以用 tail -f /var/log/cron 观察
tail -f /var/log/cron

可以看到如下即代表执行成功,这样你的服务器就可以一直保持最精确的时间啦

Mar  3 09:00:01 cpu01 CROND[32370]: (root) CMD (sh /root/Desktop/crontbl_sh/time_sync.sh)

ntp可选地址列表

ntpdate -u cn.pool.ntp.org
ntpdate -u ntp1.aliyun.com
0

评论区