arm移植NTP(同步嵌入式linux系统时间)

big_anana 2018-06-21 17:58:57 4643

一、说明

网络时间协议Network Time Protocol(NTP) 是一种确保时钟保持准确的方法。如果可以访问互联网,只需安装ntp的客户端软件到互联网上的公共ntp服务器自动修正时间即可

二、准备工作

1、ntp源码包:ntp-dev-4.3.91.tar.gz(可下载地址:http://www.ntp.org/downloads.html
2、交叉编译工具:arm-none-linux-gnueabi-gcc 4.7.3
3、编译环境Ubuntu 13.02

三、移植步骤
1、解压ntp源码包到指定目录
satellite@ubuntu:~/share$ tar -zxvf ntp-dev-4.3.91.tar.gz

2、进入刚解压目录,并配置
(1)satellite@ubuntu:~/share$ cd ntp-dev-4.3.91
(2)在ntpd/ntp_loopfilter.c中增加下面的宏定义
#ifndef MOD_NANO
#define MOD_NANO 0x2000
#endif
(2)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
注意:a.指定的安装目录位源码目录下的install文件夹, b.运行configure前要先设置好arm-linux开发环境,否则报错。configure具体的参数详解间另一篇文章。
出现错误:
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

执行命令: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
出现错误:
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文件夹会有三个文件夹产生
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目录下的需要的命令文件拷贝到开发板的/bin目录下,或者全部拷贝过去
ntp客户端需要用到的命令有ntpdate,ntp服务器需要用到的命令有ntpd

四、ntp客户端
1、保证网络能正常使用(否则ntpdate程序可能会被阻塞在获取网络时间的地方,同事启动ntpdate时也要注意ntpdate的阻塞不能影响其他程序的启动)
2、拷贝ntddate命令到开发板之后执行以下命令
root@EasyARM-iMX283 /# ntpdate time.buptnet.edu.cn(或者ntpdate 202.112.10.60)
说明:time.buptnet.edu.cn是北京时间的NTP服务器(IP:202.112.10.60)
提示:
1 Jan 01:19:17 ntpdate[2179]: no server suitable for synchronization found
有时候上面的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
如果出现以下问题:
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直接拷贝到开发板/lib目录下即可

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

3、拷贝完以上两个文件即可完成时间同步
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时间(世界标准时间),比北京时间(CST=UTC+8)相差8个小时,所以需要设置时区

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

5、如果想显示UTC时间可以使用命令
root@EasyARM-iMX283 /# date –u

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

root@EasyARM-iMX283 /# hwclock -w

7、如果想每次开机设备自动同步时间,可在启动脚本中加入命令行:

ntpdate 0.pool.ntp.org

五、ntp服务器 (因为用不着,所以暂时没有测试)
1、保证做ntp服务器的开发板和ntp客户端的系统的网络连接正常

2、在要做服务器的开发板上编辑文件/etc/ntp.conf
执行命令:
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服务器
root@EasyARM-iMX283 /# ntpd -c /etc/ntp.conf

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

声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包 点赞 收藏 评论 打赏
评论
0个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
关于作者
big_anana

big_anana

暂无个性签名~

原创101
阅读40.9w
收藏3
点赞35
评论3
打赏用户 0
我要创作
分享技术经验,可获取创作收益
分类专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
big_anana
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区