zoukankan      html  css  js  c++  java
  • NTP原理

    ntp原理与设置

    原创                     2016年09月17日 15:28:16                

    一.  基本概念

    NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。

    在计算机的世界里,时间非常地重要,例如对于火箭发射这种科研活动,对时间的统一性和准确性要求就非常地高,是按照A这台计算机的时间,还是按照B这台计算机的时间?NTP就是用来解决这个问题的,NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。

    它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)进行时间同步,它可以提供高精准度的时间校正,而且可以使用加密确认的方式来防止恶毒的协议攻击。 这里写图片描述 0层的服务器采用的是原子钟、GPS钟等物理设备,stratum 1与stratum 0 是直接相连的,往后的stratum与上一层stratum通过网络相连,同一层的server也可以交互。

    1、C/S合一

    ntpd对下层client来说是service server,对于上层server来说它是client,也就是说新版的NTP服务程序已经不对服务端和客户端进行区分了,统一叫做ntpd。ntpd根据配置文件的参数决定是要为其他服务器提供时钟服务或者是从其他服务器同步时钟。所有的配置都在/etc/ntp.conf文件中。

    2、NTP客户端同步间隔

    NTP服务会间隔多长时间想时钟服务器请求一次时钟同步呢?默认最小时间间隔为64s,默认最大时间间隔是1024s(17分钟左右)。64s是比较合理的,默认间隔也是可调的(Note that most device drivers will not operate properly if the poll interval is less than 64  s  and  that  the broadcast server and manycast client associations will also use the default, unless overridden.)。

    3、容忍误差范围

    NTP服务并不是在任何情况下都会进行同步的。当时钟服务器时间和本地时间相差大于1000s时,NTP服务就会认为是人为调整了时钟或出现了硬件故障,例如CMOS电池损坏等。此时,NTP服务就会退出,需要人工(ntpdate …)进行时钟同步。

    采用-g选项可以让ntpd忽略1000s或更大误差,设置时钟到server system time, 但是ntpd还是会因此退出。

    4、层次(strata)

    stratum根据上层server的层次而设定(+1)。

    对于提供network time service provider的主机来说,stratum的设定要尽可能准确。

    而作为局域网的time service provider,通常将stratum设置为10 (Stratum 10 is conventional for unsynchronized local clocks; it is high enough that nobody is likely to mistake it for a desirable clock to synchronize with.),如下:

    server  127.127.1.0     # local clock
    fudge   127.127.1.0     stratum 10
    #stratum设置为其它值也是可以的,其范围为0~15
    • 1
    • 2
    • 3

    二、设置与启动

    文件路径:/etc/ntp.conf

    # 1. 先处理权限方面的问题,包括放行上层服务器以及开放局域网用户来源:
    restrict default kod nomodify notrap nopeer noquery     <==拒绝 IPv4 的用户
    restrict -6 default kod nomodify notrap nopeer noquery  <==拒绝 IPv6 的用户
    restrict 220.130.158.71   <==放行 tock.stdtime.gov.tw 进入本 NTP 的服务器
    restrict 59.124.196.83    <==放行 tick.stdtime.gov.tw 进入本 NTP 的服务器
    restrict 59.124.196.84    <==放行 time.stdtime.gov.tw 进入本 NTP 的服务器
    restrict 127.0.0.1        <==底下两个是默认值,放行本机来源
    restrict -6 ::1
    restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行局域网用户来源,或者列出单独IP
    
    # 2. 设定主机来源,请先将原本的 [0|1|2].centos.pool.ntp.org 的设定批注掉:
    server 220.130.158.71 prefer  <==以这部主机为最优先的server
    server 59.124.196.83
    server 59.124.196.84
    
    # 3.默认的一个内部时钟数据,用在没有外部 NTP 服务器时,使用它为局域网用户提供服务:
    # server    127.127.1.0     # local clock
    # fudge     127.127.1.0 stratum 10
    
    # 4.预设时间差异分析档案与暂不用到的 keys 等,不需要更动它:
    driftfile /var/lib/ntp/drift
    keys      /etc/ntp/keys
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    文件路径:/etc/sysconfig/ntpd

    OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"
    SYNC_HWCLOCK=yes
    # 将他改成 yes 吧!这样 BIOS 的时间也会跟着改变的!
    • 1
    • 2
    • 3

    启动:

    /etc/init.d/ntpd start 或 /etc/init.d/ntpd restart
    • 1

    查看端口使用情况:

    netstat -tlunp | grep ntp
    • 1

    查看本机作为client更新time的过程:

    ntptrace
    • 1

    查看ntpd的运行信息:

    ntpstat
    • 1

    查看所有层次time service provider的详细信息:

    ntpq –pn
    • 1

    Client单独用ntpdate更新时间:

    ntpdate ntp_server_ip
    其后可能需要hwclock -w (clock -w)写入BIOS timer
    • 1
    • 2

    三.  可能的问题

    1、no server suitable for synchronization found

    原因:ntpd启动过程需要5分钟左右!(或者ntpd根本没有启动) 在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。

    那么如何知道何时ntp server完成了和自身同步的过程呢?

    在ntp server上使用命令:

    watch ntpq
    • 1

    出现画面: 这里写图片描述

    **注意**LOCAL的这个就是与自身同步的ntp server,而reach这个值,在启动ntp server服务后,就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。

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

    2、Server dropped: strata too high”的错误,并且显示“stratum 16”

    修改fudge server的stratum为0~15

    3、Server dropped: no data

    从客户端执行ntpdate –d server_ip(debug模式)时有错误信息: Server dropped: no data.

    出现这个问题的原因可能有2种: (1)   检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误(版本差异),解决方法是把notrust去掉。

    # ntpq -c version
    • 1

    (2) 检查ntp server的防火墙,可能是server的防火墙屏蔽了upd 123端口。可以用下面命令来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。

    #service iptables stop
  • 相关阅读:
    Java多线程系列目录(共43篇)
    Java 集合系列目录(Category)
    Floyd算法(三)之 Java详解
    Floyd算法(二)之 C++详解
    Floyd算法(一)之 C语言详解
    Dijkstra算法(三)之 Java详解
    Dijkstra算法(二)之 C++详解
    Dijkstra算法(一)之 C语言详解
    Prim算法(三)之 Java详解
    Prim算法(二)之 C++详解
  • 原文地址:https://www.cnblogs.com/gaoyuechen/p/7783634.html
Copyright © 2011-2022 走看看