• ntp时间同步服务


    前言

    NTP 网络时间协议用来同步网络上不同主机的系统时间。你管理的所有主机都可以和一个指定的被称为 NTP 服务器的时间服务器同步它们的时间。而另一方面,一个 NTP 服务器会将它的时间和任意公共 NTP 服务器,或者你选定的服务器同步。由 NTP 管理的所有系统时钟都会同步精确到毫秒级。
    在公司环境中,如果他们不想为 NTP 传输打开防火墙,就有必要设置一个内部 NTP 服务器,然后让员工使用内部服务器而不是公共 NTP 服务器
    服务端 : 192.168.16.30 master01
    客户端:  192.168.16.31 master02

    1、 服务端配置:

    centos7下首先确认服务器的防火墙、selinux关闭状态

    [root@master01 ~]# cat /etc/redhat-release
    CentOS Linux release 7.2.1511 (Core) 
    

      

    1.1为服务器和客户机安装ntp

    yum install ntp -y
    

    1.2 查找时间同步服务器

    http://www.pool.ntp.org/zone/asia

    1.3  编辑 /etc/ntp.conf

    cat >/etc/ntp.conf<<EOF
    server 210.72.145.44   #China Time Centerserver
    server cn.pool.ntp.org   #Pulbic Time Server
    server 127.127.1.0 iburst local   #当外部时间不可用时,使用本地时间
    clockrestrict 192.168.16.0 mask 255.255.255.0 nomodify  #允许更新的IP地址段
    EOF

      

      配置说明:
      ignore :关闭所有的 NTP 联机服务
      nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
      notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
      noquery :不提供客户端的时间查询
      注意:如果参数没有设定,那就表示该 IP (或子网)没有任何限制!

    1.4  启动ntp服务

    systemctl start ntpd
    systemctl enable ntpd.service 
    

      

    1.5验证服务

    [root@master01 ~]# ntpq -p 
    remote refid st t when poll reach delay offset jitter
    ============================================================================= 
    210.72.145.44 .INIT. 16 u - 1024 0 0.000 0.000 0.000
    *85.199.214.100 .GPS. 1 u 18 64 377 250.594 -0.486 0.412 
    LOCAL(0) .LOCL. 5 l 6h 64 0 0.000 0.000 0.000
    
    [root@master01 ~]# date -R
    Thu, 31 May 2018 16:38:20 +0800
    
    [root@master01 ~]# ntpdc -np 
    remote local st poll reach delay offset disp
    =======================================================================
    *85.199.214.100 192.168.16.30 1 64 377 0.25058 -0.000486 0.04756
    =127.0.0.1 5 64 0 0.00000 0.000000 3.99217
    =192.168.16.30 16 1024 0 0.00000 0.000000 3.99217Fri, 15 Jul 2016 15:28:34 +0800
    

     

    参数说明: 
    remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
    refid - 参考上一层ntp主机地址
    st - stratum阶层
    when - 多少秒前曾经同步过时间
    poll - 下次更新在多少秒后
    reach - 已经向上层ntp服务器要求更新的次数
    delay - 网络延迟
    offset - 时间补偿
    jitter - 系统时间与bios时间差
    

      

    2、客户端

    2.1客户端配置ntp:(注释并在后面添加同步服务器)

    [root@master02 ~]# vim /etc/ntp.conf
    
    .....
    # Use public servers from the pool.ntp.org project.
    # Please consider joining the pool (http://www.pool.ntp.org/join.html).
    #server 0.centos.pool.ntp.org iburst
    #server 1.centos.pool.ntp.org iburst
    #server 2.centos.pool.ntp.org iburst
    #server 3.centos.pool.ntp.org iburst
    server master01
    ......

      


    2.2  远程客户端时间同步测试

    [root@master02 ~]# ntpdate master01
    
    2 Jun 11:30:01 ntpdate[1235]: step time server 192.168.16.30 offset 2.766992 sec
    
    [root@master02 ~]# systemctl start ntpd
    
    [root@master02 ~]# systemctl enable ntpd.service
    
    [root@master02 ~]# ntpdc -np
    
    remote local st poll reach delay offset disp
    =======================================================================
    *192.168.16.30 192.168.16.31 2 128 377 0.00018 -0.003627 0.09908
    
    

    ntpd、ntpdate的区别

    tpd与ntpdate在更新时间时有什么区别。

      ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。
    时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:
      第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。
      第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
      第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。
    NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

  • 相关阅读:
    svn TortoiseSVN 回滚版本
    侵入式菜单
    Android 布局开发之百分比布局、弹性布局
    webstorm 使用svn
    bootstarp
    Retrofit get post query filed FiledMap
    http和https
    深入理解乐观锁与悲观锁
    数据库的锁机制
    数据库读现象浅析
  • 原文地址:https://www.cnblogs.com/miclis/p/9117952.html
Copyright © 2020-2023  润新知