產(chǎn)品搜索
請(qǐng)輸入產(chǎn)品關(guān)鍵字:
聯(lián)系方式
地址:上海市翔殷路128號(hào)
郵編:
聯(lián)系人:魏元棟
電話:86-021-51816731
傳真:86-021-51816733
手機(jī):13918107979
售后電話:4000210499
留言:發(fā)送留言
個(gè)性化:www.ruichengdianqi.com
手機(jī)站:m.ruichengdianqi.com
網(wǎng)址:www.ruichengdianqi.com
商鋪:http://www.hbwxwy.cn/st163316/
郵編:
聯(lián)系人:魏元棟
電話:86-021-51816731
傳真:86-021-51816733
手機(jī):13918107979
售后電話:4000210499
留言:發(fā)送留言
個(gè)性化:www.ruichengdianqi.com
手機(jī)站:m.ruichengdianqi.com
網(wǎng)址:www.ruichengdianqi.com
商鋪:http://www.hbwxwy.cn/st163316/
技術(shù)文章
如何快速搭建Time Server
點(diǎn)擊次數(shù):5018 發(fā)布時(shí)間:2011-3-29
春去春來(lái),一轉(zhuǎn)眼在CU已經(jīng)混了一年了。這一年來(lái),我在CU學(xué)了很多很多,感謝CU,感謝大家的支持和關(guān)愛。
我是做技術(shù)的,思來(lái)想去還是寫一篇技術(shù)方面的文檔,來(lái)紀(jì)念一下這個(gè)特別的日子吧。閑言少敘,言歸正傳。
一.Time Server的搭建(在此我們使用ntp:Network Time Protocol)
Time Server即時(shí)間服務(wù)器。我們經(jīng)常會(huì)發(fā)現(xiàn)服務(wù)器上的時(shí)間不正確,這樣會(huì)影響到我們的應(yīng)用,有時(shí)甚至?xí)?lái)一些不良后果。比如影響我們的備份,影響我們的數(shù)據(jù)庫(kù)的更新等。比較常見的解決方法是與公網(wǎng)上的時(shí)間服務(wù)器同步(只用使用crontab執(zhí)行00 02 * * * /usr/sbin/ntpdate 195.13.1.153就行了,后面的195.13.1.153就是公網(wǎng)上可以用的時(shí)間服務(wù)器之一),但這樣做計(jì)算機(jī)必須能連接公網(wǎng),這樣也許會(huì)有一些安全問(wèn)題。并且如果你的計(jì)算機(jī)在內(nèi)網(wǎng)不能直接連接公網(wǎng)的時(shí)間服務(wù)器的話,那就……今天我們就來(lái)講一講搭建我們自己的時(shí)間服務(wù)器的做法。
方法一:時(shí)間服務(wù)器與公網(wǎng)上的時(shí)間服務(wù)器同步,其它機(jī)器與自己的時(shí)間服務(wù)器同步。缺點(diǎn):時(shí)間服務(wù)器還要暴露在公網(wǎng)上。
步驟:
1.檢查是否有相應(yīng)的包
代碼:
# rpm -qa |grep ntp
ntp-4.1.1a-9
chkfontpath-1.9.6-3
如果沒有ntp這個(gè)包,則從光盤上裝上。執(zhí)行下面命令安裝NTP的RPM包:
# rpm -ivh ntp-4.1.1a-9.i386.rpm(我使用的是redhat8.0,請(qǐng)根據(jù)你的實(shí)際情況安裝對(duì)應(yīng)的包)
2.修改配置文件
/etc/ntp.conf是ntp的主要配置文件,里面設(shè)置了你用來(lái)同步時(shí)間的時(shí)間服務(wù)器的域名或者IP地址,下面是到互聯(lián)網(wǎng)同步時(shí)間的zui基本的配置:
首先我們來(lái)定義自己喜歡的時(shí)間服務(wù)器:(可用的時(shí)間服務(wù)器,參看http://chinaunix.net/jh/5/100591.html,或者參看:http://www.eecis.udel.edu/~mills/ntp/servers.html)
代碼:
server 195.13.1.153
server 194.137.39.67
接下來(lái),我們?cè)O(shè)置上面兩臺(tái)服務(wù)器的訪問(wèn)權(quán)限,在這個(gè)例子中我們不允許它們修改或者查詢我們配置在Linux上的NTP服務(wù)器。
代碼:
restrict 195.13.1.153 mask 255.255.255.255 nomodify notrap noquery
restrict 194.137.39.67 mask 255.255.255.255 nomodify notrap noquery
說(shuō)明:掩碼255.255.255.255是用來(lái)限制遠(yuǎn)程N(yùn)TP服務(wù)器的掩碼地址。
然后設(shè)置允許訪問(wèn)的內(nèi)網(wǎng)機(jī)器。請(qǐng)注意,配置中noquery已經(jīng)去掉了:
代碼:
restrict 192.168.1.0 mask 255.255.255.0 notrust nomodify notrap
在此例中,掩碼地址擴(kuò)展為255,因此從192.168.1.1-192.168.1.254的計(jì)算機(jī)都可以使用我們的NTP服務(wù)器來(lái)同步時(shí)間。如果你想限制的更嚴(yán)格一點(diǎn),你可以修改你的掩碼。
zui后,也是zui重要的是默認(rèn)的限制配置要從你配置文件中刪除,否則它將覆蓋你所有的配置選項(xiàng),你會(huì)發(fā)現(xiàn)如果不刪除該配置,你的時(shí)間服務(wù)器將只能和自己通訊。如果ntp.conf中有以下一行,請(qǐng)將它注釋:
代碼:
# restrict default ignore
3.檢查可用性
a.保存你的配置文件,然后對(duì)每個(gè)你在ntp.conf里配置的時(shí)間服務(wù)器執(zhí)一下查詢命令,確保這些上游時(shí)間服務(wù)器可用。
代碼:
# ntpdate 195.13.1.153
27 Jun 10:12:01 ntpdate[25475]: adjust time server 133.100.11.8 offset -0.127154 sec
# ntpdate 194.137.39.67
27 Jun 10:12:06 ntpdate[25478]: adjust time server 133.100.9.2 offset 0.010008 sec
b.執(zhí)行下列命令
# ntpq -p
…… ……輸出略
一個(gè)可以證明同步問(wèn)題的證據(jù)是所有遠(yuǎn)程服務(wù)器的jitter值是4000并且delay和reach的值是0。
代碼:
remote refid st t when poll reach delay offset jitter
==============================================================================
LOCAL(0) LOCAL(0) 10 l - 64 7 0.000 0.000 0.008
* 0.0.0.0 16 u - 64 0 0.000 0.000 4000.00
4.設(shè)置自啟動(dòng)
為了使NTP服務(wù)可以在系統(tǒng)引導(dǎo)的時(shí)候自動(dòng)啟動(dòng),執(zhí)行:
代碼:
# chkconfig ntpd on
啟動(dòng)/關(guān)閉/重啟NTP的命令是:
代碼:
# /etc/init.d/ntpd start
# /etc/init.d/ntpd stop
# /etc/init.d/ntpd restart
5.客戶端的設(shè)置
a.linux客戶端
以root身份登錄,執(zhí)行crontab -e輸入00 02 * * * /usr/sbin/ntpdate 192.168.1.1(換成你的Time Server的ip)
這樣就會(huì)在每天的凌晨?jī)牲c(diǎn)自動(dòng)與Time Server同步時(shí)間。
b.windows Xp客戶端
雙擊右下角的時(shí)間,出現(xiàn)“日期和時(shí)間屬性”的窗口,選擇Internet 時(shí)間,在服務(wù)器一欄中輸入你的Time Server的ip,點(diǎn)擊"立即更新",過(guò)幾秒鐘將能看到更新成功的提示。然后勾選“自動(dòng)與Internet時(shí)間服務(wù)器同步”。點(diǎn)擊確定。
方法二:時(shí)間服務(wù)器與自己的硬件時(shí)鐘同步,其它機(jī)器與時(shí)間服務(wù)器同步。缺點(diǎn):如果Time Server的硬件時(shí)鐘不準(zhǔn)確,則所有的時(shí)間將不準(zhǔn)確。優(yōu)點(diǎn):更安全,沒有暴露在公網(wǎng)上的機(jī)器。
unix類系統(tǒng)的時(shí)鐘都有兩種,一種是硬件時(shí)鐘,一種是系統(tǒng)時(shí)鐘。在此不在詳述。步驟如下:
1.校準(zhǔn)Time server的硬件時(shí)鐘(可以直接在bios中設(shè)置),或者用hwclock命令來(lái)校對(duì),例如: hwclock --set --date="6/16/04 11:14:05"
2.設(shè)置系統(tǒng)時(shí)間和硬件時(shí)鐘同步:
輸入:hwclock --hctosys.
3.修改配置文件
vi /etc/ntp.conf,我的ntp.conf如下
代碼:
# Prohibit general access to this service.
# restrict default ignore
# 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
# -- CLIENT NETWORK -------
# Permit systems on this network to synchronize with this
# time service. Do not permit those systems to modify the
# configuration of this service. Also, do not use those
# systems as peers for synchronization.
# restrict 192.168.1.0 mask 255.255.255.0 notrust nomodify notrap
# --- OUR TIMESERVERS -----
# or remove the default restrict line
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
# restrict mytrustedtimeserverip mask 255.255.255.255 nomodify notrap noquery
# --- NTP MULTICASTCLIENT ---
#multicastclient # listen on default 224.0.1.1
# restrict 224.0.1.1 mask 255.255.255.255 notrust nomodify notrap
# restrict 192.168.1.0 mask 255.255.255.0 notrust nomodify notrap
restrict 192.168.1.0 mask 255.255.255.0 notrust nomodify notrap
# --- GENERAL CONFIGURATION ---
#
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. The
# default stratum is usually 3, but in this case we elect to use stratum
# 0. Since the server line does not have the prefer keyword, this driver
# is never used for synchronization, unless no other other
# synchronization source is available. In case the local host is
# controlled by some external source, such as an external oscillator or
# another protocol, the prefer keyword would cause the local host to
# disregard all other synchronization sources, unless the kernel
# modifications are in use and declare an unsynchronized condition
#
#server 127.127.1.0 # local clock
server 127.127.1.0 prefer
fudge 127.127.1.0 stratum 10
#
# Drift file. Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
#
driftfile /etc/ntp/drift
broadcastdelay 0.008
#
# Authentication delay. If you use, or plan to use someday, the
# authentication facility you should make the programs in the auth_stuff
# directory and figure out what this number should be on your machine.
#
authenticate yes
#
# Keys file. If you want to diddle your server at run time, make a
# keys file (mode 600 for sure) and define the key number to be
# used for making requests.
#
# PLEASE DO NOT USE THE DEFAULT VALUES HERE. Pick your own, or remote
# systems might be able to reset your clock at will. Note also that
# ntpd is started with a -A flag, disabling authentication, that
# will have to be removed as well.
#
keys /etc/ntp/keys
其它設(shè)置和方法一相同,啟動(dòng)ntp服務(wù),配置客戶端即可。