CentOS下部署NTP服務器

原文出處: http://viong.blog.51cto.com/844766/522799

安裝NTP

wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.6p3.tar.gz
tar ntp-4.2.6p3.tar.gz
cd  ntp-4.2.6p3
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
make 
make install

cp /usr/local/ntp/bin/* /usr/bin/

 

修改ntp.conf配置文件


首先先了解一下ntp.conf裏面涉及到參數說明

關於權限設定部分; 權限的設定主要以 restrict 這個參數來設定
格式如下:
restrict IP地址 mask 子網掩碼 參數; 其中 IP 可以是IP地址,也可以是 default ,default
就是指所有的IP參數有以下幾個:

ignore :關閉所有的 NTP 聯機服務

nomodify:客戶端不能更改服務端的時間參數,但是客戶端可以通過服務端進行網絡校時

notrust:客戶端除非通過認證,否則該客戶端來源將被視爲不信任子網
 
noquery :不提供客戶端的時間查詢

注意:如果參數沒有設定,那就表示該 IP (或子網)沒有任何限制
 
用server這個參數設定上級時間服務器

格式如下: server IP地址或域名 [prefer]
IP地址或域名就是我們指定的上級時間服務器,如果 Server 參數最後加上 prefer,表示我們的 NTP
服務器主要以該部主機時間進行校準

driftfile格式如下:driftfile 文件名

在與上級時間服務器聯繫時所花費的時間,記錄在driftfile參數後面的文件內 注意: driftfile
後面接的文件需要使用完整的路徑文件名,不能是鏈接文件,並且文件的權限需要設定成 ntpd守護進程可以寫入。


vi /etc/ntp.conf

1.允許任何IP的客戶機都可以進行時間同步

將“restrict default kod nomodify notrap nopeer noquery”這行修改成:

restrict default nomodify


2.只允許192.168.0.0網段的客戶機進行時間同步

restrict 192.168.0.0 mask 255.255.0.0 nomodify


很奇怪的是安裝完後發現沒有ntp.conf,就只能手動添加,touch /etc/ntp.conf,複製以下

==============================================
restrict default nomodify notrap noquery

restrict 127.0.0.1
restrict 192.168.0.0 mask 255.255.0.0 nomodify

server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org

server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

driftfile /var/lib/ntp/drift
broadcastdelay  0.008
keys            /etc/ntp/keys 

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


讓本服務器時間與time.nist.gov時間同步,使服務器爲標準時間
[root@localhost ~]# ntpdate time.nist.gov


以守護進程啓動ntpd

[root@localhost ~]#/usr/local/ntp/bin/ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid

[root@localhost ~]#netstat -nul |grep 123
udp        0      0 192.168.50.91:123           0.0.0.0:*                               
udp        0      0 127.0.0.1:123               0.0.0.0:*                               
udp        0      0 0.0.0.0:123                 0.0.0.0:*

[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 114.80.81.1     204.152.184.72   2 u    5   64    1   13.142  -287655   0.001
 218.75.4.130    216.218.192.202  2 u    4   64    1   15.109  -287655   0.001
*LOCAL(0)        .LOCL.          10 l    3   64    1    0.000    0.000   0.001


安全設置,開放192.168.0.0訪問本機123端口
/sbin/iptables -A INPUT -p UDP -i eth0 -s 192.168.0.0/24  --dport 123 -j ACCEPT

 


linux客戶端設置NTP

目前 Linux 系統上面有兩個時間,一個是 Linux 系統,另一個則是 BIOS 時間(真正的硬件記錄的時間)!

我們可以使用 date 這個指令來手動修正目前主機的時間,date 這個指令僅修正 Linux 時間而已,所以需要以 hwclock 這個指令來將 BIOS 時間也更新才行

[root@test root]# date MMDDhhmmYYYY 
MM:月份 
DD:日期 
hh:小時 
mm:分鐘 
YYYY:公元年

  
[root@test root]# hwclock [-rw]

-r:檢視目前的 BIOS 時間 
-w:將目前 Linux 的時間寫入 BIOS 當中!

[root@localhost ~]# date ; hwclock -r 
2011年 03月 23日 星期三 01:03:59 CST
2011年03月23日 星期三 03時05分00秒  -0.914570 seconds

#  date 與 hwclock -r 所顯示的時間是『不一致的』! 
# 因爲 Linux 時間與 BIOS 時間不一致所導致的一個問題! 
# 需要以 hwclock -w 來將 Linux 時間寫入 BIOS

[root@localhost ~]# hwclock -w 
[root@localhost ~]# date ; hwclock -r 
2011年 03月 23日 星期三 01:03:59 CST
2011年03月23日 星期三 01時04分00秒  -0.814770 seconds


#自動同步服務器時鐘
[root@localhost ~]# crontab -e
# 加入這一行: 

10 5 * * * root /usr/sbin/ntpdate 192.168.50.91;/sbin/hwclock -w

[root@localhost ~]# service crond restart


windows客戶端設置NTP

直接在控制面板--日期和時間  修改

也可以下載以下軟件修改
http://www.stdtime.gov.tw/chinese/exe/NTPClock.exe 

以下是網上覆制過來當備用,因爲在域環境下NTP腳本同步問題還是需要命令行

Windows Server 2003


3.1 命令介紹
Windows Server 2003下有關時間的命令有兩個,net time和W32tm,下面對這兩個命令的語法和參數加以說明。

3.1.1 NET TIME
net time命令使計算機的時鐘與另一個計算機或域的時鐘同步。如果在沒有 /set 選項的情況下使用,則顯示另一個計算機或域的時間。net time的語法爲:

net time [\computername | /domain[:domainname] | /rtsdomain[:domainname]] [/set]

net time [\computername] [/querysntp] | [/setsntp[:ntp server list]]
 

參數說明:

l 無參數,顯示被指派爲本地計算機的Windows服務器域時間服務器的當前時間。

l \computername,指定要檢查或與之同步的服務器的名稱。

l /domain[:domainname],指定要同步時間的域。

l /rtsdomain[:domainname],指定要與之同步的可信時間服務器所在的域。

l /set,使計算機的時鐘與指定的計算機或域的時間同步。

l /querysntp,顯示當前爲本地計算機或 \computername 所指定的計算機配置網絡時間協議(NTP)服務器的名稱。

l /setsntp[:ntp server list],指定本地計算機所使用的NTP時間服務器的列表。該列表可以包含IP地址或DNS名稱,用空格分開。如果要使用多個時間服務器,該列表必須用引號引起來。參見注冊表項中NtpServer項說明。

3.1.2 W32time
W32tm是用來同步本地計算機與遠程計算機或域時間的類似命令。在域控制器上使用W32tm前必須停止W32Time,完成後再啓動W32Time。W32tm的語法爲:

w32tm [/? | /register | /unregister ]

w32tm /monitor [/domain:<domain name>]

[/computers:<name>[,<name>[,<name>...]]]

[/threads:<num>]

w32tm /ntte <NT time epoch>

w32tm /ntpte <NTP time epoch>

w32tm /resync [/computer:<computer>] [/nowait] [/rediscover] [/soft]

w32tm /stripchart /computer:<target> [/period:<refresh>]

[/dataonly] [/samples:<count>]

w32tm /config [/computer:<target>] [/update]

[/manualpeerlist:<peers>] [/syncfromflags:<source>]

[/LocalClockDispersion:<seconds>]

w32tm /tz

w32tm /dumpreg [/subkey:<key>] [/computer:<target>]
 

關於W32tm語法的詳細說明,可以在命令提示符下鍵入W32tm /?,將列出語法和所有參數的說明。

3.2 註冊表項
以下註冊表項位於 HKLM\SYSTEM\CurrentControlSet\Services\W32Time\

註冊表項
 MaxPosPhaseCorrection
 
路徑
 HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config
 
注意
 該 項指定服務可進行的最大正時間校準量(以秒爲單位)。如果服務確定某個更改幅度大於所需的幅度,它將記錄一個事件。特殊情況:0xFFFFFFFF 表示總是校準時間。域成員的默認值是 0xFFFFFFFF。獨立客戶端和服務器的默認值是 54,000(15 小時)。
 
註冊表項
 MaxNegPhaseCorrection
 
路徑
 HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config
 
注意
 該項指定服務可進行的最大負時間校準量(以秒爲單位)。如果服務確定某個更改幅度大於所需的幅度,它將轉而記錄一個事件。特殊情況:-1 表示總是校準時間。域成員的默認值是 0xFFFFFFFF。獨立客戶端和服務器的默認值是 54,000(15 小時)。
 
註冊表項
 MaxPollInterval
 
路徑
 HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config
 
注意
 該項指定系統輪詢間隔所允許的最大間隔(單位是對數表示的秒)。請注意,儘管系統必須根據預定的間隔進行輪詢,但是提供程序可以根據請求拒絕生成示例。域成員的默認值是 10。獨立客戶端和服務器的默認值是 15。
 
註冊表項
 SpecialPollInterval
 
路徑
 HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient
 
注意
 該項指定手動對等端的特殊輪詢間隔(以秒爲單位)。當啓用 SpecialInterval 0x1 標誌時,W32Time 將使用此輪詢間隔而非操作系統確定的輪詢間隔。域成員的默認值是 3,600。獨立客戶端和服務器的默認值是 604,800。
 
註冊表項
 MaxAllowedPhaseOffset
 
路徑
 HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Config
 
注意
 該項指定 W32Time 嘗試使用時鐘速率調整計算機時鐘的最大偏移(以秒爲單位)。當偏移大於該速率時,W32Time 將直接設置計算機時鐘。域成員的默認值是 300。獨立客戶端和服務器的默認值是 1。
 

3.2.1 Parameters\NtpServer
HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parameters\NtpServer

Data type
 Range
 Default value
 
REG_SZ
 DNS name or IP address[,0x1 | 0x2 | 0x3]
 time.windows.com,0x1
 

Description

Stores a list of time sources that this computer uses to synchronize its time. The time sources appear either as a DNS name or an IP address of the time source server. The list is space-delimited, with one address per line.

Each time source may also be followed by a comma and a DWORD containing flags for special handling of that time source. If the flags are not specified, they default to 0x0. The available flags are:

Value
 Meaning
 
0x1
 Instead of following the NTP specification, wait for the interval specified in the SpecialPollInterval entry before attempting to recontact this time source. Setting this flag decreases network usage, but it also decreases accuracy.
 
0x2
 Use this time source only as a fallback. If all time sources that are not fallbacks have failed, then the system selects one fallback time source at random and uses it.
 
0x4
 Set the local computer to operate in symmetric active mode in the association with this source.
 
0x8
 Set the local computer to operate in client mode in the association with this source.
 

例如net time /setsntp:"time.nist.gov,0x1 time-a.nist.gov,0x1”,其中的0x1是在使用完整域名稱作爲NTP服務器的時候需要的,如果使用IP地址則可以忽略。

Change Method

You can change the value of this entry by using the /setsntp parameter of the net time command at the command line. For more information about net time, see Help and Support Center for Windows Server 2003.

3.2.2 Parameters\Type
HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parameters

Data type
 Range
 Default value
 
REG_SZ
 NoSync | NTP | Nt5DS | AllSync
 Nt5DS for computers joined to a domain; NTP for others
 

Description

Specifies the type of time sources, if any, to which the computer synchronizes its time.

Value
 Meaning
 
NoSync
 Does not synchronize time.
 
NTP
 Synchronizes time to the time sources specified in the Parameters\NtpServer entry.
 
Nt5DS
 Synchronizes time to the domain hierarchy.
 
AllSync
 Uses all synchronization mechanisms available.
 

Change Method

To change the value of this entry, use the command-line tool w32tm.exe (specifically, the command w32tm /config /syncfromflags). For more information about w32tm.exe, see Windows Server 2003 Help and Support Center.

Activation Method

To make changes to this entry effective, at the command line, type:

w32tm /config /update

3.3 設置示例
3.3.1 使用“時間和日期 屬性”設置對話框
雙擊任務欄右下角“時間”,打開“時間和日期 屬性”設置對話框;

選擇“Internet時間”標籤;

選中“自動與Internet時間服務器同步”選項,在“服務器”中填入網絡時間服務器的IP地址,如“192.168.1.232”(只能輸入1個服務器地址)。按“立即更新”可直接同步。

3.3.2 命令方式
在“運行(R)”中用“cmd”命令進入在DOS命令行窗體;執行以下命令(注意空格):

net time /setsntp: ”192.168.1.232 time.windows.com,0x1”

net stop w32time

net start w32time

w32tm /resync /nowait /rediscover
 (設置時間服務器地址,可多個)

(停止時間服務)

(啓動時間服務)

(立即同步時間)
 

3.4 可能出現的問題
計算機沒有同步。因爲沒有可用的時間數據。(The computer did not resync because no time data was available)

出現該問題的原因可能是windows time service沒有正確的啓動,或者與組策略中的時間配置衝突。請在組策略(gpedit.msc)中將配置更改爲“未配置”。


參考文檔:http://linux-vbird.bluedata.org/linux_server/0440ntp.htm#server_startandlook



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