RHCE系列之ntp服务器----维护篇

简介:

NTP服务器的维护大概分为2部分,第一部分为日常管理命令和常用操作,第二部分为错误的排查和解决方案


第一部分:管理命令

1、ntpstat命令

ntpstat是用来查看ntp服务器和上层是否连通的命令

1
2
3
4
[root@c64-ntp-1 ~] # ntpstat
synchronised to NTP server (202.112.31.197) at stratum 3
    time  correct to within 81 ms
    polling server every 64 s

当执行命令,出现synchronised之后,才表示连通状态。这个过程大约需要在启动之后5分钟才行,为什么5分钟呢,下面就会讲到。


2、ntpq命令

ntpq是用来监视ntpd进程操作的,使用标准的NTP模式6的控制消息模式,并与NTP服务器通信。

ntpq -p查询网络中的NTP服务器,同时显示目前NTP服务器和上级NTP服务器的关系。

1
2
3
4
5
6
[root@c64-ntp-1 ~] # ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ntp.glnet.edu.c 202.112.10.60    2 u   23   64  377   63.481   37.308  16.341
+Hshh.org        204.152.184.72   2 u   16   64  377   15.475   33.557  15.059
*dns2.synet.edu. 202.118.1.46     2 u   47   64  377   47.493   35.020  15.339

选项详解:

1
2
3
4
5
6
7
8
9
10
remote: 指最先响应这个请求的NTP服务器的名称
refid: 指给本地NTP服务器提供时间同步的上级NTP服务器源。
st: 该值用来表示上级NTP服务器源的层级别。由于NTP是层型结构,有顶端的服务器,多层的NTP服务器源,再到客户端。所以服务器从高到低级别可以设定为1-16。为了减缓负荷和网络堵塞,原则上应该避免直接连接到层级别为1的服务器上。
t: 该值用来表示远程服务器的类型,有如下类型:u代表单播或多播的客户端,b代表广播或组播服务器,l代表本地(参考时钟),s代表同级NTP服务器, A代表多播服务器,B代表广播服务器,M代表组播服务器
when: 该值为自上次接收到数据包的时间距离当前的时间值
poll: 该值为本地NTP服务器和上级NTP服务器源的轮询间隔,即双方每过多久同步一次(单位为秒)。在NTP刚开始运行时,poll值会比较小,此时和上级NTP服务器源同步的频率就会增加,因此可以尽快调整到正确的时间范围内。之后poll值就会逐渐增大,同步的频率也就会相应减小。
reach:这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加
delay:该值为本地NTP服务器和上级NTP服务器源的NTP报文的往返时延。
offset:该值为本地NTP服务器和上级NTP服务器源的时间差值。该值越接近0,我们和上级NTP服务器源的时间就越准确。
jitter:该值是用来统计在特定个连续的连接数里面offset(时间差值)的分布情况。该数值的绝对值越小,我们和上级NTP服务器源的时间就越准确。

知道了每个参数的作用,我们大家也许会有疑问,我明明添加的三台上级NTP源服务器不是这三个地址啊,为什么这里会出现这三个地址呢?

第一、因为NTP提供给我们的是一个Cluster Server的服务器集群,所以每次连接得到的服务器都有可能不一样。同样也告诉我们了在指定NTP服务器地址时应该使用hostname而不是IP。

第二、既然这么多的服务器是为了在一台出问题的时候其他的服务器还可以正常地给我们提供服务。那么如何知道这些服务器的状态呢?这就是第一列的第一个字符的用途了!它会告诉我们服务器的情况,让我们做做出判断。

1
2
3
4
5
*  它告诉我们远端的服务器已经被确认为我们的主NTP Server,我们系统的时间将由这台机器所提供
+  它将作为辅助的NTP Server和带有*号的服务器一起为我们提供同步服务. 当*号服务器不可用时它就可以接管
- 远程服务器被clustering algorithm(聚类算法)认为是不合格的NTP服务器
" " (空格) 没有响应的NTP服务器
x 远程服务器不可用

3、ntptrace命令

ntptrace是用来用來追踪NTP服务器与某台时间服务器的时间对应关系的

1
2
[root@c64-ntp-1 ~] # ntptrace -n 1.cn.pool.ntp.org
1.cn.pool.ntp.org: stratum 2, offset 0.007372, synch distance 0.097818


第二部分:错误排查

当我们在客户端用ntpdate IP来同步NTP服务器时间时,有时会发现报no server suitable for synchronization found的信息,导致该信息产生的原因主要有以下2个:

第一、Server dropped: Strata too high

在NTP客户端运行ntpdate IP时,出现no server suitable for synchronization found的错误。

1
2
[root@c64-client ~] # /usr/sbin/ntpdate 192.168.1.111
  3 Nov 06:20:45 ntpdate[48188]: no server suitable  for  synchronization found

