久久ER99热精品一区二区-久久精品99国产精品日本-久久精品免费一区二区三区-久久综合九色综合欧美狠狠

博客專欄

EEPW首頁(yè) > 博客 > arm移植NTP(同步嵌入式linux系統(tǒng)時(shí)間)

arm移植NTP(同步嵌入式linux系統(tǒng)時(shí)間)

發(fā)布人:電子禪石 時(shí)間:2021-01-05 來(lái)源:工程師 發(fā)布文章
一、說(shuō)明 
 
網(wǎng)絡(luò)時(shí)間協(xié)議Network Time Protocol(NTP) 是一種確保時(shí)鐘保持準(zhǔn)確的方法。如果可以訪問(wèn)互聯(lián)網(wǎng),只需安裝ntp的客戶端軟件到互聯(lián)網(wǎng)上的公共ntp服務(wù)器自動(dòng)修正時(shí)間即可 


二、準(zhǔn)備工作 

1ntp源碼包:ntp-dev-4.3.91.tar.gz(可下載地址:http://www.ntp.org/downloads.html
2、交叉編譯工具:arm-none-linux-gnueabi-gcc  4.7.3
3、編譯環(huán)境Ubuntu 13.02

三、移植步驟 
1、解壓ntp源碼包到指定目錄 
satellite@ubuntu:~/share$ tar -zxvf ntp-dev-4.3.91.tar.gz

2、進(jìn)入剛解壓目錄,并配置 
1satellite@ubuntu:~/share$ cd ntp-dev-4.3.91
(2)在ntpd/ntp_loopfilter.c中增加下面的宏定義
#ifndef MOD_NANO
#define MOD_NANO  0x2000
#endif
2satellite@ubuntu:~/share/ntp-dev-4.3.91$ ./configure --prefix=$PWD/install --exec-prefix=$PWD/install --host=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc
注意:a.指定的安裝目錄位源碼目錄下的install文件夾, b.運(yùn)行configure前要先設(shè)置好arm-linux開發(fā)環(huán)境,否則報(bào)錯(cuò)。configure具體的參數(shù)詳解間另一篇文章。
出現(xiàn)錯(cuò)誤:
checking for pthread_kill_other_threads_np... no
checking for LinuxThreads implementation... no
checking for LinuxThreads consistency... no
checking if pthread_create() works... yes
checking if select yields when using pthreads... cross
configure: error: crossing compiling: use --with-yielding-select=yes|no|manual


執(zhí)行命令:satellite@ubuntu:~/share/ntp-dev-4.3.91$ ./configure --prefix=$PWD/install --exec-prefix=$PWD/install --host=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc --with-yielding-select=yes
    此次,在安裝目錄下成功生成了Makefile文件。

3、編譯并安裝 
satellite@ubuntu:~/share/ntp-dev-4.3.91$ make
出現(xiàn)錯(cuò)誤:
ntp_loopfilter.c: In function 'local_clock':
ntp_loopfilter.c:760: error: 'MOD_NANO' undeclared (first use in this function)
ntp_loopfilter.c:760: error: (Each undeclared identifier is reported only once
ntp_loopfilter.c:760: error: for each function it appears in.)


在ntpd/ntp_loopfilter.c中增加下面的宏定義
#ifndef MOD_NANO
#define MOD_NANO  0x2000
#endif

satellite@ubuntu:~/share/ntp-dev-4.3.91$ make  install

4、安裝完之后在安裝目錄下install文件夾會(huì)有三個(gè)文件夾產(chǎn)生
satellite@ubuntu:~/share/ntp-dev-4.3.91/install$ ls
bin  libexec  sbin  share

bin目錄中的命令文件有如下:
satellite@ubuntu:~/share/ntp-dev-4.3.91/install$ ls bin
calc_tickadj  ntpd  ntpdate  ntpdc  ntp-keygen  ntpq  ntptime  ntptrace  ntp-wait  sntp  tickadj  update-leap

5、把bin目錄下的需要的命令文件拷貝到開發(fā)板的/bin目錄下,或者全部拷貝過(guò)去 
ntp客戶端需要用到的命令有ntpdatentp服務(wù)器需要用到的命令有ntpd 
四、ntp客戶端 
1、保證網(wǎng)絡(luò)能正常使用(否則ntpdate程序可能會(huì)被阻塞在獲取網(wǎng)絡(luò)時(shí)間的地方,同事啟動(dòng)ntpdate時(shí)也要注意ntpdate的阻塞不能影響其他程序的啟動(dòng))
2、拷貝ntddate命令到開發(fā)板之后執(zhí)行以下命令 
root@EasyARM-iMX283 /# ntpdate  time.buptnet.edu.cn(或者ntpdate  202.112.10.60 
說(shuō)明:time.buptnet.edu.cn是北京時(shí)間的NTP服務(wù)器(IP202.112.10.60
提示:

 1 Jan 01:19:17 ntpdate[2179]: no server suitable for synchronization found

有時(shí)候上面的ntp服務(wù)器ping不通,建議使用智能dns的ntp服務(wù)器,返回的基本上都是中國(guó)的ntp服務(wù)。
(本人測(cè)試時(shí)使用的就是ntpdate 0.pool.ntp.org)
   0.pool.ntp.org
   1.pool.ntp.org
   2.pool.ntp.org
   3.pool.ntp.org

如果出現(xiàn)以下問(wèn)題:
root@EasyARM-iMX283 /# ntpdate time.buptnet.edu.cn
ntpdate:error while loading shared libraries: libcap.so.2: cannot open shared object file:
no such file or directory

則解決辦法:在安裝交叉編譯工具的目錄arm-arago-linux-gnueabi/usr/lib中有文件libcap.so.2和libattr.so.1直接拷貝到開發(fā)板/lib目錄下即可 

(本人arm開發(fā)板內(nèi)已經(jīng)有了這兩個(gè)庫(kù),故沒(méi)有出現(xiàn)上面的錯(cuò)誤,直接時(shí)間同步成功)


3、拷貝完以上兩個(gè)文件即可完成時(shí)間同步 
root@EasyARM-iMX283 /# ntpdate 0.pool.ntp.org 
14 Apr 12:38:30 ntpdate[2188]: step time server 110.75.186.248 offset 1460632666.423442 sec
root@EasyARM-iMX283 /# date 
Thu Apr 14 20:49:19 CST 2016

注意:用date命令查看之后顯示的是UTC時(shí)間(世界標(biāo)準(zhǔn)時(shí)間),比北京時(shí)間(CST=UTC+8)相差8個(gè)小時(shí),所以需要設(shè)置時(shí)區(qū) 

4、設(shè)置時(shí)區(qū)為CST時(shí)間 
1)把redhat或者ubuntu系統(tǒng)目錄/usr/share/zoneinfo/Asia中的文件Shanghai拷貝到開發(fā)板目錄/etc中并且改名為localtime之后,用命令reboot重啟即可,如下: 
root@EasyARM-iMX283 /# date 
Thu Jan 24 15:55:29 CST 2013 

5、如果想顯示UTC時(shí)間可以使用命令 

root@EasyARM-iMX283 /# date  –u 


6、如果想將從網(wǎng)絡(luò)上獲取到的系統(tǒng)時(shí)間寫入硬件時(shí)鐘中,則使用命令

root@EasyARM-iMX283 /# hwclock -w


7、如果想每次開機(jī)設(shè)備自動(dòng)同步時(shí)間,可在啟動(dòng)腳本中加入命令行:

ntpdate 0.pool.ntp.org


五、ntp服務(wù)器 (因?yàn)橛貌恢詴簳r(shí)沒(méi)有測(cè)試)
1、保證做ntp服務(wù)器的開發(fā)板和ntp客戶端的系統(tǒng)的網(wǎng)絡(luò)連接正常 

2、在要做服務(wù)器的開發(fā)板上編輯文件/etc/ntp.conf 
   執(zhí)行命令: 
root@EasyARM-iMX283 /# vi  /etc/ntp.conf 
#restrict default nomodify notrap noquery 
restrict 127.0.0.1 
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 
server  127.127.1.0        # local clock 
fudge   127.127.1.0  stratum 5 
driftfile /var/lib/ntp/drift 
broadcastdelay   0.008 
keys                     /etc/ntp/keys

3、然后開啟ntp服務(wù)器
root@EasyARM-iMX283 /# ntpd  -c /etc/ntp.conf
4ntp服務(wù)器開啟后五到十幾分鐘才能在客戶端系統(tǒng)中執(zhí)行以下命令,否則時(shí)間同步會(huì)失敗
root@EasyARM-iMX283 /# ntpdate  192.168.1.48  (192.168.1.48修改成你的服務(wù)器的IP地址)


*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。



關(guān)鍵詞:

相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