NTP概要
NTP要提供准確的時間,就必須有准確的時間來源,那可以用格林尼治時間嗎?答案是否定的。因為格林尼治時間是以地球自轉為基礎的時間計量系統,但是地球每天的自轉是有些不規則的,而且正在緩慢減速,因此,格林尼治時間已經不再被作為標准時間使用。
新的標准時間,是由原子鍾報時的國際標准時間UTC(Universal Time Coordinated,世界協調時)。所以NTP獲得UTC的時間來源可以是原子鍾、天文台、衛星,也可以從Internet上獲取。
有了准確而可靠的的時間源,那這個時間如何傳播呢?在NTP中,定義了時間按照服務器的等級傳播,按照離外部UTC源遠近將所有的服務器歸入不同的Stratum(層)中,例如把通過GPS(Global Positioning System,全球定位系統)取得發送標准時間的服務器叫Stratum-1的NTP服務器,而Stratum-2則從Stratum-1獲取時間,Stratum-3從Stratum-2獲取時間,以此類推,但Stratum層的總數限制在15以內。所有這些服務器在邏輯上形成階梯式的架構相互連接,而Stratum-1的時間服務器是整個系統的基礎,這種階梯式的架構示意圖如圖1所示。
圖1 NTP階梯式的架構示意圖
計算機主機一般同多個時鍾服務器連接,利用統計學的算法過濾來自不同服務器的時間,以選擇最佳的路徑和來源以便校正主機時間。即使在主機長時間無法與某一時鍾服務器聯系的情況下,NTP服務依然可以有效運轉。
為了防止對時鍾服務器的惡意破壞,NTP使用了識別機制,檢查發送來的信息是否是真正來自所宣稱的時鍾服務器並檢查信息的返回路徑,以提供對抗干擾的保護機制。[1]
NTP時間同步報文中包含的時間是格林威治時間,是從1900年開始計算的秒數
-
NTP安裝
1.編譯安裝ntp源碼包
[root@server tools]# tar xf ntp-4.2.6p5.tar.gz
[root@server tools]# cd ntp-4.2.6p5
[root@server ntp-4.2.6p5]# ./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
[root@server ntp-4.2.6p5]# make && make install
2.修改配置文件
[root@server ~]# cp /etc/ntp.conf /etc/ntp.conf.bak
[root@server ~]# vi /etc/ntp.conf
1)允許任何IP的客戶機都可以進行時間同步
將"restrict default kod nomodify notrap nopeer noquery"這行修改成:
restrict default nomodify
2)只允許192.168.1.*網段的客戶機進行時間同步
在"restrict default nomodify notrap noquery(表示默認拒絕所有IP的時間同步)"之后增加一行:
restrict 192.168.1.0 mask 255.255.255.0 nomodify
3、啟動NTP服務並設置隨機啟動
[root@server ~]# service ntpd start
[root@server ~]# chkconfig --level 35 ntpd on
4、查看ntp服務器工作情況
[root@server ~]# netstat -unl |grep 123 #查看123端口,操作結果如圖
udp 0 0 192.168.1.41:123 0.0.0.0:*
udp 0 0 127.0.0.1:123 0.0.0.0:*
udp 0 0 0.0.0.0:123 0.0.0.0:*
udp 0 0 fe80::20c:29ff:feb1:123 :::*
udp 0 0 ::1:123 :::*
udp 0 0 :::123 :::*
5、查看ntp進程是否正常啟動
[root@server ~]# ps -ef|grep ntp
ntp 17589 1 0 21:20 ? 00:00:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
root 17607 4441 0 21:26 pts/0 00:00:00 grep ntp
PS:ntpd啟動后,客戶機要等幾分鍾再與其進行時間同步,否則會提示"no server suitable for synchronization found"錯誤。
-
客戶機驗證
[root@client ~]# date serverIP #查看服務器的ntp服務是否有響應
[root@localhost ntp]# ntpdate -d 10.200.51.225
21 Jan 09:57:39 ntpdate[2790]: ntpdate 4.2.6p5@1.2349-o Fri Jul 24 10:54:24 UTC 2015 (1)
Looking for host 10.200.51.225 and service ntp
host found : 10.200.51.225
transmit(10.200.51.225)
receive(10.200.51.225)
transmit(10.200.51.225)
receive(10.200.51.225)
transmit(10.200.51.225)
receive(10.200.51.225)
transmit(10.200.51.225)
receive(10.200.51.225)
10.200.51.225: Server dropped: Leap not in sync
server 10.200.51.225, port 123
stratum 3, precision -24, leap 11, trust 000
refid [10.200.51.225], delay 0.02856, dispersion 0.00009
transmitted 4, in filter 4
reference time: da4aae25.1bacb976 Thu, Jan 21 2016 9:08:53.108
originate timestamp: da4aba6a.dd962e42 Thu, Jan 21 2016 10:01:14.865
transmit timestamp: da4ab993.8900d9af Thu, Jan 21 2016 9:57:39.535
filter delay: 0.02950 0.02870 0.02856 0.02882
0.00000 0.00000 0.00000 0.00000
filter offset: 215.3283 215.3287 215.3286 215.3286
0.000000 0.000000 0.000000 0.000000
delay 0.02856, dispersion 0.00009
offset 215.328671 # offset有時間差即可