ntpd和ntpdate
以無GUI 的Ubuntu 為例
1. ntpdate
ntpdate是Ubuntu發(fā)行版的標(biāo)配。按Ubuntu14.04的官方文檔(這里),Ubuntu在啟動(dòng)時(shí),會(huì)調(diào)用ntpdate,根據(jù)Ubuntu NTP server更新系統(tǒng)時(shí)間,但我實(shí)際測(cè)試卻并不成功。
注:測(cè)試方法:sudo sysv-rc-conf npt off -> 修改為一個(gè)過去的時(shí)間sudo date -s hh:mm:ss ,并用date檢查是否已修改-> 同步到硬件sudo hwclock -w ->
sync;sync;sync;sudo reboot ->開機(jī)后等待10分鐘,發(fā)現(xiàn)date還是錯(cuò)誤時(shí)間。
所以,對(duì)于客戶端,那就在crontab中增加一個(gè)任務(wù),如下,其中weblbserver-1是ntp服務(wù)器,如果你想出錯(cuò)時(shí)(比如weblbserver無法連接)發(fā)郵件,那要設(shè)置MAILTO=yourmail,
并且還要安裝postfix,安裝也非常簡(jiǎn)單,直接用apt-get install就行,安裝完無需任何配置。
*/10 * * * * root (ntpdate weblbserver-1 && hwclock -w) > /dev/null
如果開啟了nptd服務(wù),那就無法執(zhí)行ntpdate了,如下
zhj@test:~$ sudo service ntp status * NTP server is running zhj@test:~$ zhj@test:~$ sudo ntpdate pool.ntp.org 29 Dec 12:55:26 ntpdate[2964]: the NTP socket is in use, exiting zhj@test:~$
2. ntpd
下面是自己的ntp server——weblbserver-1的配置文件/etc/ntp.conf,它是集群中的一臺(tái)服務(wù)器,它允許集群內(nèi)的其它服務(wù)器從它這里獲取時(shí)間。
該配置文件修改后,要調(diào)用sudo service ntp reload重新加載。另外,注意在ntp server上要在防火墻中放行UDP 123號(hào)端口
ntpd啟動(dòng)后,要等4*2^(minpoll ~ maxpoll) 秒才能真正開始同步,所以minpoll和maxpoll不要設(shè)置的太大。
配置文件參數(shù)含義可參考http://wenku.baidu.com/view/e3282cc62cc58bd63186bd41.html
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help driftfile /var/lib/ntp/ntp.drift # Enable this if you want statistics to be logged. #statsdir /var/log/ntpstats/ statistics loopstats peerstats clockstats filegen loopstats file loopstats type day enable filegen peerstats file peerstats type day enable filegen clockstats file clockstats type day enable # Specify one or more NTP servers. # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for # more information.
# 參數(shù)minpoll和maxpoll都是2的冪,下面即設(shè)置2^9=512秒同步一次 server 0.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 server 1.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 server 2.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 server 3.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions> # might also be helpful. # # Note that "restrict" applies to both servers and clients, so a configuration # that might be intended to block requests from certain clients could also end # up blocking replies from your own upstream servers. # By default, exchange time with everybody, but don't allow configuration. restrict -4 default kod notrap nomodify nopeer noquery restrict -6 default kod notrap nomodify nopeer noquery # Local users may interrogate the ntp server more closely. restrict 127.0.0.1 restrict ::1 # Clients from this (example!) subnet have unlimited access, but only if # cryptographically authenticated.
# 放行局域網(wǎng)內(nèi)的主機(jī),可以從本機(jī)獲取ntp時(shí)間,但不能修改本機(jī)的時(shí)間 restrict 192.168.0.0 mask 255.255.0.0 nomodify
同時(shí),在本ntp server主機(jī)增加一個(gè)定時(shí)任務(wù),每小時(shí)將osclock刷入hwclock,如下,如果想發(fā)郵件,要設(shè)置MAILTO,并安裝postfix
*/10 * * * * root hwclock -w > /dev/null
我們可以用ntpq -p來查看本機(jī)與上層NTP server的時(shí)間同步情況,如下
zhj@weblbserver-1:~$ ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
dns1.synet.edu. 202.118.1.46 2 u 915 1024 1 43.464 -134.79 0.004
golem.canonical 192.93.2.20 2 u 914 1024 1 433.083 -198.90 0.004
zhj@weblbserver-1:~$
remote ——亦即 NTP 主機(jī)的 IP 或主機(jī)名,注意最左邊的符號(hào)
如果有『 * 』代表目前正在作用當(dāng)中的上層 NTP
如果是『 + 』代表也有連上線,而且可作為下一個(gè)提供時(shí)間更新的候選者
如果是空格,那代表還沒有連接
refid ——參考的上一層 NTP 主機(jī)的地址
st ——就是 stratum 階層,ntp允許的層數(shù)為1~15
when —— 上次同步時(shí)間到現(xiàn)在的距離,單位是秒。當(dāng)然,如果ntp服務(wù)是剛啟動(dòng),還沒同步過,那這個(gè)參數(shù)就是ntp服務(wù)啟動(dòng)后到現(xiàn)在的距離
poll —— 同步周期,單位為秒
reach —— 它是八進(jìn)制數(shù),正常情況下值為[0, 1, 3, 7, 17, 37, 77, 177, 377],對(duì)應(yīng)的二進(jìn)制為[0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111],
ntp服務(wù)啟動(dòng)后,reach就以poll值為周期與ntp server通信,為了方便理解,我們可以簡(jiǎn)單的認(rèn)為每次ping一下上層ntp server,如果成功,那
reach就向左移一位,右邊補(bǔ)1,如果失敗,則右邊補(bǔ)0,所以如果reach不是上面給出的枚舉值,那就是在通信過程中出錯(cuò)了。當(dāng)reach 達(dá)到17時(shí)
(對(duì)應(yīng)1111,即最近的四次通信都成功了),那才開始同步時(shí)間,這時(shí),remote項(xiàng)對(duì)應(yīng)的域名或IP列表有,其中一個(gè)前面會(huì)有*號(hào),表示該IP就是
NTP server。而在開始同步時(shí)間之前,當(dāng)客戶端訪問weblbserver-1這個(gè)NTP server時(shí),都會(huì)出現(xiàn)stratum 16,no server suitable for
nchronizationfound這樣的錯(cuò)誤。也就是說如果你在NTP server主機(jī)上重啟了ntp服務(wù),那要等4*poll秒(在前四次通信都是成功的前提下),
該NTP server才與上層NTPserver開始同步時(shí)間,而且只有當(dāng)開始同步時(shí),該NTP server才能為其它客戶端提供NTP服務(wù)。因此,你在/etc/ntp.conf
中設(shè)置的同步周期minpoll maxpoll不能太大,因?yàn)槊看蝞tp服務(wù)重啟后,要等4倍長(zhǎng)的時(shí)間才能開始同步。
offset —— NTP server與本機(jī)的時(shí)間差,單位為毫秒,即10^(-3) 秒。如果這個(gè)值比較大,那就要考慮縮短同步周期了
delay —— 網(wǎng)絡(luò)傳輸過程當(dāng)中延遲的時(shí)間,單位為 10^(-6) 秒
jitter ——Linux 系統(tǒng)時(shí)間與 BIOS 硬件時(shí)間的差異時(shí)間, 單位為 10^(-6) 秒
總結(jié)
以上是生活随笔為你收集整理的ntpd和ntpdate的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu apt报错无法获得锁/va
- 下一篇: python数字1 3怎么表示_Pyth