NTP服務和時間同步

目標環境,5臺linux centos 6.3, 一臺作爲NTPD服務與外部公共NTP服務同步時間,同時作爲內網的NTPD服務器,其他機器與這臺服務做時間同步。

 服務器IP 角色   說明 同步方式
 192.168.1.135   NTPD服務

 1、負責與外部公共NTPD服務同步標準時間

 2、作爲內外網絡的NTPD服務

NTPD服務平滑同步

 192.168.1.xxx  內外NTP客戶端  內網設備與192.168.1.135同步時間 NTPD服務平滑同步
 ……   內外NTP客戶端   內網設備與192.168.1.135同步時間 NTPD服務平滑同步

  

1、NTP時間同步方式選擇

   NTP同步方式在linux下一般兩種:使用ntpdate命令直接同步和使用NTPD服務平滑同步。有什麼區別呢,簡單說下,免得時間長了,概念又模糊。

   現有一臺設備,系統時間是 13:00 , 真實的當前時間(在空中,也許衛星上,這裏假設是在準備同步的上級目標NTP服務器)是: 12:30  。如果我們使用ntpdate同步(ntpdate -u 目標NTP服務器IP),操作系統的時間立即更新爲12:30,假如,我們的系統有一個定時應用,是在每天12:40運行,那麼實際今天這個的任務已經運行過了(當前時間是13:00嘛),現在被ntpdate修改爲12:30,那麼意味作10分鐘後,又會執行一次任務,這就糟糕了,這個任務只能執行一次的嘛!!我想你(其實是我)已經懂了ntpdate時間同步的隱患,當然這個例子有些極端,但的確是有風險的,生產環境我不打算這麼幹,還是穩妥點好。所以解決該問題的辦法就是時間平滑更改,不會讓一個時間點在一天內經歷兩次,這就是NTPD服務方式平滑同步時間,它每次同步時間的偏移量不會太陡,是慢慢來的(問:怎麼來,沒有細究,只曉得一次一點的同步,完全同步好需要較長時間,所以一般開啓NTPD服務同步前先用ntpdate先手動同步一次)。

 

2、安裝配置

CentOS 6.3系統已經自帶了NTPD服務,一般默認是按照了的,如果沒有安裝,先檢查下,然後配置好yum倉庫,yum方式安裝下就OK,具體如下:

# rpm -q ntp

ntp-4.2.4p8-2.el6.x86_64    // 這表示已安裝了,如果沒有安裝,這是空白。

如果沒有安裝,我們按照下

# yum install ntp

......

按上面的安裝方式在內網每臺服務器上都安裝好NTP軟件包。

完成後,都需要配置NTP服務爲自啓動

# chkconfig ntpd on

# chkconfig --list ntpd

ntpd           0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉

 

在配置前,先使用ntpdate手動同步下時間,免得本機與外部時間服務器時間差距太大,讓ntpd不能正常同步。

# ntpdate -u 202.112.10.36

22 Dec 16:52:38 ntpdate[6400]: adjust time server 202.112.10.36 offset 0.012135 sec

 

配置內網NTP-Server(192.168.1.135)

下面主要是配置內網的NPTD服務器(192.168.1.135), NTPD服務配置核心就在/etc/ntp.conf文件,配置好了就OK。網上特別是老外的文章都很簡單,我上當了,媽喲,基礎環境不一樣,我們得中國特色才行。先上配置文件再說,紅色部分是我的修改,其他的是默認。

 

# For more information about this file, see the man pages

# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

 

driftfile /var/lib/ntp/drift

 

# Permit time synchronization with our time source, but do not

# permit the source to query or modify the service on this system.

restrict default kod nomodify notrap nopeer noquery

restrict -6 default kod nomodify notrap nopeer noquery

 

# Permit all access over the loopback interface.  This could

# be tightened as well, but to do so would effect some of

# the administrative functions.

restrict 127.0.0.1

restrict -6 ::1

 

# Hosts on local network are less restricted.

# 允許內網其他機器同步時間

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

 

# Use public servers from the pool.ntp.org project.

# Please consider joining the pool (http://www.pool.ntp.org/join.html).

# 中國這邊最活躍的時間服務器 : http://www.pool.ntp.org/zone/cn

server 210.72.145.44 perfer   # 中國國家受時中心

server 202.112.10.36             # 1.cn.pool.ntp.org

server 59.124.196.83             # 0.asia.pool.ntp.org

 

#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

 

# allow update time by the upper server 

# 允許上層時間服務器主動修改本機時間

restrict 210.72.145.44 nomodify notrap noquery

restrict 202.112.10.36 nomodify notrap noquery

restrict 59.124.196.83 nomodify notrap noquery

 

# Undisciplined Local Clock. This is a fake driver intended for backup

# and when no outside source of synchronized time is available. 

# 外部時間服務器不可用時,以本地時間作爲時間服務

server  127.127.1.0     # local clock

fudge   127.127.1.0 stratum 10

 

# 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

配置參數和命令簡單說明請參考:http://linux.vbird.org/linux_server/0440ntp.php#server_ntp.conf

 

配置文件修改完成,保存退出,啓動服務。

# service ntpd start

......

啓動後,一般需要5-10分鐘左右的時候才能與外部時間服務器開始同步時間。可以通過命令查詢NTPD服務情況。

 

查看服務連接和監聽

# netstat -tlunp | grep ntp      

udp        0      0 192.168.1.135:123           0.0.0.0:*                               23103/ntpd          

udp        0      0 127.0.0.1:123               0.0.0.0:*                               23103/ntpd          

