一、硬件平臺
1、 控制器:MT7620(A9內核)
二、軟件平臺
1、開發環境:ubuntu12.04
2、openwrt 版本:15.05
3、ntp源碼包版本:4.2.8p7
ntp源碼下載鏈接:http://www.ntp.org/downloads.html
4、交叉編譯工具鏈版本:openwrt 源碼包配套的版本 toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_glibc-2.21
三、說明
NTP是網絡時間協議(Network Time Protocol),它是用來同步網絡中各個計算機的時間的協議。
四、編譯步驟
本文的ntp源碼,放在目錄 /home/sky/ntp/
1、解壓源碼: tar -vxf ntp-4.2.8p7.tar.gz
2、創建文件夾,用於存放ntp源碼編譯之後的文件: mkdir ntp_out
3、進入源碼目錄:cd ntp-4.2.8p7
4、編譯源碼
(1)對源碼進行配置,由於參數較多,採用 " \ " 分行。
注意:
a. 本文中的交叉編譯工具鏈,路徑爲:/home/sky/develop/openWrt/openwrt/staging_dir/
b. 由於openwrt的交叉編譯工具鏈比較特別,許多庫都在"target-mipsel_24kec+dsp_glibc-2.21"路徑下,而不是在"toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_glibc-2.21"目錄,所以需要特別指定openssl的路徑,否則編譯會提示找不到相應的文件,編譯失敗。
./configure --prefix=/home/sky/ntp/ntp_out \
--enable-shared \
--disable-static \
--with-yielding-select=manual \
--disable-main \
--host=mipsel-openwrt-linux \
CC=/home/sky/develop/openWrt/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_glibc-2.21/bin/mipsel-openwrt-linux-gnu-gcc \
--with-openssl-incdir=/home/sky/develop/openWrt/openwrt/staging_dir/target-mipsel_24kec+dsp_glibc-2.21/usr/include/
(2)編譯
make
(3) 安裝
make install
執行完成之後,生成的文件都在../out_ntp/ 目錄下,我們需要的文件爲../out_ntp/bin/ntpdate
五、測試ntp client
將 ntpdate 下載到開發板上,進行測試。
1、修改系統時間,隨意設置一個時間:
root@OpenWrt:/tmp#
date -s "2015-8-9 18:54:32"
Sun Aug 9 18:54:32 UTC 2015
如此一來,時間修改爲2015-8-9 18:54:32
2、查看當前時間,確認時間是否修改成功,如下所示,系統時間已經修改爲Sun Aug 9 18:54:35 UTC 2015
root@OpenWrt:/tmp#
date
Sun Aug 9 18:54:35 UTC 2015
3、ntpdate 同步時間
(1)同步時間,可能需要幾秒甚至更長纔能有結果,視網絡和ntp服務器而定。
root@OpenWrt:/tmp#
./ntpdate time.buptnet.edu.cn
3 Jun 09:17:43 ntpdate[1270]: step time server 202.112.10.60 offset 25798865.779885 sec
(2)確認時間是否已經同步
root@OpenWrt:/tmp#
date
Fri Jun 3 09:22:46 UTC 2016
經過對比當前的UTC時間,和系統時間一致,說明ntpdate 可以移植成功。
(3)ntp服務器說明
說明:time.buptnet.edu.cn是北京時間的NTP服務器(IP:202.112.10.60)
當前已提供的各國NTP服務器列表
[INTERNATIONAL NTP server list.]
區域[zone] | 域名[Domain] | IP池[IP Pool] |
---|---|---|
中國[China] | cn.ntp.org.cn |
[202.108.6.95] [202.112.29.82] [110.75.190.198] [115.28.122.198] [182.92.12.11] [120.25.108.11] [110.75.186.249] [110.75.186.248] [110.75.186.247] |
中國臺灣[ChinaTaiwan] | tw.ntp.org.cn | [120.119.28.1] |
美國[America] | us.ntp.org.cn | [24.56.178.140] [131.107.13.100] |
新加坡[Singapore] | sgp.ntp.org.cn | [139.162.20.174] [103.11.143.248] |
韓國[korea] | kr.ntp.org.cn | [218.234.23.44] [211.233.40.78] |
德國[Germany] | de.ntp.org.cn | [131.188.3.220] [131.188.3.223] |
日本[Japan] | jp.ntp.org.cn | [133.243.238.243] [157.7.152.213] |