zoukankan      html  css  js  c++  java
  • NTP for Linux

    一、服务概述

    NTP(network time  protocol 网络时间协议)服务器是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源做同步化,提供高精度的时间校正,在LAN上与标准时间差小于1毫秒,WAN相差几十毫秒,并且通过加密确认的方式来防止攻击。

    二、NTP的基本工作原理

    如图所示。Device A和Device B通过网络相连,它们都有自己独立的系统时钟,需要通过NTP实现各自系统时钟的自动同步。为便于理解,作如下假设:
    在Device A和Device B的系统时钟同步之前,Device A的时钟设定为10:00:00am,Device B的时钟设定11:00:00am。
    Device B作为NTP时间服务器,即Device A将使自己的时钟与Device B的时钟同步。
    NTP报文在Device A和Device B之间单向传输所需要的时间为1秒。

    系统时钟同步的工作过程如下:
    Device A发送一个NTP报文给Device B,该报文带有它离开Device A时的时间戳,该时间戳为10:00:00am(T1)。
    当此NTP报文到达Device B时,Device B加上自己的时间戳,该时间戳为11:00:01am(T2)。
    当此NTP报文离开Device B时,Device B再加上自己的时间戳,该时间戳为11:00:02am(T3)。
    当Device A接收到该响应报文时,Device A的本地时间为10:00:03am(T4)。
    至此,Device A已经拥有足够的信息来计算两个重要的参数:
    NTP报文的往返时延Delay=(T4-T1)-(T3-T2)=2秒。
    Device A相对Device B的时间差offset=((T2-T1)+(T3-T4))/2=1小时。
    这样,Device A就能够根据这些信息来设定自己的时钟,使之与Device B的时钟同步

    ###以上原理参考http://ntp.neu.edu.cn/archives/92  东北大学网络授时服务#####

    三、NTP服务的安装配置

    [root@localhost softs]# wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.6p5.tar.gz

    [root@localhost softs]# tar xf ntp-4.2.6p5.tar.gz -C /usr/src/
    [root@localhost softs]# cd /usr/src/ntp-4.2.6p5/

    [root@localhost ntp-4.2.6p5]# ./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks

    [root@localhost ntp-4.2.6p5]# make

    [root@localhost ntp-4.2.6p5]# make install

    解释一下相关的配置文件

    1、/etc/ntp.conf  这是ntp默认的主配置文件,不同的linux版本,可能改文件米目录不一样

    2、/usr/share/zoneinfo  规定了各个时区的配置文件,例如,上海

    [root@localhost Asia]# ls /usr/share/zoneinfo/Asia/Shanghai 
    Shanghai

    3、/etc/sysconfig/clock linux 主要时区的设定文件。每次启动后linux操作系统会自动读取这个文件来设定系统显示的时间。

    [root@localhost Asia]# cat /etc/sysconfig/clock 
    ZONE="Asia/Shanghai"     这里会使用上海这个文件的时间
     4、/etc/localtime  显示时间本地系统时间的设定文件,如果clock文件中规定了使用的时间设定文件为/usr/share/zoneinfo/Asia/Shanghai,linux系统会复制一份为/etc/localtim额,所以系统的时间显示就会以Shanghai这个时间为准。


    ntp服务默认的主配置文件

    /etc/ntp.conf

    [root@localhost bin]# cat /etc/ntp.conf  | grep -v ^# | grep -v ^$
    restrict default kod nomodify notrap nopeer noquery
    restrict -6 default kod nomodify notrap nopeer noquery
    restrict 127.0.0.1     -----------因为没有parameter部分,所以,该地址不受任何限制
    restrict -6 ::1
    server 0.rhel.pool.ntp.org--------上级时间服务器
    server 1.rhel.pool.ntp.org
    server 2.rhel.pool.ntp.org
    server  127.127.1.0     ----------- local clock
    fudge   127.127.1.0 stratum 10    -----fudge用户为这个时钟设置级别,对待内部系统时钟比较理想的情况是将时间源作为级别10来对待
    driftfile /var/lib/ntp/drift      -----------与上级时间服务器联系所花费的时间,记录在drift这个文件中
    keys /etc/ntp/keys

    权限的设定主要以 restrict 这个参数来设定,主要的语法为:restrict IP mask netmask_IP parameter

    其中 IP 可以是软件地址,也可以是 default ,default 就类似 0.0.0.0

    ignore: 关闭所有的 NTP 联机服务 
    nomodify: 表示 Client 端不能更改 Server 端的时间参数,不过, Client  端仍然可以透过 Server 端来进行网络校时。 
    notrust: Client 除非通过认证,否则该 Client 来源将被视为不信任网域 
    noquery: 不提供 Client 端的时间查询 ; 如果 paramter 完全没有设定,那就表示该 IP 没有任何限制!
    restrict default ignore  # 关闭所有的 NTP 要求封包
    restrict 127.0.0.1   # 开启内部递归网络接口 lo

    搭建一个内网的ntp服务器
    [root@localhost bin]# cp /etc/ntp.conf /usr/local/ntp/    因为我是源码安装的,并且制定目录在此,所以我手动创建一个配置文件
    [root@localhost bin]# vi /usr/local/ntp/ntp.conf 

    [root@localhost bin]# cd /usr/local/ntp/bin
    [root@localhost bin]# ./ntpd -c /usr/local/ntp/ntp.conf     -----------c制定配置文件启动
    [root@localhost bin]# lsof -i:123    --------查看ntpd的监听端口
    COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
    ntpd    5424 root   16u  IPv4  18575       UDP *:ntp 
    ntpd    5424 root   17u  IPv4  18577       UDP localhost.localdomain:ntp 
    ntpd    5424 root   18u  IPv4  18578       UDP 192.168.254.153:ntp 
    然后我到192.168.254.46上测试
    [root@localhost ~]# ntpdate 192.168.254.153   ----------以153作为ntp服务器进行时间的同步
    23 Oct 15:05:12 ntpdate[23303]: step time server 192.168.254.153 offset 48.683654 sec  成功

    这张图可以看得出效果

    相关的执行文件

    /bin/date       查询、修改日期时间的命令

    /sbin/hwclock   主机的BIOS时间与系统时间是分开的,所以使用date命令只是改的系统时间,要想将修改的时间写进bios必须还要root用户执行这个命令才可以。

    /usr/sbin/ntpd  ntp服务的守护进程

    /usr/sbin/ntpdate  ntp客户端用来连接ntp服务器的命令文件

    /usr/sbin/ntpq  标准网络计时协议查询程序

    /usr/sbin/ntptrace 跟踪网络计时协议主机链到它们的控制时间源

    /sbin/clock 调整电脑硬件时间RTC,执行这个命令可以显示现在时刻。将硬件时间与系统时间调成一致。

    接下来再用一下hwclock命令

    下一个命令ntpq
    [root@localhost bin]# ntpq -p  列出当前NTP服务器上的ntp状态
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
    *LOCAL(0)        .LOCL.          10 l   62   64  377    0.000    0.000   0.001

    remote  响应这个请求的ntp服务器的名称;  refid NTP服务器使用的上一级NTP服务器;   stremote服务器的级别;when 上次请求成功到现在的秒数;poll本地机和远程机多久同步一次;reach测试能否跟服务器连接;delay从本地鸡发送同步请求道ntp服务器的round trip time;offset主机与时间源同步的时候的时间偏移量(ms);jitter统计值

    ntptrace  ----查看ntp与上层服务器的关系,下面表示与第十层已经连上了

    [root@localhost bin]# ntptrace -n 127.0.0.1
    127.0.0.1: stratum 11, offset 0.000000, synch distance 0.000000

    顺便介绍一下windows客户端的时间同步

    win +R 打开运行,输入cmd

    在命令行输入“net stop w32time”,在输入"net start w32time",最后输入net time /setsntp:"192.168.254.153"


    提示:其实NTP服务的配置非常简单,好像貌似也没什么用,但是如果你是在玩集群的话,那你可要重视了,呵呵 !


  • 相关阅读:
    奇数阶魔方问题
    《DSP using MATLAB》示例9.3
    《DSP using MATLAB》示例9.2
    《DSP using MATLAB》示例9.1
    找个目标很重要
    《DSP using MATLAB》示例Example 8.30
    《DSP using MATLAB》示例Example 8.29
    《DSP using MATLAB》示例Example 8.28
    《DSP using MATLAB》示例Example 8.27
    《DSP using MATLAB》示例Example 8.26
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3397955.html
Copyright © 2011-2022 走看看