udp        0      0 0.0.0.0:123                 0.0.0.0:*                               23103/ntpd          

udp        0      0 fe80::6cae:8bff:fe3d:f65:123 :::*                                    23103/ntpd          

udp        0      0 fe80::6eae:8bff:fe3d:f65:123 :::*                                    23103/ntpd          

udp        0      0 ::1:123                     :::*                                    23103/ntpd          

udp        0      0 :::123                      :::*                                    23103/ntpd  

看紅色加粗的地方,表示連接和監聽已正確,採用UDP方式

 

ntpq -p 查看網絡中的NTP服務器,同時顯示客戶端和每個服務器的關係

# ntpq -p

# ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

*202.112.10.36   202.112.10.60    2 u  277  128  314  201.553    9.193  17.068

+59.124.196.83   129.6.15.28      2 u   88  128  377   71.153  -25.111  14.004

 LOCAL(0)        .LOCL.          10 l   15   64  377    0.000    0.000   0.000

 

位置 標誌 含義
符號 * 響應的NTP服務器和最精確的服務器
+ 響應這個查詢請求的NTP服務器
blank(空格) 沒有響應的NTP服務器
標題 remote 響應這個請求的NTP服務器的名稱
refid NTP服務器使用的更高一級服務器的名稱
st 正在響應請求的NTP服務器的級別
when 上一次成功請求之後到現在的秒數
poll 本地和遠程服務器多少時間進行一次同步,單位秒,在一開始運行NTP的時候這個poll值會比較小,服務器同步的頻率大,可以儘快調整到正確的時間範圍,之後poll值會逐漸增大,同步的頻率也就會相應減小
reach 用來測試能否和服務器連接,是一個八進制值,每成功連接一次它的值就會增加
delay 從本地機發送同步要求到ntp服務器的往返時間
offset 主機通過NTP時鐘同步與所同步時間源的時間偏移量,單位爲毫秒,offset越接近於0,主機和ntp服務器的時間越接近
jitter 統計了在特定個連續的連接數裏offset的分佈情況。簡單地說這個數值的絕對值越小,主機的時間就越精確

 

ntpstat 命令查看時間同步狀態,這個一般需要5-10分鐘後才能成功連接和同步。所以,服務器啓動後需要稍等下。

剛啓動的時候,一般是:

# ntpstat 

unsynchronised

  time server re-starting

   polling server every 64 s

連接並同步後:

synchronised to NTP server (202.112.10.36) at stratum 3 

   time correct to within 275 ms

   polling server every 256 s

 

OK,內網的NTPD服務已經配置完成,如果所有正常後,開始配置內網的其他設備與這臺服務器作爲時間同步服務。

 

配置內網NTP-Clients

內網其他設備作爲NTP的客戶端配置,相對就比較簡單,而且所有設備的配置都相同。

首先需要安裝NTPD服務,然後配置爲自啓動(與NTP-Server完全一樣)。然後找其中一臺配置/etc/ntp.conf文件,配置完成驗證通過後,拷貝到其他客戶端機器,直接使用即可。

# yum install ntp

...

# chkconfig ntp on

# vim /etc/ntp.conf

driftfile /var/lib/ntp/drift

restrict 127.0.0.1

restrict -6 ::1

 

# 配置時間服務器爲本地的時間服務器

server 192.168.1.135

 

restrict 192.168.1.135 nomodify notrap noquery

 

server  127.127.1.0     # local clock

fudge   127.127.1.0 stratum 10

 

includefile /etc/ntp/crypto/pw

 

keys /etc/ntp/keys

 爲了簡單,這裏只列出了配置項,註釋全部清理了。

 OK,保存退出,請求服務器前,請先使用ntpdate手動同步下時間

# ntpdate -u 192.168.0.135

22 Dec 17:09:57 ntpdate[6439]: adjust time server 192.168.1.135 offset 0.004882 sec

這裏有可能出現同步失敗,一般情況下原因都是本地的NTPD服務器還沒有正常啓動起來,一般需要幾分鐘時間後才能開始同步。

錯誤判斷請參考後面的錯誤處理。

 

# service ntpd start

....

啓動後,查看同步情況

# ntpq -p

# ntpstat

.....

因爲是內網,一般ntpstat很快就可以同步上,幾分鐘需要等下.

OK,本機客戶端配置完成後,使用SCP拷貝/etc/ntp.conf到其他需要同步的客戶端機器,啓動NTPD服務即可。

其他客戶端機器上操作配置如下:

 ntpdate -u 192.168.0.135

22 Dec 17:09:57 ntpdate[6439]: adjust time server 192.168.1.135 offset 0.004882 sec

# scp 192.168.1.xxx:/etc/ntp.conf /etc/ntp.conf

# service ntpd start

 

 

3、錯誤問題處理

用於收集安裝,配置和應用中出現的問題

 

錯誤1:ntpdate -u ip ->  no server suitable for synchronization found

判斷:在ntp客戶端用ntpdate –d serverIP查看,發現有“Server dropped: strata too high”的錯誤,並且顯示“stratum 16”。而正常情況下stratum這個值得範圍是“0~15”。

原因:NTP server還沒有和其自身或者它的server同步上。在ntp server上重新啓動ntp服務後,ntp server自身或者與其server的同步的需要一個時間段,這個過程可能是5分鐘,在這個時間之內在客戶端運行ntpdate命令時會產生no server suitable for synchronization found的錯誤。

處理:等待幾分鐘後,重試一般解決。

也可以使用命令 ntpq -p查看情況

參考:http://blog.csdn.net/weidan1121/article/details/3953021

 

發佈了10 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章