一、聲明:
操作系統:cenots 6.4
server IP: 192.168.0.199
應老男孩老師的要求,做一次NTP服務的科普,由於NTP屬於外圍知識,沒必要佔用寶貴的課堂時間進行講解,特此在課下進行科普.
其他聲明:文章內容知識本人的一點理解,未必100%準確,更多資料請查看NTP官網:
http://www.ntp.org/ntpfaq/NTP-s-config.htm
http://linux.vbird.org/linux_server/0440ntp.php 鳥哥講解
二、NTP相關知識
1) 時間和時區
date :查看當前系統時間(具體用法這裏不囉嗦)
時區 :按照地球所在經度劃分。自己可以去查查地球儀瞭解先關知識。
常見時區:CST(chinese stardard time)中國標準時區
UTC(Coordinated Universal Time)世界統一時間
GMT 格林威治時間,也就是0時區
2)設置時間和時區
# cp /usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime 設置爲上海時間
# cp /usr/share/zoneinfo/posix/UTC /etc/localtime 設置爲UTC時間
三、NTP服務簡易原理
1)分層管理的類樹形結構。網絡中的節點有兩種可能:時鐘源或客戶
2)所有服務器歸入不同的Stratun(層)中。Stratum-1在頂層,有外部UTC接入,而Stratum-2則從Stratum-1獲取時間,Stratum-3從Stratum-2獲取時間,以此類推,但Stratum層的總數限制在15以內。所有這些服務器在邏輯上形成階梯式的架構相互連接,而Stratum-1的時間服務器是整個系統的基礎,也是頂端!
3)NTP的工作模式爲典型的C/S模型
四、重要性
NTP服務器在普通行業裏,由於對時間的精度要求不高,所以幾乎很少搭建,但是對於一些科研機構,對於時間的精度要求非常高,據悉,網上有位牛人修改ntp源代碼,可以實現比微秒更小的時間計量單位。
………………………………………………………………………………………………
NTP服務器的安裝、調試
一、安裝
安裝方法有兩種:yum 和source包安裝,這裏以yum安裝爲例:
首先查看系統是否安裝ntp包
[root@gang test]# rpm -q ntp || yum -y install ntp
ntp-4.2.6p5-1.el6.centos.x86_64
############源碼包安裝方法如下############
以源代碼爲例 # wget http://ntp.buptnet.edu.cn/ntp_chinese/down/server/ntp-4.2.0.tar.gz # tar -zxvf ntp-4.2.0.tar.gz –C /usr/local/src;cd /usr/local/src/ntp-4.2.0 2. 開始設定參數、編譯與安裝: #./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks # make clean ; make # make check # make install
############ END ##################
二、配置NTP配置文件/etc/ntp.conf
1. 不得不提的幾個經典問題
6.1.2.1. Can I use my system clock as reference clock? YES,as you do like so #server 127.127.1.1 #LCL,local clock,最好不要這樣。引發時間混亂 6.1.3.1. How can I configure the amount of logging information? 6.1.3.2. How can I speed up initial Synchronization? 6.1.3.3. How do I configure remote administration? 6.1.3.4. How do I use authentication keys? 6.1.3.5. What are all the different Keys used for? 6.1.3.6. How do I use the new autokey feature? 具體解決方法,查看文首提示的官網鏈接
2. 編輯配置文件 /etc/ntp.conf
# vi /etc/ntp.conf
# write the server time and local BIOS time in it ,and count it. driftfile /var/lib/ntp/drift #restrict default kod nomodify notrap nopeer noquery #restrict -6 default kod nomodify notrap nopeer noquery # the Usage # restrict [ your IP address ] mask [ netmask ] [ power ] # power: ignore --> deny all of types NTP connection # power: nomodify --> deny clients use ntpq or ntpc to change the time of NTP server # power: noquery --> deny clients use ntpq or ntpc to checkout time , that means deny clients use the NTP server. # power: notrap --> dont't support remote event logging. # power: notrust --> deny clients that not be authored. # if "[ power ]" is null , it means every client can use this host of NTP server. restrict default nomodify notrap noquery restrict 127.0.0.1 restrict 192.168.0.0 mask 255.255.255.0 nomodify restrict -6 ::1 # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # server 210.72.145.44 is BJ Time Station. # source of ntp-server ; and prefer is priority server asia.pool.ntp.org prefer server 210.72.145.44 # local network lo interface server 127.127.1.1 # LCL does't sync fudge 127.127.1.1 stratum 10 refid NIST # broadcast delay frequency Broadcastdelay 0.008 #server 127.127.1.1 logfile /var/log/ntp.log #broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8 # Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats
3. 啓動ntp服務
[root@gang ~]# /etc/init.d/ntpd start Starting ntpd: [ OK ]4. 一下爲正常
4.以下爲正常啓動日誌message
Mar 24 18:29:45 gang ntpd[3547]: ntpd [email protected] Sat Nov 23 18:21:48 UTC 2013 (1) Mar 24 18:29:45 gang ntpd[3548]: proto: precision = 0.628 usec Mar 24 18:29:45 gang ntpd[3548]: 0.0.0.0 c01d 0d kern kernel time sync enabled Mar 24 18:29:45 gang ntpd[3548]: line 32 column 1 syntax error, unexpected T_String, expecting $end Mar 24 18:29:45 gang ntpd[3548]: syntax error in /etc/ntp.conf line 32, column 1 Mar 24 18:29:45 gang ntpd[3548]: Listen and drop on 0 v4wildcard 0.0.0.0 UDP 123 Mar 24 18:29:45 gang ntpd[3548]: Listen and drop on 1 v6wildcard :: UDP 123 Mar 24 18:29:45 gang ntpd[3548]: Listen normally on 2 lo 127.0.0.1 UDP 123 Mar 24 18:29:45 gang ntpd[3548]: Listen normally on 3 br0 192.168.0.199 UDP 123 Mar 24 18:29:45 gang ntpd[3548]: Listen normally on 4 eth0 fe80::eea8:6bff:fea4:912b UDP 123 Mar 24 18:29:45 gang ntpd[3548]: Listen normally on 5 br0 fe80::eea8:6bff:fea4:912b UDP 123 Mar 24 18:29:45 gang ntpd[3548]: Listen normally on 6 lo ::1 UDP 123 Mar 24 18:29:45 gang ntpd[3548]: peers refreshed Mar 24 18:29:45 gang ntpd[3548]: Listening on routing socket on fd #23 for interface updates
……………………………………NTP 服務器配置完成………………………………
###################NTP 客戶端的配置##################
網上有很多是在配置ntp.conf來進行客戶端的同步,但是我覺得這個很沒必要,就因爲時間同步,就要開放一個端口?crontab計劃任務完全可以勝任誒!單開個端口風險貌似很大的樣紙...不過你們公司如果時間要求高,那也可以這麼做. 具體配置如下:
# vi /etc/ntp.conf
添加一行
server 192.168.0.199
其次: 啓動ntp服務
# /etc/init.d/ntpd start
##相關排錯方式及講解:
在server端,我只配置了上級服務器:asia.pool.ntp.org之後,發現啓動日誌正常,但是時間一直不同步。輸入命令查看得知如下圖提示:
[root@gang ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== proxy02.ispros. .INIT. 16 u - 256 0 0.000 0.000 0.000
發現pool 一直在增加,但是一直不同步,ping asia.pool.ntp.org發現延遲較高,甚至不通。
[root@gang ~]# ping asia.pool.ntp.org PING asia.pool.ntp.org (78.111.50.2) 56(84) bytes of data. ^C --- asia.pool.ntp.org ping statistics --- 28 packets transmitted, 0 received, 100% packet loss, time 27241ms
解決方法:在配置文件中,添加多個server ,以防單一server連接不通時,時間同步異常。配置完成後,正常情況如下圖:
[root@gang ~]# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *time2.isu.net.s .GPS. 1 u 2 64 27 341.015 -64.352 16.181 210.72.145.44 .INIT. 16 u - 64 0 0.000 0.000 0.000 LOCAL(1) .NIST. 10 l 13 64 177 0.000 0.000 0.000
ntpq -p輸出結果說明(摘錄部分):
remote: 它指的就是本地機器所連接的遠程NTP服務器
refid: 它指的是參考的上一層NTP主機的地址
st: 遠程服務器的級別. 由於NTP是層型結構,有頂端的服務器,多層的Relay Server再到客戶端. 所以服務器從高到低級別可以設定爲1-16. 爲了減緩負荷和網絡堵塞,原則上應該避免直接連接到級別爲1的服務器的.
when: 用做計時,用來告訴我們還有多久本地機器就需要和遠程服務器進行一次時間同步
poll: 本地機和遠程服務器多少時間進行一次同步(單位爲秒). 在一開始運行NTP的時候這個poll值會比較小,那樣和服務器同步的頻率也就增加了,可以儘快調整到正確的時間範圍.之後poll值會逐漸增大,同步的頻率也就會相應減小
reach: 這是一個八進制值,表示已經向上層NTP伺服器要求更新的次數。每成功連接一次它的值就會增加。
delay: 網路傳輸過程當中延遲的時間,單位爲 10^(-6) 秒
offset: 這是個最關鍵的值, 它告訴了我們本地機和服務器之間的時間差別. offset越接近於0,我們就和服務器的時間越接近。單位爲10^(-3)秒
jitter: Linux系統時間與BIOS硬件時間的差異時間,單位爲10^(-6)秒。簡單地說這個數值的絕對值越小我們和服務器的時間就越精確。