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