① 請教unix伺服器上xntpd服務的一個參數問題
-x, --slew
Slew up to 600 seconds.
Normally, 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 sets the
threshold to 600 s, which is well within the accuracy
window to set the clock manually. Note: Since the slew
rate of typical Unix kernels is limited to 0.5 ms/s,
each second of adjustment requires an amortization
interval of 2000 s. Thus, an adjustment as much as 600
s will take almost 14 days to complete. This option
can be used with the -g and -q options. See the tinker
configuration file directive for other options. Note:
The kernel time discipline is disabled with this
option.
加了-x後,用SLEW的方式調整同步時間,每秒鍾調整0.5ms,調整一秒鍾需要2000秒鍾完成,上面的例子,調整600s需要大概14天完成(Thus, an adjustment as much as 600s will take almost 14 days to complete),即算一下,600x2000=1200000秒=13.89天,即差不多14天。
② NTP執行ntpdate時,報如下錯誤,怎麼解決
在成功配置好NTP服務端後,在NTP客戶端執行ntpdate命令更新NTP客戶端的時間時,如果報以下錯誤:
no server suitable for synchronization found
則可採用以下步驟檢測:
(1) 在NTP服務端執行以下命令檢測NTP服務是否運行
#service ntpd status
(2) 運行ping命令檢測NTP客戶端與NTP服務端是否連通
#ping NTP服務端IP
(3) 在NTP客戶端執行 ntpdate -d NTP服務端IP:
如果輸出結果如下:
6 Nov 10:23:16 ntpdate[3521]: ntpdate [email protected] Tue Nov 18 07:40:49 UTC 2008 (1)
Looking for host 10.75.80.47 and service ntp
host found : 10.75.80.47
transmit(10.75.80.47)
transmit(10.75.80.47)
transmit(10.75.80.47)
transmit(10.75.80.47)
transmit(10.75.80.47)
10.75.80.47: Server dropped: no data
……
……
6 Nov 10:23:20 ntpdate[3521]: no server suitable for synchronization found
如果出現以上情況,請按以下步驟處理:
a) 檢查NTP服務端使用的ntp版本:
#ntpq -c version
如果輸出版本是ntp4.2(包括4.2)之後的版本,則請檢測是否在restrict的定義中使用了notrust。如果有則刪除notrust,再進行NTP時間同步。
b) 檢查NTP服務端的防火牆是否開放NTP服務埠:udp 123
#service iptables stop
執行以上命令關閉NTP服務端的防火牆,然後再進行NTP時間同步,如果成功,則需要修改iptables的設置,以開放NTP服務埠:
#setup #進入配置界面
在配置界面選擇Firewall configuration進入防火牆配置界面:
在防火牆配置界面中選擇Customize進入詳細的配置界面:
按圖中方式加入ntp:udp以運行NTP服務通過防火牆。
9) 如果輸出結果如下:
5 Nov 19:02:27 ntpdate[28566]: ntpdate [email protected] Fri Sep 4 18:54:46 UTC 2009 (1)
Looking for host 10.75.80.55 and service ntp
host found : 10.75.80.55
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
10.75.80.55: Server dropped: strata too high
……
……
5 Nov 19:02:27 ntpdate[28566]: no server suitable for synchronization found
出現以上情況的原因是由於NTP Server還沒有和自身或其他NTP Server保持同步,因此需在ntp的配置文件ntp.conf中加入以下語句以保證NTP Server與自身同步:
server 127.127.1.0
fudge 127.127.1.0 stratum 10
注意:在NTP Server重新啟動NTP服務後,NTP Server自身或與其他NTP
Server的同步大概需要5分鍾左右,因此NTP客戶端在這個時間段運行ntpdate命令時會產生no server suitable for
synchronization found錯誤。
③ NTP執行ntpdate時顯示「錯誤」怎麼解決
NTP執行ntpdate時顯示「錯誤」的解決方法:
no server suitable for synchronization found
則可採用以下步驟檢測:
在NTP服務端執行以下命令檢測NTP服務是否運行
#service ntpd status
運行ping命令檢測NTP客戶端與NTP服務端是否連通
#ping NTP服務端IP
在NTP客戶端執行 ntpdate -d NTP服務端IP:
如果輸出結果如下:
6 Nov 10:23:16 ntpdate[3521]: ntpdate [email protected] Tue Nov 18 07:40:49 UTC 2008 (1)
Looking for host 10.75.80.47 and service ntp
host found : 10.75.80.47
transmit(10.75.80.47)
transmit(10.75.80.47)
transmit(10.75.80.47)
transmit(10.75.80.47)
transmit(10.75.80.47)
10.75.80.47: Server dropped: no data
……
……
6 Nov 10:23:20 ntpdate[3521]: no server suitable for synchronization found
如果出現以上情況,請按以下步驟處理:
檢查NTP服務端使用的ntp版本:
#ntpq -c version
如果輸出版本是ntp4.2(包括4.2)之後的版本,則請檢測是否在restrict的定義中使用了notrust。如果有則刪除notrust,再進行NTP時間同步。
b) 檢查NTP服務端的防火牆是否開放NTP服務埠:udp 123
#service iptables stop
執行以上命令關閉NTP服務端的防火牆,然後再進行NTP時間同步,如果成功,則需要修改iptables的設置,以開放NTP服務埠:
#setup #進入配置界面
在配置界面選擇Firewall configuration進入防火牆配置界面:
在防火牆配置界面中選擇Customize進入詳細的配置界面:
按圖中方式加入ntp:udp以運行NTP服務通過防火牆。
9) 如果輸出結果如下:
5 Nov 19:02:27 ntpdate[28566]: ntpdate [email protected] Fri Sep 4 18:54:46 UTC 2009 (1)
Looking for host 10.75.80.55 and service ntp
host found : 10.75.80.55
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
receive(10.75.80.55)
transmit(10.75.80.55)
10.75.80.55: Server dropped: strata too high
……
……
5 Nov 19:02:27 ntpdate[28566]: no server suitable for synchronization found
出現以上情況的原因是由於NTP Server還沒有和自身或其他NTP Server保持同步,因此需在ntp的配置文件ntp.conf中加入以下語句以保證NTP Server與自身同步:
server 127.127.1.0
fudge 127.127.1.0 stratum 10
注意:在NTP Server重新啟動NTP服務後,NTP Server自身或與其他NTP
Server的同步大概需要5分鍾左右,因此NTP客戶端在這個時間段運行ntpdate命令時會產生no server suitable for
synchronization found錯誤。
gbucks
④ centos ntpd服務啟動後不生效,ntpstat一直顯示unsynchronised
Protocol(NTP)是用來使計算機時間同步化的一種協議,它可以使計算機對其伺服器或時鍾源(如石英鍾,GPS等等)做同步化,它可以提供高精準度的時間校正(LAN上與標准間差小於1毫秒,WAN上幾十毫秒),且可介由加密確認的方式來防止惡毒的協議攻擊。下面介紹下查看ntpserver狀態的兩條命令及其差別。
具體如下:
電腦常見問題解決
1、無法自動識別硬碟控制器
使用非正版的個別操作系統光碟,在安裝系統時,容易出現此錯誤。原因是非正版光碟自動載入的硬碟控制器驅動不符合電腦自身需要的驅動。這種情況就建議換正版光碟安裝操作系統。
2、手動更新錯誤的驅動程序
windows操作系統正常使用,但手動更新驅動程序把硬碟控制器的驅動程序更新錯誤,導致此故障。解決方法是進入windows系統高級菜單,選擇最後一次的正常配置,即可正常進入系統。
3、bios設置變化後所導致
windows操作系統正常,但是由於某些原因,用戶修改了bios設置,導致0x0000007b故障。
⑤ ntpd服務會佔用伺服器多少資源
設置NTP伺服器不難但是NTP本身是一個很復雜的協議. 這里只是簡要地介紹一下實踐方法
和上次一樣,下面的實驗都在RHEL5上運行
1. 時間和時區
如果有人問你說現在幾點? 你看了看錶回答他說晚上8點了. 這樣回答看上去沒有什麼問題,但是如果問你的這個人在歐洲的話那麼你的回答就會讓他很疑惑,因為他那裡還太陽當空呢.
這里就有產生了一個如何定義時間的問題. 因為在地球環繞太陽旋轉的24個小時中,世界各地日出日落的時間是不一樣的.所以我們才有劃分時區(timezone) 的必要,也就是把全球劃分成24個不同的時區. 所以我們可以把時間的定義理解為一個時間的值加上所在地的時區(注意這個所在地可以精確到城市)
地理課上我們都學過格林威治時間(GMT), 它也就是0時區時間. 但是我們在計算機中經常看到的是UTC. 它是Coordinated Universal Time的簡寫. 雖然可以認為UTC和GMT的值相等(誤差相當之小),但是UTC已經被認定為是國際標准,所以我們都應該遵守標准只使用UTC
那麼假如現在中國當地的時間是晚上8點的話,我們可以有下面兩種表示方式
20:00 CST
12:00 UTC
這里的CST是Chinese Standard Time,也就是我們通常所說的北京時間了. 因為中國處在UTC+8時區,依次類推那麼也就是12:00 UTC了.
為什麼要說這些呢(呵呵這里不是地理論壇吧...)
第一,不管通過任何渠道我們想要同步系統的時間,通常提供方只會給出UTC+0的時間值而不會提供時區(因為它不知道你在哪裡).所以當我們設置系統時間的時候,設置好時區是首先要做的工作
第二,很多國家都有夏令時(我記得小時候中國也實行過一次),那就是在一年當中的某一天時鍾撥快一小時(比如從UTC+8一下變成UTC+9了),那麼同理到時候還要再撥慢回來.如果我們設置了正確的時區,當需要改變時間的時候系統就會自動替我們調整
現在我們就來看一下如何在Linux下設置時區,也就是time zone
2. 如何設置Linux Time Zone
在Linux下glibc提供了我們事先編譯好的許多timezone文件, 他們就放在/usr/share/zoneinfo這個目錄下,這里基本涵蓋了大部分的國家和城市
代碼:
# ls -F /usr/share/zoneinfo/
Africa/ Chile/ Factory Iceland Mexico/ posix/ Universal
America/ CST6CDT GB Indian/ Mideast/ posixrules US/
Antarctica/ Cuba GB-Eire Iran MST PRC UTC
Arctic/ EET GMT iso3166.tab MST7MDT PST8PDT WET
Asia/ Egypt GMT0 Israel Navajo right/ W-SU
Atlantic/ Eire GMT-0 Jamaica NZ ROC zone.tab
Australia/ EST GMT+0 Japan NZ-CHAT ROK Zulu
Brazil/ EST5EDT Greenwich Kwajalein Pacific/ Singapore
Canada/ Etc/ Hongkong Libya Poland Turkey
CET Europe/ HST MET Portugal UCT在這裡面我們就可以找到自己所在城市的time zone文件. 那麼如果我們想查看對於每個time zone當前的時間我們可以用zmp命令
代碼:
# zmp Hongkong
Hongkong Fri Jul 6 06:13:57 2007 HKT那麼我們又怎麼來告訴系統我們所在time zone是哪個呢? 方法有很多,這里舉出兩種
第一個就是修改/etc/localtime這個文件,這個文件定義了我么所在的local time zone.
我們可以在/usr/share/zoneinfo下找到我們的time zone文件然後拷貝去到/etc/localtimezone(或者做個symbolic link)
假設我們現在的time zone是BST(也就是英國的夏令時間,UTC+1)
代碼:
# date
Thu Jul 5 23:33:40 BST 2007我們想把time zone換成上海所在的時區就可以這么做
代碼:
# ln -sf /usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime
# date
Fri Jul 6 06:35:52 CST 2007
這樣時區就改過來了(注意時間也做了相應的調整)
第二種方法也就設置TZ環境變數的值. 許多程序和命令都會用到這個變數的值. TZ的值可以有多種格式,最簡單的設置方法就是使用tzselect命令
代碼:
# tzselect
...
TZ='America/Los_Angeles';export TZtzselect
會讓你選擇所在的國家和城市(我省略了這些步驟),最後輸出相應的TZ變數的值.那麼如果你設置了TZ的值之後時區就又會發生變化
代碼:
# date
Thu Jul 5 15:48:11 PDT 2007
通過這兩個例子我們也可以發現TZ變數的值會override /etc/localtime. 也就是說當TZ變數沒有定義的時候系統才使用/etc/localtime來確定time zone. 所以你想永久修改time zone的話那麼可以把TZ變數的設置寫入/etc/profile里
好了現在我們知道怎麼設置時區了,下面我們就來看看如何設置Linux的時間吧
3. Real Time Clock(RTC) and System Clock
說道設置時間這里還要明確另外一個概念就是在一台計算機上我們有兩個時鍾:一個稱之為硬體時間時鍾(RTC),還有一個稱之為系統時鍾(System Clock)
硬體時鍾是指嵌在主板上的特殊的電路, 它的存在就是平時我們關機之後還可以計算時間的原因
系統時鍾就是操作系統的kernel所用來計算時間的時鍾. 它從1970年1月1日00:00:00 UTC時間到目前為止秒數總和的值 在Linux下系統時間在開機的時候會和硬體時間同步(synchronization),之後也就各自獨立運行了
那麼既然兩個時鍾獨自運行,那麼時間久了必然就會產生誤差了,下面我們來看一個例子
代碼:
# date
Fri Jul 6 00:27:13 BST 2007
# hwclock --show
Fri 06 Jul 2007 12:27:17 AM BST -0.968931 seconds
通過hwclock --show 命令我們可以查看機器上的硬體時間(always in local time zone), 我們可以看到它和系統時間還是有一定的誤差的, 那麼我們就需要把他們同步
如果我們想要把硬體時間設置成系統時間我們可以運行以下命令
代碼:
# hwclock --hctosys 反之,我們也可以把系統時間設置成硬體時間
代碼:
# hwclock --systohc 那麼如果想設置硬體時間我們可以開機的時候在BIOS里設定.也可以用hwclock命令
代碼:
# hwclock --set --date="mm/dd/yy hh:mm:ss" 如果想要修改系統時間那麼用date命令就最簡單了
代碼:
# date -s "dd/mm/yyyy hh:mm:ss" 現在我們知道了如何設置系統和硬體的時間. 但問題是如果這兩個時間都不準確了怎麼辦? 那麼我們就需要在互聯網上找到一個可以提供我們准確時間的伺服器然後通過一種協議來同步我們的系統時間,那麼這個協議就是NTP了. 注意接下去我們所要說的同步就都是指系統時間和網路伺服器之間的同步了
4. 設置NTP Server前的准備
其實這個標題應該改為設置"NTP Relay Server"前的准備更加合適. 因為不論我們的計算機配置多好運行時間久了都會產生誤差,所以不足以給互聯網上的其他伺服器做NTP Server. 真正能夠精確地測算時間的還是原子鍾. 但由於原子鍾十分的昂貴,只有少部分組織擁有, 他們連接到計算機之後就成了一台真正的NTP Server. 而我們所要做的就是連接到這些伺服器上同步我們系統的時間,然後把我們自己的伺服器做成NTP Relay Server再給互聯網或者是區域網內的用戶提供同步服務
好了,前面講了一大堆理論,現在我們來動手實踐一下吧. 架設一個NTP Relay Server其實非常簡單,我們先把需要的RPM包裝上
是否已經安裝了NTP包可以用這條命令來確定:
[root@NTPser ~]# rpm -qa | grep ntp
ntp-4.2.2p1-9.el5_4.1
chkfontpath-1.10.1-1.1
出現以上代碼則表示已安裝NTP包,否則用下面方法安裝:
代碼:
# rpm -ivh ntp-4.2.2p1-5.el5.rpm
那麼第一步我們就要找到在互聯網上給我們提供同步服務的NTP Server
http://www.pool.ntp.org是NTP的官方網站,在這上面我們可以找到離我們城市最近的NTP Server. NTP建議我們為了保障時間的准確性,最少找兩個個NTP Server
那麼比如在英國的話就可以選擇下面兩個伺服器
0.uk.pool.ntp.org
1.uk.pool.ntp.org
它的一般格式都是number.country.pool.ntp.org
第二步要做的就是在打開NTP伺服器之前先和這些伺服器做一個同步,使得我們機器的時間盡量接近標准時間.
這里我們可以用ntpdate命令手動更新時間
代碼:
# ntpdate 0.uk.pool.ntp.org
6 Jul 01:21:49 ntpdate[4528]: step time server 213.222.193.35 offset -38908.575181 sec
# ntpdate 0.pool.ntp.org
6 Jul 01:21:56 ntpdate[4530]: adjust time server 213.222.193.35 offset -0.000065 sec
假如你的時間差的很離譜的話第一次會看到調整的幅度比較大,所以保險起見可以運行兩次. 那麼為什麼在打開NTP服務之前先要手動運行同步呢?
1. 因為根據NTP的設置,如果你的系統時間比正確時間要快的話那麼NTP是不會幫你調整的,所以要麼你把時間設置回去,要麼先做一個手動同步
2. 當你的時間設置和NTP伺服器的時間相差很大的時候,NTP會花上較長一段時間進行調整.所以手動同步可以減少這段時間
5. 配置和運行NTP Server
現在我們就來創建NTP的配置文件了, 它就是/etc/ntp.conf. 我們只需要加入上面的NTP Server和一個driftfile就可以了
代碼:
# vi /etc/ntp.conf
server 210.72.145.44 #這是中國國家授時中心的IP
server 0.uk.pool.ntp.org
server 1.uk.pool.ntp.org
fudge 127.127.1.0 stratum 0 stratum 這行是時間伺服器的層次。設為0則為頂級,如果要向別的NTP伺服器更新時間,請不要把它設為0
driftfile /var/lib/ntp/ntp.drift 非常的簡單. 接下來我們就啟動NTP Server,並且設置其在開機後自動運行
代碼:
# /etc/init.d/ntpd start
# chkconfig --level 35 ntpd on
6. 查看NTP服務的運行狀況
現在我們已經啟動了NTP的服務,但是我們的系統時間到底和伺服器同步了沒有呢? 為此NTP提供了一個很好的查看工具: ntpq (NTP query)
我建議大家在打開NTP伺服器後就可以運行ntpq命令來監測伺服器的運行.這里我們可以使用watch命令來查看一段時間內伺服器各項數值的變化
代碼:
# watch ntpq -p
Every 2.0s: ntpq -p Sat Jul 7 00:41:45 2007
remote refid st t when poll reach delay offset jitter
===========================================================
+193.60.199.75 193.62.22.98 2 u 52 64 377 8.578 10.203 289.032
*mozart.musicbox 192.5.41.41 2 u 54 64 377 19.301 -60.218 292.411
現在我就來解釋一下其中的含義
remote: 它指的就是本地機器所連接的遠程NTP伺服器
refid: 它指的是給遠程伺服器(e.g. 193.60.199.75)提供時間同步的伺服器
st: 遠程伺服器的層級別(stratum). 由於NTP是層型結構,有頂端的伺服器,多層的Relay Server再到客戶端. 所以伺服器從高到低級別可以設定為1-16. 為了減緩負荷和網路堵塞,原則上應該避免直接連接到級別為1的伺服器的.
t: 這個.....我也不知道啥意思^_^
when: 我個人把它理解為一個計時器用來告訴我們還有多久本地機器就需要和遠程伺服器進行一次時間同步
poll: 本地機和遠程伺服器多少時間進行一次同步(單位為秒). 在一開始運行NTP的時候這個poll值會比較小,那樣和伺服器同步的頻率也就增加了,可以盡快調整到正確的時間范圍.之後poll值會逐漸增大,同步的頻率也就會相應減小
reach: 這是一個八進制值,用來測試能否和伺服器連接.每成功連接一次它的值就會增加
delay: 從本地機發送同步要求到伺服器的round trip time
offset: 這是個最關鍵的值, 它告訴了我們本地機和伺服器之間的時間差別. offset越接近於0,我們就和伺服器的時間越接近
jitter: 這是一個用來做統計的值. 它統計了在特定個連續的連接數里offset的分布情況. 簡單地說這個數值的絕對值越小我們和伺服器的時間就越精確
那麼大家細心的話就會發現兩個問題: 第一我們連接的是0.uk.pool.ntp.org為什麼和remote server不一樣? 第二那個最前面的+和*都是什麼意思呢?
第一個問題不難理解,因為NTP提供給我們的是一個cluster server所以每次連接的得到的伺服器都有可能是不一樣.同樣這也告訴我們了在指定NTP Server的時候應該使用hostname而不是IP
第二個問題和第一個相關,既然有這么多的伺服器就是為了在發生問題的時候其他的伺服器還可以正常地給我們提供服務.那麼如何知道這些伺服器的狀態呢? 這就是第一個記號會告訴我們的信息
* 它告訴我們遠端的伺服器已經被確認為我們的主NTP Server,我們系統的時間將由這台機器所提供
+ 它將作為輔助的NTP Server和帶有*號的伺服器一起為我們提供同步服務. 當*號伺服器不可用時它就可以接管
- 遠程伺服器被clustering algorithm認為是不合格的NTP Server
x 遠程伺服器不可用
了解這些之後我們就可以實時監測我們系統的時間同步狀況了
7. NTP安全設置
運行一個NTP Server不需要佔用很多的系統資源,所以也不用專門配置獨立的伺服器,就可以給許多client提供時間同步服務, 但是一些基本的安全設置還是很有必要的
那麼這里一個很簡單的思路就是第一我們只允許區域網內一部分的用戶連接到我們的伺服器. 第二個就是這些client不能修改我們伺服器上的時間
關於許可權設定部分
許可權的設定主要以 restrict 這個參數來設定,主要的語法為:
restrict IP地址 mask 子網掩碼 參數
其中 IP 可以是IP地址,也可以是 default ,default 就是指所有的IP
參數有以下幾個:
ignore:關閉所有的 NTP 聯機服務
nomodify:客戶端不能更改服務端的時間參數,但是客戶端可以通過服務端進行網路校時。
notrust :客戶端除非通過認證,否則該客戶端來源將被視為不信任子網
noquery :不提供客戶端的時間查詢
注意:如果參數沒有設定,那就表示該 IP (或子網)沒有任何限制!
在/etc/ntp.conf文件中我們可以用restrict關鍵字來配置上面的要求
首先我們對於默認的client拒絕所有的操作
代碼:
restrict default kod nomodify notrap nopeer noquery
然後允許本機地址一切的操作
代碼:
restrict 127.0.0.1
最後我們允許區域網內所有client連接到這台伺服器同步時間.但是拒絕讓他們修改伺服器上的時間
代碼:
restrict 192.168.1.0 mask 255.255.255.0 nomodify
把這三條加入到/etc/ntp.conf中就完成了我們的簡單配置. NTP還可以用key來做authentication,這里就不詳細介紹了
8. NTP client的設置
做到這里我們已經有了一台自己的Relay Server.如果我們想讓區域網內的其他client都進行時間同步的話那麼我們就都應該照樣再搭建一台Relay Server,然後把所有的client都指向這兩台伺服器(注意不要把所有的client都指向Internet上的伺服器). 只要在client的/etc/ntp.conf加上這你自己的伺服器就可以了
代碼:
server ntp1.leonard.com
server ntp2.leonard.com
LINUX客戶端使用
ntpdate 172.30.218.114
來向NTP伺服器同步自己的時間
其它LINUX如果僅作為只客戶端的話,則不能啟動ntpd服務!否則無法運行ntpdata 伺服器地址 來同步時間
之後可以使用cron或修改crontab文件定期向NTP伺服器更新時間,並用
# hwclock --systohc
將系統時間設置為硬體時間
9. 一些補充和拾遺(挺重要)
1. 配置文件中的driftfile是什麼?
我們每一個system clock的頻率都有小小的誤差,這個就是為什麼機器運行一段時間後會不精確. NTP會自動來監測我們時鍾的誤差值並予以調整.但問題是這是一個冗長的過程,所以它會把記錄下來的誤差先寫入driftfile.這樣即使你重新開機以後之前的計算結果也就不會丟失了
2. 如何同步硬體時鍾?
NTP一般只會同步system clock. 但是如果我們也要同步RTC(hwclock)的話那麼只需要把下面的選項打開就可以了
代碼:
# vi /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes
3、利用crontab讓LINUX NTP定時更新時間
註:讓linux運行ntpdate更新時間時,linux不能開啟NTP服務,否則會提示埠被佔用:如下
[root@ESXI ~]# ntpdate 1.rhel.pool.ntp.org
20 May 09:34:14 ntpdate[6747]: the NTP socket is in use, exiting
crontab文件配置簡要說明
命令格式的前一部分是對時間的設定,後面一部分是要執行的命令。時間的設定我們有一定的約定,前面五個*號代表五個數字,數字的取值范圍和含義如下:
分鍾(0-59)
小時(0-23)
日期(1-31)
月份(1-12)
星期(0-6)//0代表星期天
除了數字還有幾個個特殊的符號就是「*」、「/」和「-」、「,」,「*」代表所有的取值范圍內的數字,「/」代表每的意思,「*/5」表示每5個單位,「-」代表從某個數字到某個數字,「,」分開幾個離散的數字。以下舉幾個例子說明問題:
每天早上6點:
0 6 * * * command
每兩個小時:
0 */2 * * * command
晚上11點到早上8點之間每兩個小時,早上八點:
0 23-7/2,8 * * * command
每個月的4號和每個禮拜的禮拜一到禮拜三的早上11點:
0 11 4 * 1-3 command
1月1日早上4點:
0 4 1 1 * command
3.3、設置開機自動啟動服務
運行setup或其它服務設置工具,將crond服務勾選上
chkconfig --level 2345 crond on 定義在這幾個系統運行級別上啟用crond (系統安裝完默認就是這個設置)
__________________________________________
10.NTP客戶端的設置
一、LINUX做為客戶端自動同步時間
如果想定時進行時間校準,可以使用crond服務來定時執行。
編輯 /etc/crontab 文件
加入下面一行:
30 8 * * * root /usr/sbin/ntpdate 192.168.0.1; /sbin/hwclock -w #192.168.0.1是NTP伺服器的IP地址
然後重啟crond服務
service crond restart
這樣,每天 8:30 Linux 系統就會自動的進行網路時間校準。
二、WINDOWS 需要打開windows time服務和RPC的二個服務
如果在打開windows time 服務,時報 錯誤1058,進行下面操作
1.運行 cmd 進入命令行,然後鍵入
w32tm /register 進行注冊
正確的響應為:W32Time 成功注冊。
2.如果上一步正確,用 net start "windows time" 或 net start w32time 啟動服務。
11.其它造成無法成功更新的原因:
1、客戶端的日期必須要設置正確,不能超出正常時間24小時,不然會因為安全原因被拒絕更新。其次客戶端的時區必須要設置好,以確保不會更新成其它時區的時間。
2、fudge 127.127.1.0 stratum 10 如果是LINUX做為NTP伺服器,stratum(層級)的值不能太大,如果要向上級NTP更新可以設成2
3、LINUX的NTP伺服器必須記得將從上級NTP更新的時間從系統時間寫到硬體里去 hwclock --systohc
NTP一般只會同步system clock. 但是如果我們也要同步RTC(hwclock)的話那麼只需要把下面的選項打開就可以了
代碼:
# vi /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes
4、Linux如果開啟了NTP服務,則不能手動運行ntpdate更新時間(會報埠被佔用),它只能根據/etc/ntp.conf 里server 欄位後的伺服器地址按一定時間間隔自動向上級NTP伺服器更新時間。可以運行命令 ntpstat 查看每次更新間隔如:
[root@ESXI ~]# ntpstat
synchronised to NTP server (210.72.145.44) at stratum 2 #本NTP伺服器層次為2,已向210.72.145.44 NTP同步過
time correct to within 93 ms #時間校正到相差93ms之內
polling server every 1024 s #每1024秒會向上級NTP輪詢更新一次時間
⑥ 請教unix伺服器上ntpd服務的一個參數問題
ntpd -x -- 這里的這個-x代表加一個參數,x 可以是c, f, k, p, P, r, s, t, U等等中的一個字母。
詳細說明,可以看一下ntpd的man page. 如:
-bash-3.00$ man ntpd
Reformatting page. Please Wait... done
System Administration Commands ntpd(1M)
NAME
ntpd - Network Time Protocol daemon Version 4
SYNOPSIS
/usr/lib/inet/ntpd [-46aAbdDgLmnNqvx] [-c conffile]
[-f driftfile] [-k keyfile] [-l logfile] [-p pidfile]
[-P priority] [-r broadcastdelay] [-s statsdir]
[-t trustedkey] [-U interface_update_time]
DESCRIPTION
The ntpd program is an operating system daemon that syn-
chronises the system clock with remote NTP time servers or
local reference clocks. It is a complete implementation of
the Network Time Protocol (NTP) version 4, but also retains
compatibility with version 3, as defined by RFC 1305, and
versions 1 and 2, as defined by RFC 1059 and RFC 1119,
respectively.
。。。
man page中還有對各參數的詳細介紹。
OPTIONS
-4, --ipv4
Force DNS resolution of following host names on the
command line to the IPv4 namespace. Cannot be used with
the --ipv6 option.
-6, --ipv6
Force DNS resolution of following host names on the
command line to the IPv6 namespace. Cannot be used with
the --ipv6 option.
-a, --authreq
Require cryptographic authentication for broadcast
client, multicast client and symmetric passive associa-
tions. This is the default. This option must not
appear with authnoreq option.
-A, --authnoreq
Do not require cryptographic authentication for broad-
cast client, multicast client and symmetric passive
associations. This is almost never a good idea. This
option must not appear with the authreq option.
-b, --bcastsync
Enable the client to sync to broadcast servers.
SunOS 5.10 Last change: 5
System Administration Commands ntpd(1M)
-c string, --configfile=string
The name and path of the configuration file,
/etc/inet/ntp.conf by default.
-d, --debug-level
Increase output debug message level. This option may
appear an unlimited number of times.
-D string, --set-debug-level=string
Set the output debugging level. Can be supplied multi-
ple times, but each overrides the previous value(s).
-f string, --driftfile=string
The name and path of the frequency file,
/var/ntp/ntp.drift by default.
-g, --panicgate
Allow the first adjustment to exceed the panic limit.
Normally, ntpd exits with a message to the system log
if the offset exceeds the panic threshold, which is
1000s by default. This option allows the time to be set
to any value without restriction; however, this can
happen only once. If the threshold is exceeded after
that, ntpd will exit with a message to the system log.
This option can be used with the -q and -x options.
See the tinker configuration file directive for other
options.
-k string, --keyfile=string
Specify the name and path of the symmetric key file.
/etc/inet/ntp.keys is the default.
-l string, --logfile=string
Specify the name and path of the log file. The default
is the system log file.
-L, --novirtualips
Do not listen to virtual IPs. The default is to listen.
-n, --nofork
Do not fork.
-N, --nice
To the extent permitted by the operating system, run
ntpd at the highest priority.
-p string, --pidfile=string
Specify the name and path of the file used to record
ntpd's process ID.
SunOS 5.10 Last change: 6
System Administration Commands ntpd(1M)
-P number, --priority=number
To the extent permitted by the operating system, run
ntpd at the specified sched_setscheler(SCHED_FIFO)
priority.
-q, --quit
Set the time and quit. ntpd will exit just after the
first time the clock is set. This behavior mimics that
of the ntpdate program, which is to be retired. The -g
and -x options can be used with this option. Note: The
kernel time discipline is disabled with this option.
-r string, --propagationdelay=string
Specify the default propagation delay from the
broadcast/multicast server to this client. This is
necessary only if the delay cannot be computed automat-
ically by the protocol.
-s string, --statsdir=string
Specify the directory path for files created by the
statistics facility. This is the same operation as the
statsdir statsdir command.
-t number, --trustedkey=number
Add a key number to the trusted key list. This option
can occur more than once. This is the same operation as
the trustedkey key command.
-U number, --updateinterval=number
interval in seconds between scans for new or dropped
interfaces. This option takes an integer number as its
argument.
Give the time in seconds between two scans for new or
dropped interfaces. For systems with routing socket
support the scans will be performed shortly after the
interface change has been detected by the system. Use
0 to disable scanning. 60 seconds is the minimum time
between scans.
--var=nvar
make ARG an ntp variable (RW). This option may appear
an unlimited number of times.
--dvar=ndvar
make ARG an ntp variable (RW|DEF). This option may
appear an unlimited number of times.
-x, --slew
Slew up to 600 seconds.
SunOS 5.10 Last change: 7
System Administration Commands ntpd(1M)
Normally, 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 sets the
threshold to 600 s, which is well within the accuracy
window to set the clock manually. Note: Since the slew
rate of typical Unix kernels is limited to 0.5 ms/s,
each second of adjustment requires an amortization
interval of 2000 s. Thus, an adjustment as much as 600
s will take almost 14 days to complete. This option
can be used with the -g and -q options. See the tinker
configuration file directive for other options. Note:
The kernel time discipline is disabled with this
option.
-?, --help
Display usage information and exit.
-!, --more-help
Extended usage information passed thru pager.
--version
Output version of program and exit.