Ubuntu系統下ntp伺服器搭建
本文講講搭建一個ntp伺服器。
下面的操作基於ubuntu14.04版本。
安裝步驟
在ubuntu下安裝ntp伺服器十分方便,只要一條命令即可搞定:
sudo apt-get install ntp
一:在配置檔案中的:
restrict -6 default kod notrap nomodify nopeer noquery
語句後面加入:
restrict 192.168.10.0 mask 255.255.255.0 nomodify(主要是允許能同步的伺服器所在的內部網段)
二:為了防止出現一些問題,還要編輯/etc/ntp.conf,加入:
server 127.127.1.0
fudge 127.127.1.0 stratum 8
主要是解決同步時出現 no server suitable for synchronization found錯誤。
可以使用下面命令重啟ntp服務,安裝後,系統啟動時會自動啟動該服務。
sudo /etc/init.d/ntp restart
在ntp server上重新啟動ntp服務後,ntp server自身或者與其server的同步的需要一個時間段,這個過程可能是5分鐘,在這個時間之內在客戶端執行ntpdate命令時會產生no server suitable for synchronization found的錯誤。
那麼如何知道何時ntp server完成了和自身同步的過程呢?
在ntp server上使用命令:
#ntpq -p
問題解決
下面是遇到的一些問題的解決。
1、
安裝ntpd後,在本機執行ntpdate出錯,如下:
ntpdate[19979]: the NTP socket is in use, exiting
在執行命令,新增-u選項即可,比如:
ntpdate -u 202.120.2.101
ntpdate -u 133.100.11.8
2、
在一臺機器(IP是172.18.100.168)上安裝好ntp伺服器後,在另一臺裝置上同步時間,結果出錯了,如下:
# ntpdate -d 172.18.100.168
14 Aug 10:26:30 ntpdate[1448]: ntpdate
Looking for host 172.18.100.168 and service ntp
host found : 172.18.100.168
transmit(172.18.100.168)
receive(172.18.100.168)
transmit(172.18.100.168)
receive(172.18.100.168)
transmit(172.18.100.168)
receive(172.18.100.168)
transmit(172.18.100.168)
receive(172.18.100.168)
transmit(172.18.100.168)
172.18.100.168: Server dropped: strata too high
server 172.18.100.168, port 123
stratum 16, precision -21, leap 11, trust 000
refid [172.18.100.168], delay 0.02574, dispersion 0.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Thu, Feb 7 2036 14:28:16.000
originate timestamp: d7f42487.0fcf554a Fri, Oct 24 2014 9:06:15.061
transmit timestamp: d977d056.849ff92f Fri, Aug 14 2015 10:26:30.518
filter delay: 0.02591 0.02574 0.02580 0.02577
0.00000 0.00000 0.00000 0.00000
filter offset: -2540641 -2540641 -2540641 -2540641
0.000000 0.000000 0.000000 0.000000
delay 0.02574, dispersion 0.00000
offset -25406415.456428
14 Aug 10:26:30 ntpdate[1448]: no server suitable for synchronization found
解決方法:
編輯/etc/ntp.conf
server 127.127.1.0
fudge 127.127.1.0 stratum 8
在測試過程中發現伺服器時間與真實時間差別太大,會有同步不上的情況,如下:
$ sudo ntpdate -d 172.18.100.168
10 Nov 10:07:57 ntpdate[5892]: ntpdate [email protected] Wed Oct 9 18:57:00 UTC 2013 (1)
Looking for host 172.18.100.168 and service ntp
host found : 172.18.100.168
transmit(172.18.100.168)
receive(172.18.100.168)
transmit(172.18.100.168)
receive(172.18.100.168)
transmit(172.18.100.168)
receive(172.18.100.168)
transmit(172.18.100.168)
receive(172.18.100.168)
172.18.100.168: Server dropped: server is very broken
server 172.18.100.168, port 123
stratum 9, precision -21, leap 00, trust 000
refid [172.18.100.168], delay 0.02631, dispersion 0.00000
transmitted 4, in filter 4
reference time: d80a9c4a.02281b8f Mon, Nov 10 2014 10:07:06.008
originate timestamp: d8096562.75139fc6 Sun, Nov 9 2014 12:00:34.457
transmit timestamp: d80a9c83.eee02d38 Mon, Nov 10 2014 10:08:03.933
filter delay: 0.02631 0.02634 0.02664 0.02635
0.00000 0.00000 0.00000 0.00000
filter offset: -79649.4 -79649.4 -79649.4 -79649.4
0.000000 0.000000 0.000000 0.000000
delay 0.02631, dispersion 0.00000
offset -79649.476483
10 Nov 10:08:03 ntpdate[5892]: no server suitable for synchronization found
但有時候伺服器時間比客戶端時間大,也是可以的,這類問題比較奇怪。下面是成功的例子:
# date 01091100
Thu Jan 9 11:00:00 GMT-8 2014
# ntpdate -u 172.18.100.168
Looking for host 172.18.100.168 and service ntp
host found : 172.18.100.168
9 Nov 12:01:49 ntpdate[626]: step time server 172.18.100.168 offset 26269305.872492 sec
# date
Sun Nov 9 12:01:50 GMT-8 2014
而有時候,國內的伺服器卻似段公子的六脈神劍,時靈時不靈,下面是一個例子:
# date 11091200
Sun Nov 9 12:00:00 CST 2014
[email protected]:~# ntpdate -u 202.120.2.101
9 Nov 12:02:28 ntpdate[2607]: no server suitable for synchronization found
[email protected]:~# ntpdate -u 202.120.2.101
9 Nov 12:02:39 ntpdate[2608]: no server suitable for synchronization found
[email protected]:~#
[email protected]:~#
[email protected]:~# ntpdate -u 133.100.11.8
10 Nov 10:29:22 ntpdate[2628]: step time server 133.100.11.8 offset 80736.496480 sec
[email protected]:~# date
Mon Nov 10 10:29:24 CST 2014
參考文章: