1.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)
1.2 NTP服務器安裝
[root@test ~]# rpm -qa ntp ntp-4.2.6p5-10.el6.centos.2.x86_64
如果沒有安裝就yum install ntp -y進行安裝
1.3 配置NTP服務
備份配置文件
[root@test ~]# cp /etc/ntp.conf{,.bak} [root@test ~]# ll /etc/ntp.conf* -rw-r--r--. 1 root root 1778 Jan 11 2017 /etc/ntp.conf -rw-r--r-- 1 root root 1778 Oct 23 23:10 /etc/ntp.conf.bak
精簡化配置文件
[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 4 driftfile /var/lib/ntp/drift 5 6 # Permit time synchronization with our time source, but do not 7 # permit the source to query or modify the service on this system. 8 restrict default kod nomodify notrap nopeer noquery 9 restrict -6 default kod nomodify notrap nopeer noquery 10 11 # Permit all access over the loopback interface. This could 12 # be tightened as well, but to do so would effect some of 13 # the administrative functions. 14 restrict 127.0.0.1 15 restrict -6 ::1 16 17 # Hosts on local network are less restricted. 18 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 19 # 允許內網其他機器同步時間 20 restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap 21 22 # Use public servers from the pool.ntp.org project. 23 # Please consider joining the pool (http://www.pool.ntp.org/join.html). 24 #server 0.centos.pool.ntp.org iburst 25 #server 1.centos.pool.ntp.org iburst 26 #server 2.centos.pool.ntp.org iburst 27 #server 3.centos.pool.ntp.org iburst 28 29 # 定義使用的上游 ntp服務器,將原來的注釋 30 server time1.aliyun.com 31 server ntp1.aliyun.com 32 33 #broadcast 192.168.1.255 autokey # broadcast server 34 #broadcastclient # broadcast client 35 #broadcast 224.0.1.1 autokey # multicast server 36 #multicastclient 224.0.1.1 # multicast client 37 #manycastserver 239.255.254.254 # manycast server 38 #manycastclient 239.255.254.254 autokey # manycast client 39 40 # 允許上層時間服務器主動修改本機時間 41 restrict time1.aliyun.com nomodify notrap noquery 42 restrict ntp1.aliyun.com nomodify notrap noquery 43 44 # 外部時間服務器不可用時,以本地時間作為時間服務 45 server 127.127.1.0 46 fudge 127.127.1.0 stratum 10 47 48 # Enable public key cryptography. 49 #crypto 50 51 includefile /etc/ntp/crypto/pw 52 53 # Key file containing the keys and key identifiers used when operating 54 # with symmetric key cryptography. 55 keys /etc/ntp/keys 56 57 # Specify the key identifiers which are trusted. 58 #trustedkey 4 8 42 59 60 # Specify the key identifier to use with the ntpdc utility. 61 #requestkey 8 62 63 # Specify the key identifier to use with the ntpq utility. 64 #controlkey 8 65 66 # Enable writing of statistics records. 67 #statistics clockstats cryptostats loopstats peerstats
1.4 啟動NTP服務器
注意:如果有同步時間的定時任務要將其注銷,否則會沖突
[root@test ~]# /etc/init.d/ntpd start
Starting ntpd: [ OK ]
顯示節點列表
[root@test ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== time5.aliyun.co 10.137.38.86 2 u 28 64 3 5.092 6241215 185.623 132.163.96.4 .INIT. 16 u - 64 0 0.000 0.000 0.000
1.5 客戶機時間同步
客戶機要等幾分鍾再與新啟動的ntp服務器進行時間同步,否則會提示
no server suitable for synchronization found錯誤
[root@client ~]# ntpdate 10.0.0.250 24 Oct 16:46:58 ntpdate[26694]: adjust time server 10.0.0.250 offset 0.320652 sec