linux之NTP時間同步服務介紹

前文已經介紹了linux中時間相關的概念,在大型服務器中,應用最廣泛的其中最重要最精確的就是ntp服務了。

一、ntp定義

NTP(Network Time Protocol,網絡時間協議)是由RFC 1305定義的時間同步協議,用來在分佈式時間服務器和客戶端之間進行時間同步。NTP基於UDP報文進行傳輸,使用的UDP端口號爲123。

使用NTP的目的是對網絡內所有具有時鐘的設備進行時鐘同步,使網絡內所有設備的時鐘保持一致,從而使設備能夠提供基於統一時間的多種應用。

對於運行NTP的本地系統,既可以接收來自其他時鐘源的同步,又可以作爲時鐘源同步其他的時鐘,並且可以和其他設備互相同步。


二、ntp原理

介紹ntp原理的文章很多,網上一大把,在此就簡單說下。


系統時鐘同步的工作過程如下:


Device A發送一個NTP報文給Device B,該報文帶有它離開Device A時的時間戳,該時間戳爲10:00:00am(T1)。
當此NTP報文到達Device B時,Device B加上自己的時間戳,該時間戳爲11:00:01am(T2)。
當此NTP報文離開Device B時,Device B再加上自己的時間戳,該時間戳爲11:00:02am(T3)。
當Device A接收到該響應報文時,Device A的本地時間爲10:00:03am(T4)。


這樣,Device A已經擁有足夠的信息來計算兩個重要的參數:


NTP報文的往返時延Delay=(T4-T1)-(T3-T2)=2秒。
Device A相對Device B的時間差offset=((T2-T1)+(T3-T4))/2=1小時。


Device A就能夠根據這些信息來設定自己的時鐘,與Device B的時鐘同步。


三、ntp在linux中的用法

我們主要介紹ntp client端

1、在client端,首先配置ntpd.conf文件,添加server ip(時間服務提供商的ip或者主機名),可以指定多個

啓動ntpd服務:/etc/init.d/ntpd start

此時ntp服務啓動,通過udp的123端口與server端通信,開始時間同步

2、client和server的時間同步有兩種情況:時間跳變(time step)和漸變(time slew)

跳變是指在client和server間時間差過大時(默認128ms),瞬間調整client端的系統時間

漸變是指時間差較小時,通過改變client端的時鐘頻率,進而改變client端中“1秒”的“真實時間”,保持client端時間連續性。

舉個例子,如果client端比server端慢10s,通過ntpd,client端的中每1秒現實時間是1.0005秒!雖然client端的時間仍然是1秒1秒增加的,通過調整每秒的實際時間,直到與serrver的時間相同。在這個例子中,10s/0.0005s=20000s,20000s/60/60=5.55555小時,即需要5個多小時才能消除10s的誤差。

在linux中,很多應用軟件依賴系統的時間連續性來正確工作,系統時間的跳變將導致軟件出現意想不到的問題,所以時間漸變纔是ntpd的主要應用場景。

3、那麼怎麼禁止ntpd的時間跳變,只採用時間漸變呢?

剛開始通過man ntpd,嘗試在ntpd啓動配置(/etc/sysconfig/ntpd)中加-x選項:

-xNormally, the time is slewed if the offset is less than the step threshold, which is 128 ms by default, and stepped if above the threshold. This option forces the time to be slewed in all cases. If the step threshold is set to zero, all offsets are stepped, regardless of value and regardless of the -x option. In general, this is not a good idea, as it bypasses the clock state machine which is designed to cope with large time and frequency errors Note: Since the slew rate is limited to 0.5 ms/s, each second of adjustment requires an amortization interval of 2000 s. Thus, an adjustment of many seconds can take hours or days to amortize. This option can be used with the -q option.

結果發現-x只是提高了時間跳變的閾值,在client與server時間差小於600秒時,時間的調整使用漸變,大於600秒,時間調整使用跳變形式。

查了很多資料,最後確定在ntp配置文件/etc/ntp.conf中添加字段:

tinker panic 600

這句話的意思是在時間差大於600秒的情況下,ntpd進程自動關閉,ntpd退出時會向/var/log/messages中寫入log

在時間差過大時,應該由用戶手動設置系統時間或者調用ntpdate命令,這樣能避免因爲時間跳變出現的問題。

四、ntpq的用法和參數解釋

ntpq是ntp的輔助命令,用來查看ntp的運行狀態



參數            功能                            示例
remote         時間服務器地址
refid          上一級ntp服務器                   可以是ip;或其他比如:JJY (LF Fukushima, Japan) time standard radio receiver;GPS
St             Stratum值                        0~16
t               Type,服務器通訊類型              U:unicast
When           上一次與服務器通訊時間
Poll           輪詢間隔 <span style="white-space:pre">	</span>                       默認64
Reach          通訊成功次數                      8位二進制數,成功一次左移一位
Delay          本地與服務器通信時延
Offset         本地與服務器的時間偏差
Jitter 



在remote項前邊,會現實client與server的同步狀態,正常同步用“*”表示,備用服務器用"+"表示,未同步不現實,就像上圖那樣.

詳細說明:

狀態 		含義
“ ” 1)non-communicating remote machines
      2)“LOCAL” for this local host
      3)(unutilised) high stratum servers
“x” Out of tolerance, do not use (discarded by intersection algorithm);
“-” Out of tolerance, do not use (discarded by the cluster algorithm);
“#” Good remote peer or server but not utilised (not among the first six peers sorted by synchronization distance, ready as a backup source);
“+” Good and a preferred remote peer or server (included by the combine algorithm);
“*” The remote peer or server presently used as the primary reference;
“o” PPS peer


五、參考文獻

限於自身知識有限,且表達能力欠佳,下邊列出我覺得很有參考價值的網頁文章,大家可以詳細學習

http://www.cnblogs.com/zhouhbing/p/3877104.html
http://blog.itpub.net/14184018/viewspace-694078/
http://lists.ntp.org/pipermail/questions/2007-May/014166.html
http://blog.itpub.net/28371090/viewspace-769264/
http://bbs.chinaunix.net/thread-4156663-1-1.html
http://doc.ntp.org/4.1.0/miscopt.htm
http://lists.ntp.org/pipermail/questions/2005-February/004468.html
http://nlug.ml1.co.uk/2012/01/ntpq-p-output/831
http://support.hp.com/us-en/document/c03651251
http://www.clock.org/ntp/prefer.html
https://www.eecis.udel.edu/~mills/ntp/html/select.html
http://www.ntp.org/ntpfaq/NTP-s-def.htm
http://www.ietf.org/rfc/rfc5905.txt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章