1、查看时间服务器的时间是否已经完成同步
watch ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *appnode142 LOCAL(0) 11 u 601 1024 377 0.134 0.090 0.000 LOCAL(0) .LOCL. 10 l 24 64 377 0.000 0.000 0.000 带星号的才是生效的规则,offset的单位是毫秒。第二条规则是在时间服务器appnode142失效时候,就使用本地的时钟来同步。 *表示目前使用的ntp server,这里选择的本机; remote: 它指的就是本地机器所连接的远程NTP服务器 refid: 它指的是给远程服务器(e.g. 193.60.199.75)提供时间同步的服务器 st:即stratum阶层,值越小表示ntp serve的精准度越高; when:几秒前曾做过时间同步更新的操作; Poll表示,每隔多少毫秒与ntp server同步一次; reach:已经向上层NTP服务器要求更新的次数; delay:网络传输过程钟延迟的时间; offset:时间补偿的结果; jitter:Linux系统时间与BIOS硬件时间的差异时间 2、不用重启ntpd 服务,就能使同步生效 ntpdate -u 132.***.***.142 3、ntpstat命令,查看ntp的状态 synchronised to NTP server (132.***.***.142) at stratum 12 time correct to within 38 ms polling server every 1024 s 上面的信息说明ntp时间服务器是(132.***.***.142),每1024秒同步一次 4、之前遇到datanode-97-33 ntp老是通过本地的时钟同步,后来重启服务器后问题得到解决。
*************************************************************************************************************
一、ntp服务端 1、检查并安装ntp服务 [root@c64-ntp-1 ~]# rpm -qa|grep ntp #如果出现下面两个就说明本机已经有了 ntpdate-4.2.4p8-3.el6.centos.x86_64 ntp-4.2.4p8-3.el6.centos.x86_64 如果没有,我们执行下面的命令进行安装 [root@c64-ntp-1 ~]# yum install ntp -y 2、同步NTP服务器时间 由于NTP的限制,如果系统时间比正确的时间要快的话,NTP是不会帮你做调整的,而且当你的时间设置和正确的时间相差很大的时候,NTP会花上很长一段时间进行同步调整,因此鉴于此,我们需要先做一个同步。 首先我们要先确定好自己的时区,最好根据所在地点从新设置时区,本例以上海为例 [root@c64-ntp-1 ~]#/bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #这样我们就将时区设置成上海了 [root@c64-ntp-1 ~]# date Sat Nov 2 23:17:36 CST 2013 #可以看到我们的时区已经设置过来了 接下来就要向互联网上的NTP服务器进行时间同步了。这里你如果没有NTP服务器的地址,你可以到http://www.pool.ntp.org(NTP官网)去找离自己城市最近的NTP服务器。由于我用的是上海的地址,所以这里我将离上海最近的3个NTP服务器贴出来。 server 1.cn.pool.ntp.org server 3.asia.pool.ntp.org server 0.asia.pool.ntp.org 同步本机时间的命令是 [root@c64-ntp-1 ~]# /usr/sbin/ntpdate 1.cn.pool.ntp.org 2 Nov 23:05:34 ntpdate[11560]: adjust time server 218.75.4.130 offset -0.114499 sec [root@c64-ntp-1 ~]# /usr/sbin/ntpdate 1.cn.pool.ntp.org #为了减少时延,我们最好执行两遍 2 Nov 23:05:41 ntpdate[11561]: adjust time server 218.75.4.130 offset -0.111105 sec 二、配置NTP服务器 其实,此时我们用/usr/sbin/ntpd start 已经可以向客户端提供时间更新服务了。但是,这样是满足不了企业安全性需求的。下面将为大家讲解ntp的主配置文件设定。 1)解决NTP服务器校准时间时的传送延迟 使用driftfile参数设置: driftfile 文件名 用途:将与上级时间服务器联系时所花费的时间,记录在driftfile参数后面指定的文件内 注意:driftfile后面必须接完整的文件路径,不能是链接文件,并且必须要有ntpd守护进程可以写入的权限。 对应默认配置项: 1 driftfile /var/lib/ntp/drift 2)权限的控制主要靠restrict这个参数 要的语法为:restrict IP mask 掩码 参数 IP规定了允许或不允许访问的地址(此处若为default,即为0.0.0.0所有ip),配合掩码可以对某一网段进行限制。 参数包括: ignore:关闭所有NTP服务 nomodify:客户端不能修改服务端的时间,但可以作为客户端的校正服务器 noquery:不提供时间查询,即用户端不能使用ntpq,ntpc等命令来查询ntp服务器 nopeer:不与同一层的其他服务器进行时间同步 kod:kod技术可以阻止“kiss of death”包(一种DOS攻击)对服务器的破坏 notrap:不提供trap远端事件登陆的功能 notrust:客户端除非通过认证,否则指定的网段为不信任网段 #ntp4.2之后的版本,已经默认没有这个参数,如果你添加了,会报错的。 对应默认配置项: restrict default kod nomodify notrap nopeer noquery #默认对所有client拒绝所有的操作 restrict -6 default kod nomodify notrap nopeer noquery restrict 127.0.0.1 #允许本机地址的一切操作 restrict -6 ::1 3)用server这个参数设定上级时间服务器 语法为: server IP地址或域名 [prefer] IP地址或域名即为该NTP服务器指定的上级NTP服务器。当指定多个NTP服务器时,使用prefer参数的服务器优先级最高,如果都没有使用prefer参数,那么服务器的优先级则按从上到下的顺序依次由高到低。在指定上层服务器后,至少15min才会与上层NTP服务器进行时间校对。 对应默认配置项: server 0.centos.pool.ntp.org server 1.centos.pool.ntp.org server 2.centos.pool.ntp.org 下面我们要对配置文件进行精简和更改,更改之后的配置文件如下: [root@c64-server-1 ~]# cat /etc/ntp.conf driftfile /var/lib/ntp/drift restrict default kod nomodify notrap nopeer noquery notrust #默认对所有client拒绝所有的操作 restrict 127.0.0.1 #允许本机地址的一切操作 restrict 192.168.1.0 mask 255.255.255.0 nomodify #允许局域网内所有client连接到这台服务器同步时间.但是拒绝让他们修改服务器上的时间 server 1.cn.pool.ntp.org perfer #指定该上级NTP服务器为最优先 server 3.asia.pool.ntp.org #这个上级NTP服务器是在官网找到离上海最近的NTP服务器 server 0.asia.pool.ntp.org #这里我们也可以用上海交大之类提供的公益性NTP服务器 includefile /etc/ntp/crypto/pw keys /etc/ntp/keys 配置完成之后,我们就可以启动服务 12 [root@c64-ntp-1 ~]# /etc/init.d/ntpd start Starting ntpd: [ OK ] 启动完成后,我们通过查看端口和进程来看服务是否真的起来了 [root@c64-ntp-1 ~]# netstat -lntup|grep ntp udp 0 0 192.168.1.113:123 0.0.0.0:* 11992/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 11992/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 11992/ntpd udp 0 0 fe80::20c:29ff:fe06:9c22:123 :::* 11992/ntpd udp 0 0 ::1:123 :::* 11992/ntpd udp 0 0 :::123 :::* 11992/ntpd [root@c64-ntp-1 ~]# ps -ef |grep ntp|grep -v grep ntp 11992 1 0 02:17 ? 00:00:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g 加入开机启动项 [root@c64-ntp-1 ~]# echo '#ntp server boot configuration by sunsky in 20131020' >>/etc/rc.local [root@c64-ntp-1 ~]# echo '/etc/init.d/ntpd start start' >>/etc/rc.local [root@c64-ntp-1 ~]# tail -2 /etc/rc.local #检查配置结果 #ntp server boot configuration by sunsky in 20131020 /etc/init.d/ntpd start 四、负载均衡和高可用配置 为了保证实际生产环境中业务的可持续运作,一台NTP服务器有时间是不能满足需求了,因此这里我们需要做负载均衡和高可用,即平时分担两台NTP服务器的查询负载,即时一台NTP服务器挂掉,另外一台服务器还能照常提供时间同步服务。 由于上面我们已经配置出一台NTP服务器了,所以说这里我们只需要按照上面的部署方法,再部署一台即可。 五、客户端配置(此处不需要配置crontab也行,直接通过ntp自动和ntp server同步时钟,ntpstat可以查看同步时间间隔) 1、测试时间同步 现在我们在客户端进行时间同步的测试,同时要注意将系统时间和硬件时间进行同步,同步命令可以用hwclock systohc 或者hwclock –w。 [root@c64-client /]# /usr/sbin/ntpdate 192.168.1.111&&hwclock -w 3 Nov 03:50:34 ntpdate[19587]: adjust time server 192.168.1.111 offset -0.046270 sec [root@c64-client /]# /usr/sbin/ntpdate 192.168.1.112&&hwclock -w 3 Nov 03:50:48 ntpdate[19589]: adjust time server 192.168.1.112 offset -0.039505 sec 2、添加定时任务 因为我们不可能一直手动去同步时间,因此我们需要将同步时间命令添加到定时任务中去。由于我们启用了2台时间服务器,所以在客户端进行更新时间的定时任务设置时,我们就采用2台服务器每5分钟轮流更新的方式。 [root@c64-client ~]# echo '*/5 * * * * /usr/sbin/ntpdate 192.168.1.111&&hwclock -w >/dev/null 2>&1' >>/var/spool/cron/root [root@c64-client ~]# echo '*/10 * * * * /usr/sbin/ntpdate 192.168.1.112&&hwclock -w >/dev/null 2>&1' >>/var/spool/cron/root 添加之后,别忘了用下面的命令查看结果哦。 [root@c64-client /]# crontab -l */5 * * * * /usr/sbin/ntpdate 192.168.1.111&&hwclock -w >/dev/null 2>&1 */10 * * * * /usr/sbin/ntpdate 192.168.1.112&&hwclock -w >/dev/null 2>&1 确认无误之后,我们的部署就OK了! 到这里为止,我们的负载均衡和高可用的NTP服务器已经部署完成了。其中还有很多后期维护和故障的摆错,我将放到下一篇博文来讲解。因此,在本篇只要大家能按照这个部署下来,就已经很不错了。NTP服务器部署的过程很简单,它复杂就复杂在世面上可供大家参考的资料少,有些参数的效果也不是太好。一旦出错,就让人很郁闷头疼了。所以,关于NTP的一些细节概念一定要把握好。