51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

VPS时间同步|添加定时任务|rdate|nptdate

目录

设置时区为北京时间 {#i}

一般国外的VPS的镜像都是默认的国外时区,使用起来不是很方便。可以把它修改成北京时间,就会方便很多。代码

rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

rdate时间同步 {#rdate}

部分国内机器屏蔽了udp协议,而ntp是需要走udp协议的,这时我们可以使用rdate。

centos系统安装方法: {#centos}

yum install rdate 

ubuntu debian系统安装方法 {#ubuntu_debian}

apt-get install rdate 

安装完后,执行

rdate -s time.nist.gov 

即可完成同步时间了。然后再把同步加入到定时任务中,每10分钟执行同步一次,通过

crontab -e 

命令进行编辑crontab计划任务,把

*/10 * * * * rdate -s time.nist.gov 

加入到任务中,:wq 保存即可。

不过,rdate 也有缺点,没有ntpdate 这么精准,所以我们需要加入到定时任务中,让其在一定的时间内再次去同步时间,以保证时间的精准度。

NTP同步时间 协议 {#NTP%E5%90%8C%E6%AD%A5%E6%97%B6%E9%97%B4_%E5%8D%8F%E8%AE%AE}

众所周知,NTP协议是网络时间同步协议,有了它,我们可以很轻松的同步本地时间与互联网时间。VPS上也可以使用NTP来同步网络。首先安装必要的软件包:

Ubuntu/Debian系统 {#UbuntuDebian%E7%B3%BB%E7%BB%9F}

  1. apt-get update
  2. apt-get install ntp ntpdate -y

CentOS/RHEL系统 {#CentOSRHEL%E7%B3%BB%E7%BB%9F}

  1. yum install ntp ntpdate -y
  1. 接下来我们需要先停止NTP服务器,再更新时间。
  1. service ntpd stop #停止ntp服务
  2. ntpdate us.pool.ntp.org #同步ntp时间
  3. service ntpd start #启动ntp服务
  1. 执行完成后,VPS上就是相对精确的时间设置了。很多依赖于系统时间的应用程序也就能正常工作了。

时间同步 {#i-2}

ntpdate cn.pool.ntp.org # 同步北京时间
hwclock -w # 写入硬件时钟,虽然不知道有没有用,写就完了
0 1 * * * root ntpdate cn.pool.ntp.org >> /dev/null 2>&1 # 添加这么一条定时同步任务

定时任务 {#i-3}

*/5 * * * * root ntpdate -u ntp.sjtu.edu.cn
*/5 * * * * root rdate -s time-b.nist.gov
*/5 * * * * root rdate -s time-a.nist.gov
*/5 * * * * root rdate -s time.nist.gov

重启定时任务:


/etc/init.d/cron restart

赞(0)
未经允许不得转载:工具盒子 » VPS时间同步|添加定时任务|rdate|nptdate