Debian 开机自动同步时间
简介
之前写过 如何同步 Debian 服务器的时间,其中介绍了如何同步服务器的时间。
但是有客户反映,这样做以后,服务器时间是同步了。但是服务器重启后时间还是错的。
那么这篇文档将一劳永逸的解决这些问题。
同步时间服务器
首先我们需要确认好时间服务器是可以同步的。
下载需要的 ntpdate 软件:
apt install ntpdate
进行时间同步:
ntpdate ntp.aliyun.com
开机同步时间
硬件时钟的同步也只是保证系统的正常关机重启,遇到非正常关机,就需要我们在系统启动时同步时间。
在这里我们需要创建启动脚本:
1.创建 /etc/init.d/ntpdate
文件
#! /bin/sh # # ntpdate Execute the ntpdate command. PATH=/sbin:/bin:/usr/sbin:/usr/bin HOST=ntp.aliyun.com # Set operating system time from other host in LAN. if [ -x /usr/sbin/rdate ] then /usr/sbin/ntpdate $HOST fi
将上述脚本中的 HOST 改成时间同步服务器即可。
2.修改文件执行权限
chmod a+rx /etc/init.d/ntpdate
3.在 /etc/rc2.d
创建映射文件
cd /etc/rc2.d ln -s ../init.d/ntpdate S19ntpdate
4.重启后,系统时间将会同步过来。