文章目錄
- 安裝NTP Server
- 配置
- 啟動 NTP 伺服器
- Linux用戶端機器配置
- Windows用戶端機器配置
NTP(The Network Time Protocol) 是網路時間協議,用以同步網路內電腦的時間。
它通過udp包交換,用特定演算法進行協商,從而把電腦上的時間與時間伺服器上的
時間保持一致。通過互連網它支援的誤差是10毫秒,區域網路則可以達到200微秒。
NTP時間伺服器分為多層,從0層到4層,每層依次與上一次伺服器同步,最高層的伺服器
則直接連接一個高精度的時鐘裝置,比如原子鐘、GPS或者電波時等。
自己搭建時間伺服器需要指定一個上層的時間伺服器,然後它可以向區域網路內的其它機器
提供同步服務。
詳細資料可以見:
http://en.wikipedia.org/wiki/Network_Time_Protocol
安裝NTP Server
Linux系統核心提供了對NTP的支援,因此只需要再安裝一個NTP Server的守護進程即可。
常用的ntpd由ntp.org提供。
下載安裝ntpd的穩定版本。目前是ntp-4.2.6p3.
http://www.ntp.org/downloads.html
示範指令碼::
wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.6p3.tar.gz
tar zxf ntp-4.2.6p3.tar.gz
mkdir /app/ntp
cd ntp-4.2.6p3
./configure --prefix=/app/ntp --enable-all-clocks --enable-parse-clocks
make clean && make check && make && make intall
注意:/app/ntp為ntpd的安裝後檔案路徑
配置
1. 設定檔位置為::
/etc/ntp.conf
2. 修改配置允許區域網路內機器與該伺服器進行時間同步。
將下面語句::
restrict default kod nomodify notrap nopeer noquery
修改為::
restrict default nomodify
3. 修改上一層的時間伺服器。
ntp.org現在提供NTP POOL PROJECT,即ntp 伺服器集區項目。在中國地區,配置如下即可::
server 0.cn.pool.ntp.org
server 0.asia.pool.ntp.org
server 2.asia.pool.ntp.org
其中0.cn 是中國伺服器集區,後面兩個是亞洲的伺服器集區。
最新列表見:
http://www.pool.ntp.org/zone/cn
啟動 NTP 伺服器
進入ntpd安裝後的路徑,如/app/ntd, 啟動參數如下::
./ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid -l /app/ntp/log/ntp.log
注意啟動後需要等幾分鐘,否則其它機器來同步時會得到
no server suitable for synchronization found
的錯誤。
Linux用戶端機器配置
用戶端機器使用ntpdate 程式來執行同步。 格式如下::
ntpdate ntp-server-ip
如果需要配置計劃任務,命令如下::
# 查看是否已經添加時間同步的計劃任務
cat /etc/crontab
# 如果沒有則添加下列行,每30分鐘同步一次:
echo '*/30 * * * * root /usr/sbin/ntpdate 10.20.219.43' >> /etc/crontab
# 將計劃任務設為自動,預設是啟動的【可選】
chkconfig crond on
# 啟動計劃任務服務
service crond restart
# 有些機器需要設定時區為中國
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Windows用戶端機器配置
微軟從Windows 2000後支援NTP,由Win32 Time服務提供。
配置的命令列代碼如下::
rem : 將時間同步服務設為自動,預設被禁用 :
sc config W32Time start= AUTO
rem : 啟動時間同步服務 :
net start W32Time
rem : 設定同步選項 , 10.20.222.2 為時間伺服器ip
w32tm /config /syncfromflags:manual /manualpeerlist:10.20.222.2
rem : 更新同步選項 :
w32tm /config /update
rem : 執行同步 :
w32tm /resync