一、NTP的简介
NTP全称网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)进行时间同步,它可以提供高精准度的时间校正,而且可以使用加密确认的方式来防止病毒的协议攻击。
二、NPT应用场景
NTP通常用于同步网络中有各个计算机时间的一致性,需要保证所有网络设备的时间高度一致,且对时间精确度的要求比较高场景。例如火箭发射这种科研活动,对时间的统一性和准确性要求就非常地高,必须确定采用以哪台计算机的时间为准确发射时间;还有分布式及集群部署的场景中,要想保证网络中的各个节点在一个集群内,集群的各个计算机时间必须保持一致。
三、NTP时间同步
1、ntp时间同步方式
通常情况下,我们不可能让内网中的所有设备都去连接外网同步,这些也不太安全,大都是采用一台设备去外网同步时间,并且该设备还作为内网中其他设备的ntp-server,内网中的其他网络设备同步设该设备即可。
服务器IP | 角色 | 说明 | 同步方式 |
192.168.1.71(node01) | 内网设备(ntp-server) | 1、负责与外部公共NTPD服务同步标准时间 2、作为内网其它设备的NTPD服务 | NTPD服务平滑同步 |
192.168.1.72(node02) | 内网设备(ntp-client) | 内网设备去node01同步时间 | NTPD服务平滑同步 |
192.168.1.73(node03) | 内网设备(ntp-client) | 内网设备去node01同步时间 | NTPD服务平滑同步 |
ntp服务器可以参看http://www.ntp.org.cn/pool.php
2、安装ntp时间
(1)检查是否安装ntp服务
$ rpm -qa |grep ntp
出现类似如下情况,表示已经安装
ntp-4.2.6p5-28.el6.centos.x86_64
如果没有安装,yum进行安装即可
$ yum install -y ntp
按上面的安装方式在内网每台服务器上都安装好NTP软件包。
完成后,都需要配置NTP服务为自启动
(2)设置开机自启
$ chkconfig ntpd on
(3)查看ntp状态
$ chkconfig --list ntpd
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
在2、3、4、5 处是on表示开机自启
3、配置ntp时间同步
(1)配置内网ntp-server(node01)
编辑ntp配置 $ vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
#允许远程时间源与我们本地源同步,但不允许远程源查询或修改本本地系统服务
restrict default nomodify notrap nopeer noquery
#允许通过回环地址127.0.0.1的所有访问.也可以被收紧限制,但这样会影响一些本职的功能
restrict 127.0.0.1
restrict ::1
#本地网络主机限制较少。默认被注释了,放开注释,允许内网其他主机同步本机时间
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#这里换成以下几个ntp服务器
server 0.cn.pool.ntp.org #China
server 1.asia.pool.ntp.org #Asia
server 2.centos.pool.ntp.org #centos
server 3.pool.ntp.org #Worldwide
#允许上层时间服务器主动修改本机时间
restrict 0.cn.pool.ntp.org nomodify notrap noquery
restrict 1.asia.pool.ntp.org nomodify notrap noquery
restrict 2.centos.pool.ntp.org nomodify notrap noquery
restrict 3.pool.ntp.org nomodify notrap noquery
#上边列表中的时间服务器都不可用时,同本地时间同步
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
(2)配置内网NTP-Clients
内网其他设备作为NTP的客户端配置,相对就比较简单,而且所有设备的配置都相同。
$ vim /etc/ntp.conf
driftfile /var/lib/ntp/drift
#允许远程时间源与我们本地源同步,但不允许远程源查询或修改本本地系统服务
restrict default nomodify notrap nopeer noquery
#允许通过回环地址127.0.0.1的所有访问.也可以被收紧限制,但这样会影响一些本职的功能
restrict 127.0.0.1
restrict ::1
#本地网络主机限制较少。默认被注释了,放开注释,允许内网其他主机同步本机时间
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#这里换成以下几个ntp服务器
server 192.168.1.71 #内网ntp-server服务器
#允许上层时间服务器主动修改本机时间
restrict 192.168.1.71 nomodify notrap noquery
#上边列表中的时间服务器都不可用时,同本地时间同步
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
4、启动ntp
(1)启动ntp-server(node01)。
启动ntp服务 $ service ntpd start后。用$ ntpstat 命令查看时间同步状态,这个一般需要5分钟左右才能成功连接和同步。
刚启动的时候未同步:
unsynchronised
time server re-starting
polling server every 64 s
连接并同步后:
synchronised to NTP server (202.108.6.95) at stratum 3
time correct to within 68 ms
polling server every 128 s
查看同步网络中的NTP服务器状况 $ ntpq -pn,如下
remote refid st t when poll reach delay offset jitter
==============================================================================
*78.46.102.180 89.231.96.83 2 u 27 256 377 182.463 1.386 0.679
+210.173.160.87 133.243.236.17 2 u 224 256 377 186.914 20.632 13.542
-94.130.49.186 195.13.23.5 3 u 144 256 271 206.529 -27.483 0.505
+193.228.143.14 194.58.202.20 2 u 15 256 377 257.885 -36.178 13.940
127.127.1.0 .LOCL. 10 l 88m 64 0 0.000 0.000 0.000
看到带“*”的remote表示已经同步成功了
各表示含义如下:
*: 目前使用的NTP服务器
+:表示可能作为下一个提供时间更新的候选者
空白:空白,remote前什么都不带的,表示没有响应的NTP服务器
remote:响应这个请求的NTP服务器的名称
refid:NTP服务器使用的更高一级服务器的名称
st:即stratum层级,响应请求的NTP服务器的层级,值越小越精确
t:本地NTP服务器与远程NTP服务器的通信方式,u: 单播;m:多播,不可跨子网 ;b: 广播,不可跨子网; l: 本地
when:上一次成功请求之后到现在的秒数
poll:本地和远程服务器多少秒进行一次同步。在一开始运行NTP的时候这个poll值会比较小,服务器同步的频率大,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小
reach:是一个八进制值,每成功连接一次它的值就会增加,即已经向上层NTP服务器要求更新的次数
delay:从本地机发送同步要求到ntp服务器的往返时间
offset:主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒,offset越接近于0,主机和ntp服务器的时间越接近
jitter:统计了在特定个连续的连接数里offset的分布情况。简单地说这个数值的绝对值越小,主机的时间就越精确
(2)启动ntp-server(node02、node03)
分别node02、node03上启动ntp服务 $ service ntpd start。因为是内网,一般ntpstat很快就可以同步上,用$ntpstat 查看命令同步状况即可。若是未同步unsynchronised等个5 分钟左右
synchronised to NTP server (192.168.172.71) at stratum 4
time correct to within 358 ms
polling server every 128 s
在node02、node03上查看同步服务器状况 $ ntpq -pn
remote refid st t when poll reach delay offset jitter
==============================================================================
*192.168.172.74 78.46.102.180 3 u 9 64 377 0.180 10.042 0.667
127.127.1.0 .LOCL. 10 l 20 64 377 0.000 0.000 0.000
可以看到已经同步成功了