zoukankan      html  css  js  c++  java
  • Linux 6.2 配置NTP服务

    Linux自带了ntp服务 -- /etc/init.d/ntpd,这个服务不仅可以设置让本机和某台/某些机器做时间同步,他本身还可以扮演一个time 的角色,让其他机器和他同步时间,配置文件就是/etc/ntp.conf。
      现有4台机器,让217做时间服务器,让218、219、200与217进行同步,配置如下:
      第一步:
      [root@eytdb etc]# rpm -qa|grep ntp  --是否已经安装了这个软件包:
      [root@eytdb etc]# ntpstat  --NTP服务状态查询
      217做time server,217本身不和其他机器时间同步,就是取本地时间,先把217机器的时间调准了:
      [root@eytdb etc]# date --set "10/26/2011 16:05:00"
      [root@eytdb etc]# clock -w
      [root@eytdb etc]# hwclock --systohc
      后两个命令是把设置的时间写到硬件时间中去(也就是CMOS里面的时间)。
      第二步:
      然后将217配置成一个time server,修改/etc/ntp.conf,
      [root@eytdb etc]# cp ntp.conf ntp-back.conf   --先备份
    
      [root@eytdb etc]# vi /etc/ntp.conf
      其他的配置不怎么需要改,只需要关注restrict的配置:
      1. 注释掉下面2行,意思是不响应任何的ntp更新请求,其实也就是禁用了本机的ntp server的功能,所以需要注释掉。
      # restrict default kod nomodify notrap nopeer noquery    --对于默认的client拒绝所有的操作
      # restrict -6 default kod nomodify notrap nopeer noquery
    
    
      2. 加入下面3行 -- 让x.x.x.218---200网段上的机器能和本机做时间同步
    
      restrict x.x.x.218 mask 255.255.255.0 nomodify notrap    ---x.x.x.218 为需要同步的机器IP地址
      restrict x.x.x.219 mask 255.255.255.0 nomodify notrap
      restrict x.x.x.200 mask 255.255.255.0 nomodify notrap
    
       restrict 10.130.130.0 mask 255.255.255.0 nomodify notrap  ---允许10.130.130.0网段和本NTP SERVER进行实践校对
    ~                                                          
      --说明:ignore 关闭所有的NTP服务 ,  nomodify 客户端不能修改时间服务器的参数,只能进行时间校对
      notrap 不提供trap远程事件登陆的功能,notrust 拒绝所有通过认证的客服端,
      kod  kod技术可以阻止Kiss Of Death 包对服务器的破坏,使用此参数将开启该功能。
      nopeer  不与同一层的NTP服务器进行时间的同步
    
      3. 把下面2行的注释去掉,这是让本机的ntpd和本地硬件时间同步:
      server 127.127.1.0 # local clock
      fudge 127.127.1.0 stratum 10
      当然,也可以添加server xxx.xxx.xxx.xxx,让他和其他的time server时间同步。
    
      4. 重启服务/etc/init.d/ntpd restart
      或者:启动/停止/重启动NTP服务的命令:
      service ntpd start |stop|restart
    
      5. NTP服务开机自动启动的配置及检查命令:
      chkconfig ntpd on    --在运行级别2、3、4、5上设置为自动运行
      或 chkconfig --level 35 ntpd on  --在运行级别3、5上设置为自动运行
      (打开自动启动用“on”;去掉自动启动用“off”)
      # watch ntpq -p   --查看
      # chkconfig  --list  --检查
      ntpd        0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
      ntpdate    0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:关闭  6:关闭
    
      6. 修改iptables配置,将tcp和udp 123端口开放,这是ntp需要的端口,在/etc/services中可以查到这个端口。
      --打开端口
      [root@eytdb etc]# iptables -I INPUT -p udp --dport 123 -j ACCEPT
      这样217就成为一台time server了。
      可以使用下面的命令来检查NTP 服务是否启动,可以得到一个进程ID号:
      [root@eytdb etc]# pgrep ntpd
      26957
      26959
      使用下面的命令检查时间服务器同步的状态:
      [root@eytdb etc]# ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
      ==============================================================================
      *LOCAL(0)        .LOCL.          10 l   46   64  377    0.000    0.000   0.000
    
      
    第三步配置ntp客户端:
      配置其它几台机器,也是修改/etc/ntp.conf ,
    
      [root@eytweb ~]# vi /etc/ntp.conf
      1. 注释掉server 127.127.1.0, fudge 127.127.1.0 stratum 10这两行,因为这台机器不需要和本地硬件时钟同步了。
      2. 加入server x.x.x.217这行,和217机器同步。
      
    配置完成,看时间已经和217同步了。往后默认配置好像是5分钟和time server同步一次。ntpdate命令是显式的和某台机器做时间同步。
      
        [root@eytweb ~]# ntpq -p     --检查与217机器是否同步
      remote           refid      st t when poll reach   delay   offset  jitter
      ==============================================================================
      *x.x.x.217      .INIT.          16 u   39  128  376    0.150    0.057   0.017
      
    第四步,将ntpdate放到crontab中定期步也是可以的
      [root@eytweb ~]#vi ntpupdate.sh
    
    /usr/sbin/ntpdate x.x.x.217
    
    
    ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个:  
    
    错误1.Server dropped: Strata too high
    
    在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。
    
    在ntp客户端用ntpdate -d serverIP查看,发现有“Server dropped: strata too high”的错误,
    并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。
    
    这是因为NTP server还没有和其自身或者它的server同步上。
    
    以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。
    
    server 127.127.1.0 fudge
    
    127.127.1.0 stratum 8 
    
     
    
    在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,
    这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
    
    
    [root@czcb_node2 ~]# /usr/sbin/ntpdate 10.5.128.240
    26 Mar 21:53:27 ntpdate[7170]: step time server 10.5.128.240 offset -0.675455 sec
    
    [root@agent1 lib]# ntpq -p
    ntpq: read: Connection refused
    
    客户端也需要启动NTP服务

  • 相关阅读:
    iOS开发之WIFI,3G/4G两种网络同时使用技巧
    UIScreen的scale属性
    iOS开发中的Self-Manager 模式
    iOS UICollectionView简单使用
    Size Classes with Xcode 6
    SizeClass介绍
    AutoLayout处理UITableView动态高度
    在Scrollview中使用AutoLayout
    hashmap与hashtable的本质区别
    MySQL的5大引擎及优劣之分
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13352082.html
Copyright © 2011-2022 走看看