Raspberry Pi开发之旅-同步时间
使用htpdate同步时间
由于树莓派板子上没有 RTC 硬件和电池,因此树莓派上的系统时间重启是保存不了的。网上已经有人想到应对 NTP 被防火墙封掉类似的需求了,开源的 htpdate 命令直接使用 HTTP 协议来进行系统时间同步,项目主页在这里:
https://github.com/iridium77/htpdate
htpdate 的原理也非常简单,直接解析 HTTP 协议头中的服务器时间信息,然后设置本地时间,我们来看百度返回的 HTTP 头:
HTTP/1.1 200 OKDate Mon, 13 Oct 2014 16:05:18 GMTContent-Type text/htmlTransfer-Encoding chunkedConnection Keep-AliveCache-Control privateExpires Mon, 13 Oct 2014 16:05:18 GMTServer BWS/1.1BDPAGETYPE 2BDQID 0x8b40c1f700000bd4BDUSERID 13923551Set-Cookie BDSVRTM=133; path=/Set-Cookie BD_HOME=1; path=/上面的 Date Mon, 13 Oct 2014 16:05:18 GMT 就是百度的 Web 服务器上的系统时间了。
htpdate 命令做时间同步会有 0.5 秒左右的误差(看看 HTTP 头就知道里面很多时间都是以秒为单位哈),对于我的树莓派来说就完全没有关系,如果对这个比较在意的话只能尽量用 NTP 时间同步了。
htpdate 使用上也很简单,简单编译安装之后把这条命令加到 Raspberry Pi Raspbian 系统的 /etc/rc.local 文件中(不加 -t 参数基本不能同步成功哦):
htpdate -t -s ntp.neu.edu.cn
然后重启树莓派就可以正常同步系统时间了。
使用局域网NTP服务器同步时间
1、配置局域网NTP服务器
下载地址:https://www.meinbergglobal.com/english/sw/ntp.htm
按默认步骤安装即可。
2、修改配置文件ntp.conf
配置文件默认路径为:C:\Program Files (x86)\NTP\etc\ntp.conf
去掉
- #server 127.127.1.0
- #fudge 127.127.1.0 stratum 12
前的#
保存
笔者改完后的配置文件内容如下
- # NTP Network Time Protocol
- # **** ATTENTION ****: *You have to restart the NTP service when you change this file to activate the changes*
- # PLEASE CHECK THIS FILE CAREFULLY AND MODIFY IT IF REQUIRED
- # Configuration File created by Windows Binary Distribution Installer Rev.: 1.27 mbg
- # please check http://www.ntp.org for additional documentation and background information
- # restrict access to avoid abuse of NTP for traffic amplification attacks
- # see http://news.meinberg.de/244 for details
- restrict default noquery nopeer nomodify notrap
- restrict -6 default noquery nopeer nomodify notrap
- # allow status queries and everything else from localhost
- restrict 127.0.0.1
- restrict -6 ::1
- # if you need to allow access from a remote host, you can add lines like this:
- # restrict <IP OF REMOTE HOST>
- # Use drift file
- driftfile "D:\Program Files (x86)\NTP\etc\ntp.drift"
- # your local system clock, could be used as a backup
- # (this is only useful if you need to distribute time no matter how good or bad it is)
- server 127.127.1.0
- # but it should operate at a high stratum level to let the clients know and force them to
- # use any other timesource they may have.
- fudge 127.127.1.0 stratum 12
- # Use a NTP server from the ntp pool project (see http://www.pool.ntp.org)
- # Please note that you need at least four different servers to be at least protected against
- # one falseticker. If you only rely on internet time, it is highly recommended to add
- # additional servers here.
- # The 'iburst' keyword speeds up initial synchronization, please check the documentation for more details!
- server 0.asia.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 1.asia.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 2.asia.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 0.us.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 1.us.pool.ntp.org iburst minpoll 6 maxpoll 7
- server 2.us.pool.ntp.org iburst minpoll 6 maxpoll 7
- # End of generated ntp.conf --- Please edit this to suite your needs
3、重启服务
计算机--右键 管理--服务与应用程序--服务,找到Network Time Protocol Daemon,右键重启
或者通过开始菜单重启。两者作用一样
开始--Meinberg--Network Time Protocol--Service Control--Restart NTP Service
4、本地测试
命令行输入
ntpq -p
结果中的第一行如果出现LOCAL,说明NTP服务器进程存在
- Microsoft Windows [版本 6.3.9600]
- (c) 2013 Microsoft Corporation。保留所有权利。
- C:\Windows\system32>ntpq -p
- remote refid st t when poll reach delay offset jitter
- ==============================================================================
- LOCAL(0) .LOCL. 12 l 201 64 10 0.000 0.000 0.000
- +ntp2.aliyun.com 10.137.38.86 2 u 61 64 7 55.655 1.918 7.038
- *118.189.211.186 .PPS. 1 u 62 64 7 117.009 10.206 3.703
- +shim.active-app 218.186.3.36 2 u 124 64 2 105.239 -4.597 2.797
- -104.156.99.226 192.12.19.20 2 u 61 64 5 256.067 -2.231 7.130
- -y.ns.gin.ntt.ne 249.224.99.213 2 u 10 64 5 138.076 11.235 8.025
- C:\Windows\system32>
5、添加防火墙例外
将ntp.exe添加到防火墙例外或者将UDP的123端口添加到防火墙例外
6、再次重启服务器
7、配置树莓派同步时间
首先熟悉几个关于时间命令
- date #查看当前时间
- date -s "2016-03-31 10:18:00" #设置当前时间为2016年3月31日10:18:00
- date -s 2016-03-31 #设置当前日期为2016年3月31日0:00:00
- date -s 10:18:00 #设置当前时间为10:18:00
8、安装ntpdate
sudo apt-get install ntpdate
9、对时
sudo ntpdate 172.26.69.87
其中172.26.69.87为局域网NTP服务器的IP地址
对时后可用date命令查看时间
tip1:如果遇到the NTP socket is in use, exiting的提示,这是因为ntpd也是用的UDP123端口更新时间,我们先将ntp这个服务关掉
sudo service ntp stop
然后再执行
sudo ntpdate 172.26.69.87
tip2:如果遇到no server suitable for synchronization found的提示,多半是因为网络不通或者对应的NTP服务器没有启动。
10、修改时区
默认情况下树莓派使用的是UTC时间,与北京时间相差8小时,所以需要修改时区
tzselect命令并不能真正的修改
正确的做法是替换掉时区文件
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
修改之后用date查看到的是CST时间
11、添加局域网NTP服务器地址
修改配置文件ntp.conf
sudo nano /etc/ntp.conf
在server项目前面添加如下内容
- server 172.26.69.87 prefer
- server 192.168.42.254 iburst
- server cn.pool.ntp.org iburst
- server asia.pool.ntp.org iburst
- server pool.ntp.org iburst
这个配置文件用于ntpd程序同步时间,每次树莓派开机启动后都会启动这个程序,同步的时间需要5分钟。
12、查看时间
date
13、配置开机自启
在实际运行中,如果计算机的时间与网络时间相差超过30分钟,那么ntpd就不会自动同步了,笔者处于每天断电7~8个小时的校园网,所以每次开机必须先用ntpdate强制同步时间,但这个进程不能执行得太早,太早的话可能还没联网。所以加了个延迟40秒启动。
编辑
sudo nano /usr/bin/synctime
内容
- #! /bin/sh
- #延迟40秒启动
- sleep 40s
- #停止ntpd服务
- killall ntpd
- #对时
- ntpdate -u 202.199.131.1
- #开启ntpd服务器
- ntpd -c /etc/ntp.conf
保存
修改脚本执行权限
sudo chmod a+x /usr/bin/synctime
加入开机启动
sudo nano /etc/rc.local
#在exit 0前面添加
sudo /usr/bin/synctime > /dev/null 2>&1
保存
修改执行权限
sudo chmod +x /etc/rc.local
重启
reboot
附:
DJTU内网推荐NTP配置
- server 222.26.224.216 prefer
- server 202.199.131.1 iburst
- server 202.120.2.100 iburst
- server cn.pool.ntp.org iburst
Raspberry Pi开发之旅-同步时间的更多相关文章
- Raspberry Pi开发之旅-发送邮件记录时间及IP
由于我使用树莓派的场景大多数是在没有显示器.只用terminal连接它的情况下,所以,它的IP地址有时会在重启之后变掉(DHCP的),导致我无法通过terminal连接上它.然后我又要很麻烦地登录路由 ...
- Raspberry Pi开发之旅-实现云平台监控
一.基本设置 1 sudo raspi-config 移动到第五项“Enable Camera”,回车进入,按tab键切换到“Enable”回车确认.回到主菜单,tab键切换到“Finish”回车确认 ...
- Raspberry Pi开发之旅-土壤湿度检测
一.土壤传感器 传感器四个针脚: 针脚 含义 AO 模拟信号输出 DO 数字信号输出 GND 电源负极 VCC 电源正极 二.接线 YL-38和YL69 之间直接用2根母对母线连接. YL-38和树 ...
- Raspberry Pi开发之旅-WIFI遥控小车
一.简单介绍树莓派的GPIO口 上图是树莓派2代的接口(不同型号接口会有差异),我们就以此为例来说下这些接口. 1.GPIO介绍 GPIO 英文全称是:General-purpose input/ou ...
- Raspberry Pi开发之旅-控制蜂鸣器演奏乐曲
一.无源蜂鸣器和有源蜂鸣器 步进电机以及无源蜂鸣器这些都需要脉冲信号才能够驱动,这次尝试用GPIO的PWM接口驱动无源蜂鸣器弹奏一曲<一闪一闪亮晶晶>. 无源蜂鸣器: 无源内部没有震荡源, ...
- Raspberry Pi开发之旅-光照强度检测(BH1750)
一.前期准备 1.环境要求 GY30模块(BH1750FVI传感器),树莓派系统,python-smbus,iic开启 2.取消对IIC驱动的黑名单 nano /etc/modprobe.d/rasp ...
- Raspberry Pi开发之旅-远程监控
1.安装辅助工具 1 2 sudo apt-get install libjpeg8-dev sudo apt-get install cmake 2.编辑源文件 1 2 sudo git clone ...
- Raspberry Pi开发之旅-空气温湿度检测(DHT11)
一.首先,简单介绍下DHT11: DHT11是一个温湿度传感器,分为3个接口,分别为:VCC, DATA, GND 引脚号 名称 类型 说明 1 VCC 电源 +级,输入3V-5.5V 2 DATA ...
- Hello Raspberry Pi
Raspberry Pi 入手好一段时间了,原意是想撸 linux,但是后来一整年都在忙孩子房子户口本子的事,这玩意也就搁了一年尘. 最近终于被生活折腾到了尾声,开始找一些东西来折腾折腾. 一.什么是 ...
随机推荐
- Java HashMap中在resize()时候的rehash,即再哈希法的理解
HashMap的扩容机制---resize() 虽然在hashmap的原理里面有这段,但是这个单独拿出来讲rehash或者resize()也是极好的. 什么时候扩容:当向容器添加元素的时候,会判断当前 ...
- git 显示多个url地址推送
前提 一般来说,我们为git增加远程库,一般都是git remote add origin <url> ( 你可以使用真实的地址来代替 \<url\> ) 但是你可能想要把你的 ...
- Python中的图像处理
第 1 章 基本的图像操作和处理 本章讲解操作和处理图像的基础知识,将通过大量示例介绍处理图像所需的 Python 工具包,并介绍用于读取图像.图像转换和缩放.计算导数.画图和保存结果等的基本工具.这 ...
- 构造方法与构造块的执行顺序(区别于static)
小面试题:在类的实例化时,会调用类的构造块(类中的构造块)和构造方法,无论构造方法在前还是在后,都先执行构造块 class Person{ public Person(){ System.out.pr ...
- Bootstrap(Web前端CSS框架)
官方定义: Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile fi ...
- CodeIgniter框架——CI中视图路径问题
答: 视图中的所有路径全部和 index.php 同级,也就是和 index.php 属于一个目录下,也就是网站根目录. 因为 index.php 后面看似是路径的东西其实那只是一种 URL 参数而已 ...
- Linux 并发服务器雏形总结
如下介绍一个并发回射客户端/服务器的雏形,所谓回射:就是客户端输入一条数据,服务器端读取并显示,然后服务器端再把刚读取的信息发送回客户端进行显示.示意图如下: 所谓并发服务器:就是一个服务器可以同时为 ...
- 【BZOJ1912】[Apio2010]patrol 巡逻 树形DP
[BZOJ1912][Apio2010]patrol 巡逻 Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示 ...
- springboot之修改内置tomcat配置项
1.spring boot默认端口号是8080,如果要修改端口的话,只需要修改application.properties文件,在其中加入 例如: server.port=8081 2.在正常的项目中 ...
- docker postgres
启动一个 Postgres 实例 docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d daocloud.i ...