Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux 下 NTP 服务器的日常维护与故障排查

部署好 NTP 只是开始,真正决定它好不好用的是维护。这一篇分成两块:日常管理常用的三条命令与字段含义,以及最常见的两类同步失败如何定位。

第一部分:管理命令

1. ntpstat:确认与上层是否连通

[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 分钟。原因见后文的 reach 说明。

2. ntpq -p:查看与上层的关系

[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

字段含义:

  • remote:最先响应请求的 NTP 服务器;refid:给本地服务器提供校时的上级来源。
  • st:上级来源的层级。NTP 是阶层结构,层级 1 到 16,数字越小越接近时间源头;为减轻负载与网络拥塞,原则上应避免直接连到层级 1。
  • t:远程服务器类型,u 为单播/多播客户端,b 为广播/组播服务器,l 为本地参考时钟,s 为同级服务器。
  • when:距上次接收数据包的时间;poll:轮询间隔(秒)。刚启动时 poll 较小、同步较频繁,以便尽快校准,之后逐渐增大。
  • reach:八进制值,每成功连接一次就加一,用来测试能否连通。
  • delay:报文往返时延;offset:与上级的时间差,越接近 0 越准;jitter:offset 的分布统计,绝对值越小越精确。

常有人疑惑:明明设定的是三台上层服务器,为什么这里出现别的地址?因为 pool 提供的是服务器集群,每次解析到的机器都可能不同——这也是建议用 hostname 而非 IP 来指定 NTP 服务器的原因。而最左列的符号正是用来判断状态的:

  • *:已被选为主服务器,系统时间由它提供
  • +:候补服务器,主服务器失效时可接管
  • -:被聚类算法判定为不合格
  • 空白:无响应;x:远程服务器不可用

3. ntptrace:追踪时间对应关系

[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 时出现 no server suitable for synchronization found,多半属于以下两种情形之一。

情形一:Server dropped: Strata too high

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

[root@c64-client ~]# /usr/sbin/ntpdate -d 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,出现 16 说明 NTP 服务器尚未与自己或它的上级同步上。

解法是让服务器先能与自身保持同步:在 /etc/ntp.conf 中加入下面两行,当设定的 server 都不可用时,就以本地时间对外提供服务。

server 127.127.1.0
fudge 127.127.1.0 stratum 8

重启 ntpd 后,服务器完成自身或上级同步需要一段时间(可能约 5 分钟),这段时间内客户端同步就会报同样的错。想知道何时完成,可在服务器上持续观察:

[root@c64-ntp-1 ~]# watch ntpq -p

   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 这一行就是与自身同步的来源。关键看 reach:ntpd 启动后它从 0 开始递增,增到 17(八进制,相当于十进制 15)时表示已完成 5 次 poll 变更,即约 64 秒乘 5、三百多秒的时间——这正是 ntpstat 要等几分钟才有结果的原因。

情形二:Server dropped: no data

[root@c64-client ~]# /usr/sbin/ntpdate -d 192.168.1.111
192.168.1.111: Server dropped: no data
server 192.168.1.111, port 123
stratum 0, precision 0, leap 00, trust 000
delay 0.00000, dispersion 64.00000

常见原因有两个:

(1)restrict 中的 notrust 与版本行为差异。ntp 4.2 之后,notrust 的含义从「不信任此主机的时间」变成「忽略所有未经加密认证的 NTP 封包」,于是客户端会被拒。先确认版本:

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

官方文档对此的说明是:4.1 及更早版本中 notrust 意为不信任该主机/子网的时间;4.2 及之后意为忽略所有未通过密码学认证的封包,这会强制远程服务器向你的 ntpd 认证身份。解法很简单:编辑 /etc/ntp.conf,把 notrust 去掉。

(2)防火墙挡住了 UDP 123。可先关闭 iptables 再测一次,若同步成功即确认是防火墙问题,接着调整规则放行 123/UDP 即可,无需长期关闭防火墙:

[root@c64-ntp-1 ~]# /etc/init.d/iptables stop

把这两类错误的原因摸清楚,NTP 的维护基本就没什么神秘的了:先看 reach 有没有涨起来,再看 stratum 是否过高,最后检查认证参数与端口。

Copyright © Linux教程網 All Rights Reserved