配置树莓派3和局域网NTP服务器实现内网时间校准
一、配置局域網(wǎng)NTP服務(wù)器
1.安裝ntp-4.2.8p5-win32-setup.exe
下載地址:https://www.meinbergglobal.com/english/sw/ntp.htm
按默認(rèn)步驟安裝即可。
2.修改配置文件ntp.conf
配置文件默認(rèn)路徑為:C:Program Files (x86)NTPetc
tp.conf
去掉
[plain]view plaincopy
#server127.127.1.0
#fudge127.127.1.0stratum12
前的#
保存
筆者改完后的配置文件內(nèi)容如下
[plain]view plaincopy
#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.重啟服務(wù)
計(jì)算機(jī)--右鍵 管理--服務(wù)與應(yīng)用程序--服務(wù),找到Network Time Protocol Daemon,右鍵重啟
或者通過(guò)開(kāi)始菜單重啟。兩者作用一樣
開(kāi)始--Meinberg--Network Time Protocol--Service Control--Restart NTP Service
4.本地測(cè)試
命令行輸入
ntpq -p
結(jié)果中的第一行如果出現(xiàn)LOCAL,說(shuō)明NTP服務(wù)器進(jìn)程存在
[plain]view plaincopy
MicrosoftWindows[版本6.3.9600]
(c)2013MicrosoftCorporation。保留所有權(quán)利。
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.再次重啟服務(wù)
步驟同第三步
二、配置樹(shù)莓派同步時(shí)間
首先熟悉幾個(gè)關(guān)于時(shí)間命令
[plain]view plaincopy
date#查看當(dāng)前時(shí)間
date-s"2016-03-3110:18:00"#設(shè)置當(dāng)前時(shí)間為2016年3月31日10:18:00
date-s2016-03-31#設(shè)置當(dāng)前日期為2016年3月31日0:00:00
date-s10:18:00 #設(shè)置當(dāng)前時(shí)間為10:18:00
1.安裝ntpdate
[plain]view plaincopy
sudoapt-getinstallntpdate
2.對(duì)時(shí)
[plain]view plaincopy
sudontpdate172.26.69.87
其中172.26.69.87為局域網(wǎng)NTP服務(wù)器的IP地址
對(duì)時(shí)后可用date命令查看時(shí)間
tip1:如果遇到the NTP socket is in use, exiting的提示,這是因?yàn)閚tpd也是用的UDP123端口更新時(shí)間,我們先將ntp這個(gè)服務(wù)關(guān)掉
[plain]view plaincopy
sudoservicentpstop
然后再執(zhí)行
[plain]view plaincopy
sudontpdate172.26.69.87
tip2:如果遇到no server suitable for synchronization found的提示,多半是因?yàn)榫W(wǎng)絡(luò)不通或者對(duì)應(yīng)的NTP服務(wù)器沒(méi)有啟動(dòng)。
3.修改時(shí)區(qū)
默認(rèn)情況下樹(shù)莓派使用的是UTC時(shí)間,與北京時(shí)間相差8小時(shí),所以需要修改時(shí)區(qū)
tzselect命令并不能真正的修改
正確的做法是替換掉時(shí)區(qū)文件
[plain]view plaincopy
sudocp/usr/share/zoneinfo/Asia/Shanghai/etc/localtime
修改之后用date查看到的是CST時(shí)間
4.添加局域網(wǎng)NTP服務(wù)器地址
修改配置文件ntp.conf
[plain]view plaincopy
sudonano/etc/ntp.conf
在server項(xiàng)目前面添加如下內(nèi)容
[plain]view plaincopy
server172.26.69.87prefer
server192.168.42.254iburst
servercn.pool.ntp.orgiburst
serverasia.pool.ntp.orgiburst
serverpool.ntp.orgiburst
這個(gè)配置文件用于ntpd程序同步時(shí)間,每次樹(shù)莓派開(kāi)機(jī)啟動(dòng)后都會(huì)啟動(dòng)這個(gè)程序,同步的時(shí)間需要5分鐘。
5.查看時(shí)間
[plain]view plaincopy
date
三、配置開(kāi)機(jī)自啟
在實(shí)際運(yùn)行中,如果計(jì)算機(jī)的時(shí)間與網(wǎng)絡(luò)時(shí)間相差超過(guò)30分鐘,那么ntpd就不會(huì)自動(dòng)同步了,筆者處于每天斷電7~8個(gè)小時(shí)的校園網(wǎng),所以每次開(kāi)機(jī)必須先用ntpdate強(qiáng)制同步時(shí)間,但這個(gè)進(jìn)程不能執(zhí)行得太早,太早的話可能還沒(méi)聯(lián)網(wǎng)。所以加了個(gè)延遲40秒啟動(dòng)。
編輯
[plain]view plaincopy
sudonano/usr/bin/synctime
內(nèi)容
[plain]view plaincopy
#!/bin/sh
#延遲40秒啟動(dòng)
sleep40s
#停止ntpd服務(wù)
killallntpd
#對(duì)時(shí)
ntpdate-u202.199.131.1
#開(kāi)啟ntpd服務(wù)器
ntpd-c/etc/ntp.conf
保存
修改腳本執(zhí)行權(quán)限
[plain]view plaincopy
sudochmoda+x/usr/bin/synctime
加入開(kāi)機(jī)啟動(dòng)
[plain]view plaincopy
sudonano/etc/rc.local
#在exit 0前面添加
[plain]view plaincopy
sudo/usr/bin/synctime>/dev/null2>&1
保存
修改執(zhí)行權(quán)限
[plain]view plaincopy
sudochmod+x/etc/rc.local
重啟
[plain]view plaincopy
reboot
附:
DJTU內(nèi)網(wǎng)推薦NTP配置
[plain]view plaincopy
server222.26.224.216prefer
server202.199.131.1iburst
server202.120.2.100iburst
servercn.pool.ntp.orgiburst
總結(jié)
以上是生活随笔為你收集整理的配置树莓派3和局域网NTP服务器实现内网时间校准的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 本地存储cookie和localStor
- 下一篇: mysql 字段存储多个值 ,判断一个值