Centos服务器如何设置时间自动同步
更新时间:2026年10月30日 09:52:54 作者:驯龙高手_追风
还在为服务器时间不同步烦恼吗?本文手把手教你安装ntpd和ntpdate服务,解决同步失败的报错,并配置crontab定时任务每10分钟自动同步时间,让你的服务器始终精准,附ntp可选地址列表,超实用
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
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Linux下用dnsmasq做dns cache server的配置方法
最近国外的服务器本地DNS总是出故障,闪断一会儿都会影响业务。于是在机房里找了两台Server,安装上keepalived和dnsmasq实际本地的DNS缓存2014-08-08


最新评论