在NTP客户端用ntpdate –d IP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@c64-client ~]# /usr/sbin/ntpdate -d  192.168 . 1.111
  3  Nov  06 : 19 : 55  ntpdate[ 48178 ]: ntpdate  4.2 .4p8@ 1.1612 -o Fri Feb  22  11 : 23 : 28  UTC  2013  ( 1 )
Looking  for  host  192.168 . 1.111  and service ntp
host found :  192.168 . 1.111
transmit( 192.168 . 1.111 )
receive( 192.168 . 1.111 )
transmit( 192.168 . 1.111 )
receive( 192.168 . 1.111 )
transmit( 192.168 . 1.111 )
receive( 192.168 . 1.111 )
transmit( 192.168 . 1.111 )
receive( 192.168 . 1.111 )
transmit( 192.168 . 1.111 )
192.168 . 1.111 : Server dropped: strata too high
server  192.168 . 1.111 , port  123
stratum  16 , precision - 24 , leap  11 , trust  000    #stratum的值正常范围为 1 - 16
refid [ 192.168 . 1.111 ], delay  0.02684 , dispersion  0.00017
transmitted  4 in  filter  4
reference time:     00000000.00000000   Thu, Feb   7  2036   6 : 28 : 16.000
originate timestamp: d620698b.ac24dcce  Sun, Nov   3  2013   6 : 19 : 55.672
transmit timestamp:  d620698b.9f531149  Sun, Nov   3  2013   6 : 19 : 55.622
filter delay:   0.02698   0.02774   0.02768   0.02684
          0.00000   0.00000   0.00000   0.00000
filter offset:  0.049317  0.048929  0.048861  0.049286
          0.000000  0.000000  0.000000  0.000000
delay  0.02684 , dispersion  0.00017
offset  0.049286
3  Nov  06 : 19 : 55  ntpdate[ 48178 ]: no server suitable  for  synchronization found

这是因为NTP服务器还没有和其自身或者它的server同步上。

以下的定义是让NTP服务器和其自身保持同步,如果在/etc/ntp.conf中定义的server都不可用时,将使用本地时间作为NTP服务提供给NTP客户端。

添加以下内容到/etc/ntp.conf文件里面即可

1
2
server 127.127.1.0
fudge 127.127.1.0 stratum 8

在NTP服务器上重新启动ntpd服务后,NTP服务器自身或者与其上级NTP服务器源的同步需要一个时间段,这个过程可能是5分钟,在这个时间之内客户端运行ntpdate命令时就会产生no server suitable for synchronization found这样的错误。

那么如何知道何时NTP服务器完成了和自身同步的过程呢?

在NTP服务器上使用如下命令:

1
2
3
4
5
6
7
8
[root@c64-ntp-1 ~] # watch ntpq -p
出现画面:
Every 2.0s: ntpq -p                                     Sun Nov  3 14:26:59 2013
      remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
  gus.buptnet.edu 202.112.10.60    2 u    7   64    3   49.205    0.093   1.257
  dns2.synet.edu. 202.118.1.46     2 u    6   64    3  369.197  150.284  63.078
  LOCAL(0)        .LOCL.           8 l    6   64    3    0.000    0.000   0.000

注意LOCAL的这个就是与自身同步的ntp server。

注意reach这个值,在启动NTP服务端启动ntpd服务后,这个值就从0开始不断增加,当增加到17的时候(poll是经过5次的变更实现从0到17的),每一次是poll的值的秒数,即64秒*5=320秒的时间。

如果之后从NTP客户端同步NTP服务器还失败的话,用ntpdate –d IP来查询详细错误信息,再做判断。

第二、Server dropped: no data

从客户端执行netdate –d时有错误信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@c64-client ~]# /usr/sbin/ntpdate -d  192.168 . 1.111
  3  Nov  06 : 50 : 02  ntpdate[ 48271 ]: ntpdate  4.2 .4p8@ 1.1612 -o Fri Feb  22  11 : 23 : 28  UTC  2013  ( 1 )
Looking  for  host  192.168 . 1.111  and service ntp
host found :  192.168 . 1.111
transmit( 192.168 . 1.111 )
transmit( 192.168 . 1.111 )
transmit( 192.168 . 1.111 )
transmit( 192.168 . 1.111 )
transmit( 192.168 . 1.111 )
192.168 . 1.111 : Server dropped: no data      #此处显示no data
server  192.168 . 1.111 , port  123
stratum  0 , precision  0 , leap  00 , trust  000
refid [ 192.168 . 1.111 ], delay  0.00000 , dispersion  64.00000
transmitted  4 in  filter  4
reference time:     00000000.00000000   Thu, Feb   7  2036   6 : 28 : 16.000
originate timestamp:  00000000.00000000   Thu, Feb   7  2036   6 : 28 : 16.000
transmit timestamp:  d620709d.580fb4d8  Sun, Nov   3  2013   6 : 50 : 05.343
filter delay:   0.00000   0.00000   0.00000   0.00000
          0.00000   0.00000   0.00000   0.00000
filter offset:  0.000000  0.000000  0.000000  0.000000
          0.000000  0.000000  0.000000  0.000000
