第1章 内网NTP服务搭建

1.1 NTP简介

NTP(网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms
NTP服务器就是利用NTP协议提供时间同步服务的
NTP服务器通信采用UDP协议,端口是123

1.2 NTP服务安装

  1. #系统自带ntp
  2. [root@oldboyedu ~]# rpm -qa ntp
  3. ntp-4.2.6p5-5.el6.centos.x86_64
  4. #如果没有就安装
  5. yum -y install ntp
  6. #启动ntp,并设置开机自启动
  7. /etc/init.d/ntpd start
  8. chkconfig ntpd on
  9. 192.168.89.11是服务端
  10. 192.168.89.10是客户端

1.3 NTP服务端主配置

  1. [root@mysql ~]# cat /etc/ntp.conf
  2. # For more information about this file, see the man pages
  3. # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
  4. driftfile /var/lib/ntp/drift
  5. # Permit time synchronization with our time source, but do not
  6. # permit the source to query or modify the service on this system.
  7. #restrict default nomodify notrap nopeer noquery
  8. restrict default nomodify
  9. # Permit all access over the loopback interface. This could
  10. # be tightened as well, but to do so would effect some of
  11. # the administrative functions.
  12. restrict 127.0.0.1
  13. restrict ::1
  14. # Hosts on local network are less restricted.
  15. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
  16. # Use public servers from the pool.ntp.org project.
  17. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  18. server 0.centos.pool.ntp.org iburst
  19. server 1.centos.pool.ntp.org iburst
  20. server 2.centos.pool.ntp.org iburst
  21. server 3.centos.pool.ntp.org iburst
  22. server 127.127.1.0
  23. fudge 127.127.1.0 stratum 10
  24. #broadcast 192.168.1.255 autokey # broadcast server
  25. #broadcastclient # broadcast client
  26. #broadcast 224.0.1.1 autokey # multicast server
  27. #multicastclient 224.0.1.1 # multicast client
  28. #manycastserver 239.255.254.254 # manycast server
  29. #manycastclient 239.255.254.254 autokey # manycast client
  30. # Enable public key cryptography.
  31. #crypto
  32. includefile /etc/ntp/crypto/pw
  33. # Key file containing the keys and key identifiers used when operating
  34. # with symmetric key cryptography.
  35. keys /etc/ntp/keys
  36. # Specify the key identifiers which are trusted.
  37. #trustedkey 4 8 42
  38. # Specify the key identifier to use with the ntpdc utility.
  39. #requestkey 8
  40. # Specify the key identifier to use with the ntpq utility.
  41. #controlkey 8
  42. # Enable writing of statistics records.
  43. #statistics clockstats cryptostats loopstats peerstats
  44. # Disable the monitoring facility to prevent amplification attacks using ntpdc
  45. # monlist command when default restrict does not include the noquery flag. See
  46. # CVE-2013-5211 for more details.
  47. # Note: Monitoring will not be disabled with the limited restriction flag.
  48. disable monitor
  49. ############################################################
  50. #配置文件详解
  51. [root@mysql ~]# cat /etc/ntp.conf
  52. # For more information about this file, see the man pages
  53. # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
  54. driftfile /var/lib/ntp/drift
  55. # Permit time synchronization with our time source, but do not
  56. # permit the source to query or modify the service on this system.
  57. #restrict default nomodify notrap nopeer noquery
  58. #restrict 控制相关权限:
  59. #ignore:关闭所有的NTP联机服务
  60. #nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时
  61. #notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网
  62. #noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
  63. #notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
  64. #nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
  65. restrict default nomodify
  66. # Permit all access over the loopback interface. This could
  67. # be tightened as well, but to do so would effect some of
  68. # the administrative functions.
  69. #确保localhost(这个常用的IP地址用来指linux服务器本身)有足够权限,使用没有任何限制关键词的语法
  70. restrict 127.0.0.1
  71. restrict ::1
  72. # Hosts on local network are less restricted.
  73. #限制你允许的这些服务器的访问类型,在这个列子中的服务器是不容许修改运行时配置或查询您的linux NTP服务器,但是可以时间同步,详情请查看前面的(restrict 控制相关权限)
  74. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
  75. # Use public servers from the pool.ntp.org project.
  76. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  77. #设定NTP主机来源,127.127.1.0 是指已本地的服务器为NTP服务器
  78. server 0.centos.pool.ntp.org iburst
  79. server 1.centos.pool.ntp.org iburst
  80. server 2.centos.pool.ntp.org iburst
  81. server 3.centos.pool.ntp.org iburst
  82. server 127.127.1.0
  83. fudge 127.127.1.0 stratum 10
  84. #broadcast 192.168.1.255 autokey # broadcast server
  85. #broadcastclient # broadcast client
  86. #broadcast 224.0.1.1 autokey # multicast server
  87. #multicastclient 224.0.1.1 # multicast client
  88. #manycastserver 239.255.254.254 # manycast server
  89. #manycastclient 239.255.254.254 autokey # manycast client
  90. # Enable public key cryptography.
  91. #crypto
  92. includefile /etc/ntp/crypto/pw
  93. # Key file containing the keys and key identifiers used when operating
  94. # with symmetric key cryptography.
  95. keys /etc/ntp/keys
  96. # Specify the key identifiers which are trusted.
  97. #trustedkey 4 8 42
  98. # Specify the key identifier to use with the ntpdc utility.
  99. #requestkey 8
  100. # Specify the key identifier to use with the ntpq utility.
  101. #controlkey 8
  102. # Enable writing of statistics records.
  103. #statistics clockstats cryptostats loopstats peerstats
  104. # Disable the monitoring facility to prevent amplification attacks using ntpdc
  105. # monlist command when default restrict does not include the noquery flag. See
  106. # CVE-2013-5211 for more details.
  107. # Note: Monitoring will not be disabled with the limited restriction flag.
  108. disable monitor

1.4 重启NTP服务

  1. /etc/init.d/ntpd restart

1.5 检查时间服务器是否正确同步

  1. [root@mysql ~]# ntpq -p
  2. remote refid st t when poll reach delay offset jitter
  3. ==============================================================================
  4. +makaki.miuku.ne 218.186.3.36 2 u 47 64 1 79.040 4651437 18.347
  5. +ntp8.flashdance 192.36.143.151 2 u 46 64 1 321.483 4651438 1.101
  6. -119.79-161-57.c 129.242.4.241 2 u 44 64 1 342.352 4651439 1.897
  7. *static-5-103-13 .GPS. 1 u 44 64 1 280.109 4651438 1.134
  8. LOCAL(0) .LOCL. 10 l 53 64 1 0.000 0.000 0.000

1.6 客户端设置时间同步

  1. #安装ntp
  2. yum install ntp -y
  3. #定时任务,同步时间服务器
  4. crontab -l
  5. #ntpdate
  6. */5 * * * * /usr/sbin/ntpdate 192.168.89.11

第2章 公网搭建NTP服务器

2.1 NTP服务安装

  1. #系统自带ntp
  2. [root@oldboyedu ~]# rpm -qa ntp
  3. ntp-4.2.6p5-5.el6.centos.x86_64
  4. #如果没有就安装
  5. yum -y install ntp
  6. #启动ntp,并设置开机自启动
  7. /etc/init.d/ntpd start
  8. chkconfig ntpd on
  9. 192.168.89.11是服务端
  10. 192.168.89.10是客户端

2.2 配置NTP服务

  1. [root@oldboyedu ~]# vim /etc/ntp.conf
  2. # restrict default kod nomodify notrap nopeer noquery
  3. restrict default nomodify
  4. # nomodify客户端可以同步
  5. # 将默认时间同步源注释改用可用源
  6. # server 0.centos.pool.ntp.org iburst
  7. # server 1.centos.pool.ntp.org iburst
  8. # server 2.centos.pool.ntp.org iburst
  9. # server 3.centos.pool.ntp.org iburst
  10. server ntp1.aliyun.com
  11. server time.nist.gov

2.3 启动NTP服务器

  1. # 如果计划任务有时间同步,先注释,两种用法会冲突。
  2. [root@oldboyedu ~]# crontab -e
  3. # time sync by oldboy at 2010-2-1
  4. #*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
  5. [root@oldboyedu ~]# /etc/init.d/ntpd start
  6. Starting ntpd: [ OK ]
  7. [root@oldboyedu ~]# ntpq -p
  8. remote refid st t when poll reach delay offset jitter
  9. ==============================================================================
  10. *ntp1.aliyun.com 10.137.38.86 2 u 22 64 1 525.885 -42.367 0.000
  11. [root@oldboyedu ~]# ntpstat
  12. synchronised to NTP server (110.75.186.247) at stratum 3
  13. time correct to within 4257 ms
  14. polling server every 64 s
  15. [root@oldboyedu ~]# ntpdate 10.0.0.9
  16. 7 Dec 18:43:07 ntpdate[26950]: the NTP socket is in use, exiting

2.4 客户端同步

  1. #安装ntp
  2. yum install ntp -y
  3. #定时任务,同步时间服务器
  4. [root@oldboyedu ~]# crontab -l
  5. #ntpdate
  6. * * * * * /usr/sbin/ntpdate 192.168.89.11