NTP多種模式的配置


自己安裝和配置了一個NTP服務器的一些心得,希望與大家分享。寫了一個文檔包含了各個命令和參數的具體含義,由於不能上傳無法與大家分享。以后還希望大家多多支持和幫助我們共同成長,我會不斷做把這些年的經營和教訓與大家分享。

在日常生活中,我們會遇到服務器或我們的PC時間不准確的問題。特別是在一些比較重要的行業中,系統對時間的要求又比較高。系統需要一個准確而可靠的時鍾源來保障應用的運行。特別在證券行業中,時間的要求更加嚴格。一般可以根據證交所提供的時間為准,在報盤系統的工作站上,證交所會每天強制更新其時間,以達到該時間與交易所配置的標准時間為准。我們可以設置該工作站為NTP服務器,為其他系統提供與交易所相同的時間服務。同時在互聯網上有大量的時間服務器可以連接,可以配置為其客戶端來校准我們主機的時間。

1 NTP服務介紹

Network Time Protocol(NTP)是用來使計算機時間同步化的一種協議,它可以使計算機對其服務器或時鍾源(如石英鍾,GPS等等)做同步化,它可以提供高精准度的時間校正(LAN上與標准間差小於1毫秒,WAN上幾十毫秒),且可介由加密確認的方式來防止惡毒的協議攻擊。NTP可以利用多個途徑和來源的時間服務器來更加精確的校正時間。

現在絕大部分的操作系統都支持NTP,比如在Unix和Windows中都有他們自己的NTP服務。比如在Windows中的SNTP。由於各個系統廠商會根據自己的系統情況對NTP來進行優化和設置,更加適合他自己的系統,對於其他系統的兼容性就會差很多。還有不同時期的系統會使用不同版本的NTP服務。最新版本位V4,由於每個版本都要他自己的一些特征。所以在設置NTP服務器時會有一些兼容性問題。因此,在多類型操作系統的環境中,盡量使用NTP官方組織提供的軟件進行設置而不使用其他廠商在系統中默認提供的NTP服務器。NTP官方組織為 www.ntp.org

2  NTP服務器的運行方式

NTP服務器的使用方式包括,固定連接和動態連接方式。在固定連接中,客戶端僅僅接收指定的服務器端的連接,無論服務端是否正常連接他都不會去換另外的服務器端接收時間。一般客戶/服務器方式為這種模式。另外一直為動態連接,客戶端在不停接收服務器端的廣播信息,當一台服務器停止服務,只要有服務器廣播出來的正確可靠的時間信息,客戶端就可以正常的更新時間。一般為廣播方式連接。

 

服務器/客戶端方式

客戶服務器方式為在互聯網上使用最為廣泛的一種方法,在設定服務器后,他可以靈活的配置客戶端,讓客戶端選擇是否使用NTP服務器或使用那台NTP服務器。客戶服務器方式使用常用的RPC(remote-procedure-call)方式來傳輸時間信息。他們運行的過程為,客戶端發出時間請求給服務器端,然后根據服務器端的答復來調節時間。

一般在客戶端可以定義他們同步時間的間隔,和他們請求最大和最小的時間間隔。客戶端會根據自己時鍾時間和NTP服務器時間進行比對,如果客戶端本機時鍾比較准確,他們查詢的時間間隔就會變長,如果本機時間出現誤差較大,客戶端會在比較短的時間內做出時間請求,來保障客戶端時間的精確度。

 

廣播/多播方式

多播和廣播方式利用在以太網上的廣播和多播協議來進行時間同步,時間服務器會定時發出廣播和多播信息給指定的網段。使用廣播和多播需要網絡設備支持該協議,同時客戶端也支持該協議。可以使用multicasting和manycasting在不同網段進行廣播信息,來達到在包含多個網段的復雜網絡中使用廣播和多播來提供NTP服務。

 

主動/被動方式

主動/被動方式最多使用在集群系統中,由於集群系統對同一集群內的所有服務器的時間同步要求比較嚴格,他們相互配置為NTP服務器端和客戶端,在不停的提供時鍾同步服務,當其中的一台服務器出現停頓時不會影響到其他服務器的時間同步。可以說時互備的方式來進行時間同步服務。

3 NTP配置文件

文件名

默認位置

選項

使用命令

配置文件

/etc/ntp.conf

-c

none

頻率文件

/etc/ntp.drift

-f

driftfile

進程pid文件

None

-p

pidfile

日志文件

system log

-l

logfile

庫文件

None

none

includefile

狀態信息文件

/var/NTP

-s

statsdir

密碼文件

/usr/local/etc

-k

keysdir

 

4 NTP服務器的配置

服務器端配置

在NTP服務器端的默認配置文件為/etc/ntp.conf文件(根據不同OS可能在不同位置,如在Solaris下為 /etc/inet/ntp.conf)。

配置文件的參數:

主要的四個參數:

server address [options ...]

peer address [options ...]

broadcast address [options ...]

manycastclient address [options ...]

這四個參數主要是指定NTP服務器使用的服務器名稱和IP地址以及他們的運行模式。名稱或地址可以是主機名、IP地址和DNS解析后的域名。

