Centos服务器设置时间自动同步

1.安装ntpd,ntpdate服务

 yum -y install ntp ntpdate

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

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

即可成功运行

3.配置定时脚本

可以通过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

wq保存执行

4.查看定时脚本执行情况

看 /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)

5.ntp可选地址列表

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>