zoukankan      html  css  js  c++  java
  • NTP服务

    一、什么是NTP

    NTP(network time protocol),网络时间协议是用来校准计算机时间同步化的一种协议。它可以使计算机对其服务器或者时钟源同步化,可以提供高精准度的时间校正(lan网标准差小于1ms,wan网标准差小于几十ms),且可以由加密确认的方式来防止恶意的协议攻击。端口为 123

    二、安装ntp

    yum install ntp -y
    

    三、常用命令

    systemctl start ntpd  #启动ntp服务
    systemctl enable ntpd #设置ntp服务开机自启
    systemctl list-unit-files|grep ntpd #查看ntp服务是否已经设置开机自启
    
    [root@localhost ~]# ntpdate 0.centos.pool.ntp.org  #指定某时间同步服务器,进行时间同步
     4 Mar 15:21:42 ntpdate[10378]: adjust time server 84.16.67.12 offset -0.005641 sec
    
    

    四、与远程服务器进行时间同步

    4.1、配置文件简介
    [root@localhost ~]# vim /etc/ntp.conf 
    driftfile /var/lib/ntp/drift #系统时间与BIOS事件的偏差记录
    restrict default nomodify notrap nopeer noquery  #拒绝所有服务器连接到此服务器所有权限。
        nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
        notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
        noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
        notrap :不提供trap远端登陆。
        nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
    restrict 10.10.10.0 mask 255.255.255.0  #允许ip网段为 10.10.10的机器用本台服务器进行时间同步
    
    server cn.ntp.org.cn  iburst
    server edu.ntp.org.cn iburst
    server ntp1.aliyun.com iburst
    server ntp2.aliyun.com iburst
    server 127.127.1.0 stratum 10
    #配置时间同步服务器,本台服务器跟远程的服务器进行时间同步,我这里配置了五个,iburst 如果在一个标准的轮询间隔内没有应答,客户端会发送一定数量的包(八个包而不是通常的一个)给 NTP 服务器。
    
    server 127.127.1.0 stratum 10   #如果上面的时间同步服务器不通,则用本机自己的时间,可指定层级
    
    4.2、开始同步
    [root@localhost ~]# systemctl restart ntpd
    [root@localhost ~]# ntpstat  
    synchronised to local net (127.127.1.0) at stratum 6
       time correct to within 7948 ms
       polling server every 64 s
    #我们此时发现该服务器并没有与aliyun时间服务器进行同步,而是直接把本地时间进行同步。此时没啥问题,需要等待几分钟就好了,实在不行,我们可以使用 ntpdate 进行手动同步。
    
    4.3、常用命令
    [root@localhost ~]# ntpstat  #连接信息
    synchronised to NTP server (202.118.1.130) at stratum 2   #已经与 202.118.1.130 进行时间同步,该服务器位于第二层,一般时间同步服务器共16层。此时我们自己的机器位于第三层。
       time correct to within 228 ms  #更新所需时间
       polling server every 64 s    #每64s同步一次
    
    
    [root@localhost ~]#  ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
    +120.25.115.20   10.137.53.7      2 u   62   64   27  132.669  -27.653 124.418
    *202.118.1.130   .PTP.            1 u   60   64   37  286.663  -73.993 198.148
     203.107.6.88    10.137.38.86     2 u   15   64   11  458.916   -5.581 149.835
     LOCAL(0)        .LOCL.           5 l  279   64   60    0.000    0.000   0.000
    remote #远程服务器地址
    refid  #远程服务器的上层服务器
    st     #层级
    t      #不太懂
    when   #还有多久进行同步
    poll   #每64 秒更新一次
    delay  # 不记得了
    offset #与远程服务器的时差,
    

    五、与本地时间服务器进行同步

    因为在10.10.10.20 已经配置好了相关设置,我们可以用10.10.10.20 进行时间同步

    [root@localhost ~]# ntpdate 10.10.10.20
     6 Mar 12:22:07 ntpdate[7403]: adjust time server 10.10.10.20 offset 0.045833 sec
    
    5.1、使用定时任务进行自动同步
    [root@localhost ~]# vim /etc/cronta
    */5 *  *  *  *  ntpdate 10.10.10.20    #每五分钟进行一次同步
    
    5.2、启动ntp服务自动同步
    [root@localhost ~]# vim /etc/ntp.conf  #将默认的服务器删除,添加自己的服务器
    server 10.10.10.20 iburst
    
    
    [root@localhost ~]#systemctl start ntpd
    
    [root@localhost ~]# ntpstat
    synchronised to NTP server (10.10.10.20) at stratum 3
       time correct to within 330 ms
       polling server every 64 s
    
    [root@localhost ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
    *10.10.10.20     202.118.1.130    2 u   42   64    1    0.485  -42.469   0.340
    
    
  • 相关阅读:
    Mac安装zookeeper
    征途
    vue-配置文件
    数组去重
    判断身份证
    判断邮箱
    判断手机号是否正确
    JS 时间格式转换
    打印 print-js
    自"愚"自乐的云服务器
  • 原文地址:https://www.cnblogs.com/hjnzs/p/12425882.html
Copyright © 2011-2022 走看看