tnpdate ip (目标主机)
使用定时任务与nptdate结合实现自动时间同步
查看有没有任务:
[root@rac121 ~]# crontab -l
no crontab for root
添加时间同步任务,内容如下(绿色背景部分):
[root@rac121 ~]# crontab -eno crontab for root - using an empty one
0,10,21,31,41,51 * * * * /usr/sbin/ntpdate -v 10.1.1.222 > /root/ntpdate.log 2>&1
59 23 * * * /usr/sbin/ntpdate -v 10.1.1.222 > /root/ntpdate.log 2>&1;hwclock --systohc
再查看时该任务已经添加进来
[root@rac121 ~]# crontab -l
0,10,21,31,41,51 * * * * /usr/sbin/ntpdate -v 10.1.1.222 > /root/ntpdate.log 2>&1
59 23 * * * /usr/sbin/ntpdate -v 10.1.1.222 > /root/ntpdate.log 2>&1;hwclock --systohc
重启服务
[root@rac121 ~]# service crond restart
Stopping crond: [ OK ]
Starting crond: [ OK ]
对比验证时间是否同步
[root@rac121 ~]# date
Tue Jun 12 10:38:23 CST 2012
首先来看下正确的时区设置。
[root@vm3 ~]# date -R
Mon, 07 May 2012 16:20:14 +0800
[root@vm3 ~]# date
2012年 05月 07日 星期一 16:23:35 CST
[root@vm3 ~]# strings /etc/localtime
TZif2
p ~h
!Iap"^J
#)Cp$Gg
TZif2
!Iap
#)Cp
CST-8
可以看到我们使用的时区是 +0800,这个对应的就是中国的正确的时区。
接下来我要模拟下如何将时区设置为一个错误的值,时区的设置文件为 /etc/localtime,linux下保存着所有可用的时区设置参数文件,目录为/usr/share/zoneinfo/,需要设置时区的时候将对应的时区设置文件覆盖到/etc/localtime即可,知道原理后我们就可用设置到一个错误的时区了。本例中我选择了一个非洲的时区。
[root@vm3 ~]# ll /usr/share/zoneinfo/Africa/ | more
总计 424
-rw-r--r-- 1 root root 156 2011-05-06 Abidjan
设置当前时区为Abidjan:
[root@vm3 ~]# cp /usr/share/zoneinfo/Africa/Abidjan /etc/localtime
cp:是否覆盖“/etc/localtime”? y
这个覆盖是立即生效的:
[root@vm3 ~]# date -R
Mon, 07 May 2012 08:28:06 +0000
[root@vm3 ~]# date
2012年 05月 07日 星期一 08:28:08 GMT
发现这个案例的原因是开发人员报告数据库的时间不对,于是上去使用nptdate同步了下时间,发现还是不对,仔细一看原来是时区设置错误了。
接下来我们可以将时区设置回正确值:
[root@vm3 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp:是否覆盖“/etc/localtime”? y
[root@vm3 ~]# date -R
Mon, 07 May 2012 16:30:50 +0800
[root@vm3 ~]# date
2012年 05月 07日 星期一 16:30:52 CST
[root@vm3 ~]# hwclock -w
另外一些关于时间设置的系统配置文件和命令:
/etc/sysconfig/clock
选择时区 tzselect
设置时间 timeconfig
写入硬件 hwclock -w
-The End-