设为首页 收藏本站
查看: 845|回复: 0

[经验分享] centos7 搭建ntp服务器

[复制链接]

尚未签到

发表于 2018-4-25 06:33:13 | 显示全部楼层 |阅读模式
  一、安装ntp服务

  首先检查服务器上是否已安装ntp服务:
  [root@centos7 /]rpm -qa | grep ntp   

  ntp-4.2.6p5-22.el7.centos.2.x86_64   
  ntpdate-4.2.6p5-22.el7.centos.2.x86_64
  如果有上述内容输出,测说明ntp服务已安装;否则需要安装ntp服务
  [root@centos7 /]yum install -y ntpd
  安装有yum、rpm、源码安装等多种方式,本次安装使用yum安装。如果服务器能联网,建议采用yum安装。
  二、配置ntp
  采用yum或rpm安装情况下,ntp的配置文件路径为/etc/ntp.conf
  1、首先找到互联网上提供时间时间同步的ntp server
  http://www.pool.ntp.org是NTP的官方网站,在这上面我们可以找到离我们城市最近的NTP Server. NTP建议我们为了保障时间的准确性,最少找两个个NTP Server。
  比如在中国可以选择下面几台服务器

  
       server 1.cn.pool.ntp.org
   server 0.asia.pool.ntp.org
   server 2.asia.pool.ntp.org  2、在配置之前最好先手动和上面的服务器同步一次时间,使服务器时间尽量接近标准时间

  [root@centos7 /]ntpdate 1.cn.pool.ntp.org
  假如你的时间差的很离谱的话第一次会看到调整的幅度比较大,所以保险起见可以运行两次。 那么为什么在打开NTP服务之前先要手动运行同步呢?
1. 因为根据NTP的设置,如果你的系统时间比正确时间要快的话那
么NTP是不会帮你调整的,所以要么你把时间设置回去,要么先做一个手动同步
  2. 当你的时间设置和NTP服务器的时间相差很大的时候,NTP会花上较长一段时间进行调整.所以手动同步可以减少这段时间
  3、配置/etc/ntp.conf(红色部分为修改部分)
  # For more information about this file, see the man pages
  # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
  

  driftfile /var/lib/ntp/drift
  

  # Permit time synchronization with our time source, but do not
  # permit the source to query or modify the service on this system.
  restrict default nomodify notrap nopeer noquery
  

  # Permit all access over the loopback interface.  This could
  # be tightened as well, but to do so would effect some of
  # the administrative functions.
  restrict 127.0.0.1
  restrict ::1
  

  # Hosts on local network are less restricted.
  #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
  restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap#允许特定网段能同步时间
  

  # 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 1.cn.pool.ntp.org  #向该服务器同步时间,下同
  server 0.asia.pool.ntp.org
  server 2.asia.pool.ntp.org
  

  #broadcast 192.168.1.255 autokey# broadcast server
  #broadcastclient# broadcast client
  #broadcast 224.0.1.1 autokey# multicast server
  #multicastclient 224.0.1.1# multicast client
  #manycastserver 239.255.254.254# manycast server
  #manycastclient 239.255.254.254 autokey # manycast client
  ####restrict参数####
  #kod 使用kod技术防范“kiss of death”攻击
  #ignore 拒绝任何NTP连接
  #nomodify 用户端不能使用ntpc,ntpq修改时间服务器参数,但是可以进行网络校时
  #noquery 用户端不能使用ntpc,ntpq查询时间服务器参数,不可以进行网络校时
  #notrap 不提供远程日志功能
  #notrust 拒绝没有认证的客户端
  restrict 1.cn.pool.ntp.org nomodify notrap noquery
  restrict 0.asia.pool.ntp.org nomodify notrap noquery
  restrict 2.asia.pool.ntp.org nomodify notrap noquery
  

  server 127.0.0.1
  fudge 127.127.1.0 stratum 5
  #设置本地时间级别是5,如果上级时间服务器均失效,对外发布本地时间,时间层级是4。
  #这行是时间服务器的层次。设为0则为顶级,如果要向别的NTP服务器更新时间,请不要把它设为0
  # Enable public key cryptography.
  #crypto
  

  includefile /etc/ntp/crypto/pw
  

  # Key file containing the keys and key identifiers used when operating
  # with symmetric key cryptography.
  keys /etc/ntp/keys
  

  # Specify the key identifiers which are trusted.
  #trustedkey 4 8 42
  

  # Specify the key identifier to use with the ntpdc utility.
  #requestkey 8
  

  # Specify the key identifier to use with the ntpq utility.
  #controlkey 8
  

  # Enable writing of statistics records.
  #statistics clockstats cryptostats loopstats peerstats
  

  # Disable the monitoring facility to prevent amplification attacks using ntpdc
  # monlist command when default restrict does not include the noquery flag. See
  # CVE-2013-5211 for more details.
  # Note: Monitoring will not be disabled with the limited restriction flag.
  disable monitor
  

  配置好之后,启动ntp服务
  [root@centos7 /]systemctl start ntpd
  [root@centos7 /]ntpq -p #查询运行情况
DSC0000.png

更多细节
表头


  •   remote – 用于同步的远程节点或服务器。“LOCAL”表示本机 (当没有远程服务器可用时会出现)
        refid – 远程的服务器进行同步的更高一级服务器
        st – 远程节点或服务器的 Stratum(级别,NTP 时间同步是分层的)
        t – 类型 (u: unicast(单播) 或 manycast(选播) 客户端, b: broadcast(广播) 或 multicast(多播) 客户端, l: 本地时钟, s: 对称节点(用于备份), A: 选播服务器, B: 广播服务器, M: 多播服务器, 参见“Automatic Server Discovery“)
        when – 最后一次同步到现在的时间 (默认单位为秒, “h”表示小时,“d”表示天)
        poll – 同步的频率:rfc5905建议在 NTPv4 中这个值的范围在 4 (16秒) 至 17 (36小时) 之间(即2的指数次秒),然而观察发现这个值的实际大小在一个小的多的范围内 :64 (26 )秒 至 1024 (210 )秒
        reach – 一个8位的左移移位寄存器值,用来测试能否和服务器连接,每成功连接一次它的值就会增加,以 8 进制显示
        delay – 从本地到远程节点或服务器通信的往返时间(毫秒)
        offset – 主机与远程节点或服务器时间源的时间偏移量,offset 越接近于0,主机和 NTP 服务器的时间越接近(以方均根表示,单位为毫秒)
        jitter – 与远程节点同步的时间源的平均偏差(多个时间样本中的 offset 的偏差,单位是毫秒),这个数值的绝对值越小,主机的时间就越精确
  

  [root@centos7 /]ntpstat #查看是否同步状态,下图可以看出同步成功
DSC0001.png

  

如需其它服务器从本机同步时间,还需将ntp服务开放
[root@centos7 /]firewall-cmd --zone=public --add-service=ntp --permanent
[root@centos7 /]firewall-cmd --reload
  最后设置开机启动
  [root@centos7 /]systemctl enable ntpd
  

参考资料:http://www.centoscn.com/CentOS/config/2015/1105/6385.html
http://blog.csdn.net/iloli/article/details/6431757  

  

  

  

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-451531-1-1.html 上篇帖子: CentOS7系统——systemd特性 下篇帖子: centos内存手动清理缓存
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表