Server一般用於服務器端,該服務器不會去同步其他服務器時間,一般server后可以跟  127.127.1.0 該地址代表本級的時鍾,如果Server后跟其他服務器IP,表示會去同步其他時間服務器的地址。

Peer一般配置中表示,服務器接收其他服務器的地址,同時也會為其他設備提供NTP服務器。

Broadcast配置為廣播地址

Manycastclient配置為多播地址

具體解釋如下:

server

For type s and r addresses (only), this command normally mobilizes a persistent client mode association with the specified remote server or local reference clock. If the preempt flag is specified, a preemptable association is mobilized instead. In client mode the client clock can synchronize to the remote server or local reference clock, but the remote server can never be synchronized to the client clock. This command should NOT be used for type b or m addresses.

peer

For type s addresses (only), this command mobilizes a persistent symmetric-active mode association with the specified remote peer. In this mode the local clock can be synchronized to the remote peer or the remote peer can be synchronized to the local clock. This is useful in a network of servers where, depending on various failure scenarios, either the local or remote peer may be the better source of time. This command should NOT be used for type b, m or r addresses.

broadcast

For type b and m addresses (only), this command mobilizes a persistent broadcast mode association. Multiple commands can be used to specify multiple local broadcast interfaces (subnets) and/or multiple multicast groups. Note that local broadcast messages go only to the interface associated with the subnet specified, but multicast messages go to all interfaces.

In broadcast mode the local server sends periodic broadcast messages to a client population at the address specified, which is usually the broadcast address on (one of) the local network(s) or a multicast address assigned to NTP. The IANA has assigned the multicast group address IPv4 224.0.1.1 and IPv6 ff05::101 (site local) exclusively to NTP, but other nonconflicting addresses can be used to contain the messages within administrative boundaries. Ordinarily, this specification applies only to the local server operating as a sender; for operation as a broadcast client, see the broadcastclient or multicastclient commands below.

manycastclient

For type m addresses (only), this command mobilizes a preemptable manycast client mode association for the multicast group address specified. In this mode a specific address must be supplied which matches the address used on the manycastserver command for the designated manycast servers. The NTP multicast address 224.0.1.1 assigned by the IANA should NOT be used, unless specific means are taken to avoid spraying large areas of the Internet with these messages and causing a possibly massive implosion of replies at the sender.

The manycastclient command specifies that the host is to operate in client mode with the remote servers that are discovered as the result of broadcast/multicast messages. The client broadcasts a request message to the group address associated with the specified address and specifically enabled servers respond to these messages. The client selects the servers providing the best time and continues as with the server command. The remaining servers are discarded as if never heard.

 

附加選項參數:

同時還可以添加一下的參數來控制NTP服務

autokey

burst

iburst

key key

minpoll minpoll
maxpoll maxpoll

noselect

preempt

prefer  

true

ttl ttl

version version

dynamic

 

同時NTP服務器還有配置安全,加密,在不同網段等的配置,具體可以參照 www.ntp.org.

客戶端配置

1.      UNIX、Linux服務器的配置方法為:

編輯/etc/ntp.conf(在Solaris中為/etc/inet/ntp.conf)

在文件中加入 server   IPADDRESS

 

2.      在Windows 2000中配置方式:

在命令行模式輸入:

net time /SETSNTP:10.20.0.1  (指定NTP服務器IP)

net stop w32time        (關閉NTP服務)

Net start w32time      (啟動NTP服務)

w32tm –s                (同步NTP時間)

 

3.      在Windows XP中配置為:

控制面板à日期、時間、語言和區域設置à日期和時間

在服務器筐中添加NTP服務器的IP地址就可以了。

5  NTP中的命令

ntpd命令

ntpd命令為NTP服務的配置進程,同時ntpd命令可以配置ntp服務。執行時ntpd從配置文件/etc/ntp.conf中讀取配置信息然后去執行該配置文件中指定的屬性。

ntpd命令的參數選項使用方法:

ntpd [ -46aAbdDgLmnNqx ] [ -c conffile ] [ -f driftfile ] [ -i jaildir ] [ -k keyfile ] [ -l logfile ] [ -p pidfile ] [ -Ppriority ] [ -r broadcastdelay ] [ -s statsdir ] [ -t key ] [ -u user[:group] ] [ -U interface_update_interval ] [ -v variable ] [ -V variable ]

 

Ntpq命令

Ntpq命令是用來查詢ntp服務的命令,可以查詢比較詳細的信息,其中包含參數命令模式和交互命令模式。

ntpq [-inp] [-c command] [host] [...]

 

Ntpdc命令

Ntpdc是一個特殊的ntp查詢命令

ntpdc [ -ilnps ] [ -c command ] [ host ] [ ... ]

 

Ntptrace命令

Ntptrace命令是追蹤ntp服務傳輸信息的命令,可以查看服務的傳輸狀況,查找ntp信息從那個服務器發出以及命令傳輸的過程。

ntptrace [ -vdn ] [ -r retries ] [ -t timeout ] [ server ]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM