首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >系统时间——ntpd 原

系统时间——ntpd 原

作者头像
阿dai学长
发布2019-04-03 10:30:01
3K0
发布2019-04-03 10:30:01
举报
文章被收录于专栏:阿dai_linux阿dai_linux

The ntpd program is an operating system daemon which sets and maintains the system time of day in synchronism with Internet standard time servers. It is a complete implementation of the Network Time Protocol (NTP) version 4, but also retains compatibility with version 3, as defined by RFC-1305, and version 1 and 2, as defined by RFC-1059 and RFC-1119, respectively. ntpd does most computations in 64-bit floating point arithmetic and does relatively clumsy 64-bit fixed point operations only when necessary to preserve the ultimate precision, about 232 picoseconds. While the ultimate precision, is not achievable with ordinary workstations and networks of today, it may be required with future gigahertz CPU clocks and gigabit LANs.

官网:http://doc.ntp.org/4.1.0/ntpd.htm

ntpd

1. ntp是什么?

NTP服务器【Network Time Protocol(NTP)】是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。 官方文档: http://support.ntp.org/bin/view/Support/WebHome

1.1 ntp服务配置解析

配置文件 “/etc/ntp.conf :”

#记录系统时间与BIOS事件偏差的文件
driftfile /var/lib/ntp/drift

#server:指定ntp服务器
server 202.120.2.101 prefer iburst minpoll 1 maxpoll 8
## prefer:优先使用该时间服务器
## burst:当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。
## iburst:当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。
## 参数minpoll和maxpoll指定NTP消息的最小和最大轮询间隔(以秒为单位)。
## 默认minpoll 6 maxpoll 10,表示的时间间隔分别为2的6次(64秒)方和2的10次方(1024秒)  
## minpoll最小可设置为3(2的3次方=8s),maxpoll最大可设置为17(2的17次方=36h)  
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server 3.pool.ntp.org

# tinker [ allan allan | dispersion dispersion | freq freq | huffpuff huffpuff | panic panic | step step | stepout stepout ]

statistics loopstats peerstats clockstats

filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
restrict ::1

#restrict--控制相关权限:
#其中IP地址也可以是default ,default 就是指所有的IP
## ignore:关闭所有的 NTP 联机服务
## nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
## notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网
## noquery:不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
## notrap:不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
## nopeer:用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
## kod:访问违规时发送 KoD 包。
## restrict -6 表示IPV6地址的权限设置。

总结一下,restrict用来设置访问权限,server用来设置上层时间服务器,driftfile用来设置保存漂移时间的文件。

官方参数解析:

2. ntp相关命令

2.1 ntpq命令

ntpq是查询ntpd工作状态的命令,一般使用“ntpq -np”参数。

