1、NTP简介

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

  1. [root@test ~]# cat /etc/redhat-release
  2. CentOS release 6.9 (Final)

2、NTP服务器安装

  1. [root@test ~]# rpm -qa ntp
  2. ntp-4.2.6p5-10.el6.centos.2.x86_64

如果没有安装就yum install ntp -y进行安装

3、配置NTP服务

备份配置文件

  1. [root@test ~]# cp /etc/ntp.conf{,.bak}
  2. [root@test ~]# ll /etc/ntp.conf*
  3. -rw-r--r--. 1 root root 1778 Jan 11 2017 /etc/ntp.conf
  4. -rw-r--r-- 1 root root 1778 Oct 23 23:10 /etc/ntp.conf.bak

精简化配置文件

  1. [root@test ~]# egrep -v "^$|#" /etc/ntp.conf.bak >/etc/ntp.conf

修改配置文件

  1. # For more information about this file, see the man pages
  2. # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
  3. driftfile /var/lib/ntp/drift
  4. # Permit time synchronization with our time source, but do not
  5. # permit the source to query or modify the service on this system.
  6. restrict default kod nomodify notrap nopeer noquery
  7. restrict -6 default kod nomodify notrap nopeer noquery
  8. # Permit all access over the loopback interface. This could
  9. # be tightened as well, but to do so would effect some of
  10. # the administrative functions.
  11. restrict 127.0.0.1
  12. restrict -6 ::1
  13. # Hosts on local network are less restricted.
  14. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
  15. # 允许内网其他机器同步时间
  16. restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap
  17. # Use public servers from the pool.ntp.org project.
  18. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  19. #server 0.centos.pool.ntp.org iburst
  20. #server 1.centos.pool.ntp.org iburst
  21. #server 2.centos.pool.ntp.org iburst
  22. #server 3.centos.pool.ntp.org iburst
  23. # 定义使用的上游 ntp服务器,将原来的注释
  24. server time1.aliyun.com
  25. server ntp1.aliyun.com
  26. #broadcast 192.168.1.255 autokey # broadcast server
  27. #broadcastclient # broadcast client
  28. #broadcast 224.0.1.1 autokey # multicast server
  29. #multicastclient 224.0.1.1 # multicast client
  30. #manycastserver 239.255.254.254 # manycast server
  31. #manycastclient 239.255.254.254 autokey # manycast client
  32. # 允许上层时间服务器主动修改本机时间
  33. restrict time1.aliyun.com nomodify notrap noquery
  34. restrict ntp1.aliyun.com nomodify notrap noquery
  35. # 外部时间服务器不可用时,以本地时间作为时间服务
  36. server 127.127.1.0
  37. fudge 127.127.1.0 stratum 10
  38. # Enable public key cryptography.
  39. #crypto
  40. includefile /etc/ntp/crypto/pw
  41. # Key file containing the keys and key identifiers used when operating
  42. # with symmetric key cryptography.
  43. keys /etc/ntp/keys
  44. # Specify the key identifiers which are trusted.
  45. #trustedkey 4 8 42
  46. # Specify the key identifier to use with the ntpdc utility.
  47. #requestkey 8
  48. # Specify the key identifier to use with the ntpq utility.
  49. #controlkey 8
  50. # Enable writing of statistics records.
  51. #statistics clockstats cryptostats loopstats peerstats

4、启动NTP服务器

注意:如果有同步时间的定时任务要将其注销,否则会冲突

  1. [root@test ~]# /etc/init.d/ntpd start
  2. Starting ntpd: [ OK ]

显示节点列表

  1. [root@test ~]# ntpq -p
  2. remote refid st t when poll reach delay offset jitter
  3. ==============================================================================
  4. time5.aliyun.co 10.137.38.86 2 u 28 64 3 5.092 6241215 185.623
  5. 132.163.96.4 .INIT. 16 u - 64 0 0.000 0.000 0.000

5、客户机时间同步

客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示
no server suitable for synchronization found错误

  1. [root@client ~]# ntpdate 10.0.0.250
  2. 24 Oct 16:46:58 ntpdate[26694]: adjust time server 10.0.0.250 offset 0.320652 sec