1、3台cache服务器时间有差异
同事说3台服务器时间不一致,在执行分布式任务的时候出了诡异的问题,赶紧去看下3台cache服务器,果然彼此相差几分钟。
[root@cache_3_40 ~]# date
2016年 07月 26日 星期二 17:24:57 CST
[root@cache_3_40 ~]#
[root@cache_3_41 ~]# date
2016年 07月 26日 星期二 17:27:44 CST
[root@cache_3_41 ~]#
[root@cache_3_42 ~]# date
2016年 07月 26日 星期二 17:29:46 CST
[root@cache_3_42 ~]#
记得在搭建服务器的时候,通过date -s 09:38:40设置过时间,而且还用设置好了时区,预计是linux系统运行久了后,会存在一点点时间上的误差,所以需要重新进行时间同步
2、重新设置时间同步
先设置时区:
cp -f /usr/share/zoneinfo/Asia/Shanghai/etc/localtime -y
echo 'ZONE="Asia/Shanghai"' >/etc/sysconfig/clock
hwclock
首先需要安装同步服务组件ntpdate
yum install -y ntpdate
先手动ntpdate命令同步时间,同步报错
[root@cache_3_41 ~]# /usr/sbin/ntpdate -u pool.ntp.org
26 Jul 17:33:14 ntpdate[20154]: the NTP socketis in use, exiting
[root@cache_3_41 ~]#
原因是有ntpd服务占据了socket端口,所以关闭ntpd服务,然后再执行同步命令
[root@cache_3_40 ~]# service ntpd stop
关闭 ntpd: [确定]
[root@cache_3_40 ~]# /usr/sbin/ntpdate -u pool.ntp.org
26 Jul 17:44:02 ntpdate[32191]: step timeserver 115.28.122.198 offset 693.768108 sec
[root@cache_3_40 ~]#
[root@cache_3_40 ~]# date
2016年 07月 26日 星期二 17:44:05 CST
[root@cache_3_40 ~]#
PS:加上-u参数:
若不加上-u参数,会出现以下提示:no server suitable for synchronization found;-u:从man ntpdate中可以看出-u参数可以越过防火墙与主机同步;
PS:date命令手动设置时间:
date :查看当前时间,结果如下:Tue Mar 401:36:45 CST 2014
date -s 09:38:40 :设置当前时间,结果如下:TueMar 4 09:38:40 CST 2014
3、设定crontab定时任务及时同步时间
为了crontab任务的允许安全起见,取消ntpd开机自启动
chkconfig ntpd off;
[root@cache_3_42 ~]# chkconfig ntpd off;
[root@cache_3_42 ~]#
然后这里设定自动同步crontab任务,每隔30分钟同步一次。
crontab -e
*/30 * * * * /usr/sbin/ntpdate -upool.ntp.org >> /var/log/crontab.log 2>&1
4、一些常用的时间服务器
ntp常用服务器:
中国国家授时中心:210.72.145.44
NTP服务器(上海) :ntp.api.bz
美国:time.nist.gov
复旦:ntp.fudan.edu.cn
微软公司授时主机(美国) :time.windows.com
台警大授时中心(台湾):asia.pool.ntp.org
经测试中国国家授时中心与NTP上海服务器可以正常同步时间,注意需要加上-u参数!