嵌入式ntp服务器的移植

                嵌入式ntp服务器的移植


网络时间协议Network Time Protocol(NTP) 是一种确保时钟保持准确的方法。如果可以访问互联网,只需安装ntp的客户端软件到互联网上的公共ntp服务器自动修正时间即可 
一、源码获取
1.官网下载http://www.ntp.org/点击download选项页
2.百度网盘:链接:http://pan.baidu.com/s/1bpliP2J 密码:k75a
我的版本是(百度网盘)ntp-4.2.6p5.tar.gz

二、编译源码文件

嵌入式ntp服务器的移植

1.输入:tar -xzvf ntp-4.2.6p5.tar.gz

2.进入解压目录配置

输入: cd ntp-4.2.6p5/

3.指定编译器安装路径

./configure --host=arm-linux CC=arm-none-linux-gnueabi-gcc     --prefix=/home//ntp/

4.make和[make install]

5.安装完之后在安装目录下会有三个文件夹产生

/bin(ntpd,ntpdate,ntpdc,ntp-******,ntpd,ntptime,sntp,tickadj...)

/lib

/sbin

/share (man)

将bin目录下的文件拷贝到目标文件系统bin目录下

嵌入式ntp服务器的移植


三、ntp客户端 
1、保证网络能正常使用(否则ntpdate程序可能会被阻塞在获取网络时间的地方,同时启动ntpdate时也要注意ntpdate的阻塞不能影响其他程序的启动)
2、拷贝ntddate命令到开发板之后执行以下命令 
[email protected]:/# ntpdate  time.buptnet.edu.cn(或者ntpdate  202.112.10.60 
说明:time.buptnet.edu.cn是北京时间的NTP服务器(IP202.112.10.60 
有时候上面的ntp服务器ping不通,建议使用智能dns的ntp服务器,返回的基本上都是中国的ntp服务。
(本人测试时使用的就是ntpdate 0.pool.ntp.org)
   0.pool.ntp.org
   1.pool.ntp.org
   2.pool.ntp.org
   3.pool.ntp.org

如果出现以下问题:
[email protected]:/# 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直接拷贝到开发板/lib目录下即可 

(本人arm开发板内已经有了这两个库,故没有出现上面的错误,直接时间同步成功)

3、拷贝完以上两个文件即可完成时间同步 
[email protected]:/# ntpdate time.buptnet.edu.cn 
24 Jan 07:39:12 ntpdate[2352]: step time server 202.112.10.30 offset -28758.575296 sec 
[email protected]:/# date 
Thu Jan 24 07:39:18 UTC_2013 


注意:用date命令查看之后显示的是UTC时间(世界标准时间),比北京时间(CST=UTC+8)相差8个小时,所以需要设置时区 

4、设置时区为CST时间 
1)把redhat或者ubuntu系统目录/usr/share/zoneinfo/Asia中的文件Shanghai拷贝到开发板目录/etc中并且改名为localtime之后,用命令reboot重启即可,如下: 
[email protected]:~# date 
Thu Jan 24 15:55:29 CST 2013 

5、如果想显示UTC时间可以使用命令 

[email protected]:~# date  –u 

6、如果想将从网络上获取到的系统时间写入硬件时钟中,则使用命令

[email protected]:~# hwclock -w


四、ntp服务器 (因为用不着,所以暂时没有测试)
1、保证做ntp服务器的开发板和ntp客户端的系统的网络连接正常 
2、在要做服务器的开发板上编辑文件/etc/ntp.conf 
   执行命令: 
[email protected]:~# 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                                                                       Qiyang 

3、然后开启ntp服务器
  [email protected]:~# ntpd  -c /etc/ntp.conf

4、ntp服务器开启后五到十几分钟才能在客户端系统中执行以下命令,否则时间同步会失败
  [email protected]:~# ntpdate  192.168.1.48  (192.168.1.48修改成你的服务器的IP地址)