centos7自动校时
(2018-07-26 15:44:22)
标签:
centos7ntpntpdate校时时间校准 |
分类: linux |
1、轻量级,不启动服务,使用ntpdate和crontab做定时时间修正,比较暴力
#安装ntpdate
yum -y install ntpdate;
#时间修正,同aliyun的时间服务器
ntpdate ntp1.aliyun.com;
#修改crontab作业列表,重启系统或重启crond服务即可定时修正时间
/bin/crontab -l > /tmp/crontab.bak;
echo "* */1 * * * /sbin/ntpdate ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com ntp4.aliyun.com ntp5.aliyun.com ntp6.aliyun.com ntp7.aliyun.com;/sbin/hwclock -w;" >> /tmp/crontab.bak;
/bin/crontab /tmp/crontab.bak;
echo "* */1 * * * /sbin/ntpdate ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com ntp4.aliyun.com ntp5.aliyun.com ntp6.aliyun.com ntp7.aliyun.com;/sbin/hwclock -w;" >> /tmp/crontab.bak;
/bin/crontab /tmp/crontab.bak;
2、自动校时,需启动ntp服务,时间慢慢校正
#安装ntp
yum -y install ntp;
#修改/etc/ntp.conf配置文件
/usr/bin/sed -i.ori 's#restrict default.*noquery#restrict
default nomodify#;13i\restrict 0.centos.pool.ntp.org nomodify
notrap noquery\nrestrict 1.centos.pool.ntp.org nomodify notrap
noquery\nrestrict 2.centos.pool.ntp.org nomodify notrap
noquery\nrestrict 3.centos.pool.ntp.org nomodify notrap
noquery\nrestrict 4.ntp1.aliyun.com nomodify notrap noquery'
/etc/ntp.conf;
/usr/bin/sed -i.ori '29a\server ntp1.aliyun.com\nserver time.nist.gov' /etc/ntp.conf;
/usr/bin/sed -i.ori '29a\server ntp1.aliyun.com\nserver time.nist.gov' /etc/ntp.conf;
#启动ntpd服务
systemctl start ntpd.service;
两种方法,可结合使用。
参考:
https://www.cnblogs.com/liuyou/archive/2012/07/29/2614330.html
https://www.cnblogs.com/fanjifen/p/6382919.html
https://ken.io/note/ntp-server-deploy-time-sync
https://www.cnblogs.com/anyux/p/7844260.html