由于树莓派板子上没有 RTC 硬件和电池,因此树莓派上的系统时间重启是保存不了的。网上已经有人想到应对 NTP 被防火墙封掉类似的需求了,开源的 htpdate 命令直接使用 HTTP 协议来进行系统时间同步,项目主页在这里:
https://github.com/iridium77/htpdate
htpdate 的原理也非常简单,直接解析 HTTP 协议头中的服务器时间信息,然后设置本地时间,我们来看百度返回的 HTTP 头:
HTTP/1.1 200 OK
Date Mon, 13 Oct 2014 16:05:18 GMT
Content-Type text/html
Transfer-Encoding chunked
Connection Keep-Alive
Cache-Control private
Expires Mon, 13 Oct 2014 16:05:18 GMT
Server BWS/1.1
BDPAGETYPE 2
BDQID 0x8b40c1f700000bd4
BDUSERID 13923551
Set-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)NTPetc tp.conf
去掉
- #server127.127.1.0
- #fudge127.127.1.0stratum12
前的#
保存
笔者改完后的配置文件内容如下
- #NTPNetworkTimeProtocol
- #****ATTENTION****:*YouhavetorestarttheNTPservicewhenyouchangethisfiletoactivatethechanges*
- #PLEASECHECKTHISFILECAREFULLYANDMODIFYITIFREQUIRED
- #ConfigurationFilecreatedbyWindowsBinaryDistributionInstallerRev.:1.27mbg
- #pleasecheckhttp://www.ntp.orgforadditionaldocumentationandbackgroundinformation
- #restrictaccesstoavoidabuseofNTPfortrafficamplificationattacks
- #seehttp://news.meinberg.de/244fordetails
- restrictdefaultnoquerynopeernomodifynotrap
- restrict-6defaultnoquerynopeernomodifynotrap
- #allowstatusqueriesandeverythingelsefromlocalhost
- restrict127.0.0.1
- restrict-6::1
- #ifyouneedtoallowaccessfromaremotehost,youcanaddlineslikethis:
- #restrict<IPOFREMOTEHOST>
- #Usedriftfile
- driftfile"D:ProgramFiles(x86)NTPetc tp.drift"
- #yourlocalsystemclock,couldbeusedasabackup
- #(thisisonlyusefulifyouneedtodistributetimenomatterhowgoodorbaditis)
- server127.127.1.0
- #butitshouldoperateatahighstratumleveltolettheclientsknowandforcethemto
- #useanyothertimesourcetheymayhave.
- fudge127.127.1.0stratum12
- #UseaNTPserverfromthentppoolproject(seehttp://www.pool.ntp.org)
- #Pleasenotethatyouneedatleastfourdifferentserverstobeatleastprotectedagainst
- #onefalseticker.Ifyouonlyrelyoninternettime,itishighlyrecommendedtoadd
- #additionalservershere.
- #The'iburst'keywordspeedsupinitialsynchronization,pleasecheckthedocumentationformoredetails!
- server0.asia.pool.ntp.orgiburstminpoll6maxpoll7
- server1.asia.pool.ntp.orgiburstminpoll6maxpoll7
- server2.asia.pool.ntp.orgiburstminpoll6maxpoll7
- server0.us.pool.ntp.orgiburstminpoll6maxpoll7
- server1.us.pool.ntp.orgiburstminpoll6maxpoll7
- server2.us.pool.ntp.orgiburstminpoll6maxpoll7
- #Endofgeneratedntp.conf---Pleaseeditthistosuiteyourneeds
3、重启服务
计算机--右键 管理--服务与应用程序--服务,找到Network Time Protocol Daemon,右键重启
或者通过开始菜单重启。两者作用一样
开始--Meinberg--Network Time Protocol--Service Control--Restart NTP Service
4、本地测试
命令行输入
ntpq -p
结果中的第一行如果出现LOCAL,说明NTP服务器进程存在
- MicrosoftWindows[版本6.3.9600]
- (c)2013MicrosoftCorporation。保留所有权利。
- C:Windowssystem32>ntpq-p
- remoterefidsttwhenpollreachdelayoffsetjitter
- ==============================================================================
- LOCAL(0).LOCL.12l20164100.0000.0000.000
- +ntp2.aliyun.com10.137.38.862u6164755.6551.9187.038
- *118.189.211.186.PPS.1u62647117.00910.2063.703
- +shim.active-app218.186.3.362u124642105.239-4.5972.797
- -104.156.99.226192.12.19.202u61645256.067-2.2317.130
- -y.ns.gin.ntt.ne249.224.99.2132u10645138.07611.2358.025
- C:Windowssystem32>
5、添加防火墙例外
将ntp.exe添加到防火墙例外或者将UDP的123端口添加到防火墙例外
6、再次重启服务器
7、配置树莓派同步时间
首先熟悉几个关于时间命令
- date#查看当前时间
- date-s"2016-03-3110:18:00"#设置当前时间为2016年3月31日10:18:00
- date-s2016-03-31#设置当前日期为2016年3月31日0:00:00
- date-s10:18:00 #设置当前时间为10:18:00
8、安装ntpdate
sudoapt-getinstallntpdate
9、对时
sudontpdate172.26.69.87
其中172.26.69.87为局域网NTP服务器的IP地址
对时后可用date命令查看时间
tip1:如果遇到the NTP socket is in use, exiting的提示,这是因为ntpd也是用的UDP123端口更新时间,我们先将ntp这个服务关掉
sudoservicentpstop
然后再执行
sudontpdate172.26.69.87
tip2:如果遇到no server suitable for synchronization found的提示,多半是因为网络不通或者对应的NTP服务器没有启动。
10、修改时区
默认情况下树莓派使用的是UTC时间,与北京时间相差8小时,所以需要修改时区
tzselect命令并不能真正的修改
正确的做法是替换掉时区文件
sudocp/usr/share/zoneinfo/Asia/Shanghai/etc/localtime
修改之后用date查看到的是CST时间
11、添加局域网NTP服务器地址
修改配置文件ntp.conf
sudonano/etc/ntp.conf
在server项目前面添加如下内容
- server172.26.69.87prefer
- server192.168.42.254iburst
- servercn.pool.ntp.orgiburst
- serverasia.pool.ntp.orgiburst
- serverpool.ntp.orgiburst
这个配置文件用于ntpd程序同步时间,每次树莓派开机启动后都会启动这个程序,同步的时间需要5分钟。
12、查看时间
date
13、配置开机自启
在实际运行中,如果计算机的时间与网络时间相差超过30分钟,那么ntpd就不会自动同步了,笔者处于每天断电7~8个小时的校园网,所以每次开机必须先用ntpdate强制同步时间,但这个进程不能执行得太早,太早的话可能还没联网。所以加了个延迟40秒启动。
编辑
sudonano/usr/bin/synctime
内容
- #!/bin/sh
- #延迟40秒启动
- sleep40s
- #停止ntpd服务
- killallntpd
- #对时
- ntpdate-u202.199.131.1
- #开启ntpd服务器
- ntpd-c/etc/ntp.conf
保存
修改脚本执行权限
sudochmoda+x/usr/bin/synctime
加入开机启动
sudonano/etc/rc.local
#在exit 0前面添加
sudo/usr/bin/synctime>/dev/null2>&1
保存
修改执行权限
sudochmod+x/etc/rc.local
重启
reboot
附:
DJTU内网推荐NTP配置
- server222.26.224.216prefer
- server202.199.131.1iburst
- server202.120.2.100iburst
- servercn.pool.ntp.orgiburst