delay  0.00000 , dispersion  64.00000
offset  0.000000
  3  Nov  06 : 50 : 06  ntpdate[ 48271 ]: no server suitable  for  synchronization found

出现这个问题的原因可能有两点:

1)检查ntp的版本

如果你使用的是ntp-4.2(包括4.2)之后的版本,那么在restrict的定义中使用了notrust的话,会导致以上错误。

我们可以用下面的命令检查ntp的版本

1
2
[root@c64-ntp-1 ~] # ntpq -c version
ntpq 4.2.4p8@1.1612-o Fri Feb 22 11:23:30 UTC 2013 (1)

大家可以看到我这里用的版本是4.2.4的。

下面我截取了NTP官方文档对此问题的解释:

1
2
3
The behavior of notrust changed between versions 4.1 and 4.2.
In 4.1 (and earlier) notrust meant  "Don't trust this host/subnet for time" .
In 4.2 (and later) notrust means  "Ignore all NTP packets that are not cryptographically authenticated."  This forces remote  time  servers to authenticate themselves to your (client) ntpd

解放方案:

编译/etc/ntp.conf,把notrust去掉即可。


2)检查NTP服务器的防火墙

导致此种问题的另一原因就是NTP服务器的防火墙屏蔽了upd的123号端口。

1
[root@c64-ntp-1 ~] # /etc/init.d/iptables stop   #先关闭防火墙,进行错误排查

我们可以先关闭iptables服务之后,再尝试从NTP客户端进行时间同步。如果时间同步成功,则证明是防火墙的问题,那么我们就需要更改iptables的配置策略了。


到这里,本系列博文就告一段落了。我用了3篇博文来为大家讲述时间和NTP的概念,以及在生产环境下的简易机构的实现负载均衡和高可用功能的NTP服务器部署流程,然后在末尾一篇为大家介绍了一些NTP服务的日常管理命令和错误排查流程。这里也是给加一个思路,当出问题的时候,能更好的去解决它。希望对大家有所帮助!










本文转自 aaao 51CTO博客,原文链接:http://blog.51cto.com/nolinux/1319597,如需转载请自行联系原作者

目录
相关文章
|
Ubuntu 安全 机器人
把Windows打造成一个NTP时间同步服务器
把Windows打造成一个NTP时间同步服务器
把Windows打造成一个NTP时间同步服务器
|
弹性计算 Windows
同步服务器时间NTP
在秒杀、抢券等高并发的活动中,一点点的时间延迟,也会让自己后悔莫及……
1467 0
同步服务器时间NTP
|
Ubuntu Linux 虚拟化
使用阿里云镜像站NTP服务搭建NTP服务器(基于CentOS 7系统)
使用阿里云镜像站NTP服务搭建NTP服务器(基于CentOS 7系统)
1401 0
使用阿里云镜像站NTP服务搭建NTP服务器(基于CentOS 7系统)
|
安全 Linux 网络安全
手把手教你在centos 7.4上搭建NTP服务器
手把手教你在centos 7.4上搭建NTP服务器
730 0
手把手教你在centos 7.4上搭建NTP服务器
|
5月前
|
Linux
CentOS7搭建ntp时钟服务器
CentOS7搭建ntp时钟服务器
656 0
|
7月前
NTP服务器时间配置
NTP服务器时间配置
157 0
|
11月前
|
安全 Linux 网络安全
Linux 时间同步 -NTP 服务器
本文主要介绍如何配置 Linux 服务器集群的时间同步,技术实现上使用 NTP 服务器和客户端同步时间。
429 0
|
Linux 网络安全
linux篇-CentOS7搭建NTP服务器
linux篇-CentOS7搭建NTP服务器
865 0
linux篇-CentOS7搭建NTP服务器
|
BI 数据库 C语言
西门子PCS7系统中央归档服务器、维护站、OpenPCS7站的作用是什么?如何安装?
今天我们简单介绍一下西门子PCS7系统中央归档服务器、维护站、OpenPCS7站的作用是什么以及如何安装。
西门子PCS7系统中央归档服务器、维护站、OpenPCS7站的作用是什么?如何安装?
|
Ubuntu Linux 虚拟化
【阿里云镜像】使用阿里云镜像站NTP服务搭建NTP服务器(基于CentOS 7系统)
网络时间协议(Network Time Protocol,NTP)服务器,也就是日常所说的NTP服务器,用来提供同步时间服务。在生产环境中,很多人都会忽略时间问题,实际上服务器、网络设备等,特别是Linux操作系统和虚拟化平台的时间不同步会导致很多问题。那么搭建一台NTP服务器就非常重要,生产环境中的设备可以直接与NTP服务器进行时间同步,NTP服务器本身也可以访问互连的NTP服务器进行同步。NTP服务器可以是物理服务器,也可以是虚拟机。
1202 0
【阿里云镜像】使用阿里云镜像站NTP服务搭建NTP服务器(基于CentOS 7系统)