[root@host3 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 210.72.145.44   .INIT.          16 u    -   64    0    0.000    0.000   0.000
+ntp1.ams1.nl.le 130.133.1.10     2 u   39   64   17  164.059    4.156  28.802
*sircabirus.von- 36.224.68.195    2 u   42   64   17  200.129   -2.154   5.885
-ntp2.flashdance 194.58.202.148   2 u   40   64   15  326.854   -2.366  29.832
+darwin.kenyonra 127.67.113.92    2 u   36   64   17  167.833    4.292  15.224
  • remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,如果是“*”则表示也有连上线,不过是作为次要联机的NTP主机。
  • t: 本地NTP服务器与远程NTP服务器的通信方式,u: 单播; b: 广播; l: 本地。
  • refid:参考的上一层NTP主机的地址
  • st:即stratum阶层,值越小表示ntp serve的精准度越高;
  • when:几秒前曾做过时间同步更新的操作;
  • Poll表示,每隔多少毫秒与ntp server同步一次;
  • reach:已经向上层NTP服务器要求更新的次数;
  • delay:网络传输过程钟延迟的时间;
  • offset:时间补偿的结果;
  • jitter:本地与remote同步的时间源的平均偏差(多个时间样本中的 offset的偏差,单位是毫秒),这个数值的绝对值越小,主机的时间就越精确

注意: NTP服务端重启后,客户机要等5分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。等待的时间可以通过命令 watch ntpq -p来监控。

2.2 ntptime命令

这个使用特殊程序描述一个内核模型精确计时显示,他调用ntp_gettime()读取和显示时间相关的内核变量。类似的显示可以使用ntpdc程序的kerninfo命令。

[root@host2 ~]# ntptime
ntp_gettime() returns code 0 (OK)
  time de5061d9.7034f000  Mon, Mar 12 2018 10:18:01.438, (.438308),
  maximum error 116705 us, estimated error 7753 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset 0.000 us, frequency -13.286 ppm, interval 1 s,
  maximum error 116705 us, estimated error 7753 us,
  status 0x0 (),
  time constant 4, precision 1.000 us, tolerance 500 ppm,

需要关注的数据:

  • maximum error:最大误差
  • offset:系统时间偏移量

用法指南:https://docs.oracle.com/cd/E56344_01/html/E54077/ntptime-1m.html#scrolltoc

2.3 ntpstat

ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。

  • 刚启动的时候:
[root@host2 ~]# ntpstat
timeout

[root@host2 ~]# ntpstat
unsynchronised
  time server re-starting
   polling server every 64 s
  • 连接成功后
[root@host2 scripts]# ntpstat
unsynchronised
   polling server every 8 s

2.4 ntpdate命令

设置本地日期和时间。它从指定的每个服务器获得了一些样本,并应用标准 NTP 时钟过滤器和选择算法来选择最好的样本。 语法: http://man.linuxde.net/ntpdate eg: 使用ntpdate命令同步网络时间

#安装ntpdate工具
[root@localhost sbin]# yum install -y ntp
##安装ntp的同时会同步安装ntpdate命令

#进行时间同步,语法:ntpdate ntp_server_address
[root@localhost sbin]# ntpdate ntp.ubuntu.com
 3 Sep 20:28:54 ntpdate[3407]: step time server 91.189.91.157 offset 31565561.035581 sec
 
 #查看时间:
 [root@localhost sbin]# date
 2017年 09月 03日 星期日 20:29:02 CST

注意:

  1. 使用ntpdate时候需要关闭ntpd服务;
  2. 虽然你的Linux防火墙允许123端口的udp协议,但是可能你的上层网络服务ISP是禁用特殊端口来传输ntp协议的。这时候使用-u 参数可。
2.4.1 ntpd和ntpdate的区别
  • ntpd在实际同步时间时是一点点的校准过来时间的,最终把时间慢慢的校正对(平滑同步)。而ntpdate不会考虑其他程序是否会阵痛,直接调整时间(“跃变”)。
  • 一个是校准时间,一个是调整时间。
  • “跃变”的危害:
    • 这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。
    • 这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
    • 这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。

<table><tr><td bgcolor=#00FFFF>因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。</td></tr></table>

参考:https://www.cnblogs.com/liuyou/archive/2012/07/29/2614330.html

2.5 应用:

背景:

公司部署的Hadoop(Kudu)集群总是因为节点之间的时差太大导致节点无法连接(服务宕机),经查看是因为节点之间的时间差导致的,所以需要手动调整系统时间后重启服务。

解决办法:

优化系统时间同步,减小系统时间偏移量。

过程: 服务宕机报错:

Error during hole punch test. The log block manager requires a
filesystem with hole punching support such as ext4 or xfs. On el6,
kernel version 2.6.32-358 or newer is required. To run without hole
punching (at the cost of some efficiency and scalability), reconfigure
Kudu with --block_manager=file. Refer to the Kudu documentation for more
details. Raw error message follows.

起初修复办法:https://kudu.apache.org/docs/troubleshooting.html#ntp (安装ntpd服务,重启服务,进行时间同步)

优化办法: 在ntp.conf配置文件中加入如下参数:

#调整时间同步的频率,将其限制在8秒到64秒之间
server 202.120.2.101 prefer iburst minpoll 3 maxpoll 6

#Apache的kudu官方建议(理由是缩小最大误差):
tinker dispersion 500
tinker allan 0

说明:通过调整系统时间的平滑同步平率来所需最大误差。 参考: http://www.mamicode.com/info-detail-1774325.html

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/03/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ntpd
    • 1. ntp是什么?
      • 1.1 ntp服务配置解析
    • 2. ntp相关命令
      • 2.1 ntpq命令
    • 2.2 ntptime命令
      • 2.3 ntpstat
        • 2.4 ntpdate命令
          • 2.4.1 ntpd和ntpdate的区别
        • 2.5 应用:
          • 背景:
          • 解决办法:
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档