一、配置局域网NTP服务器
1.安装ntp-4.2.8p5-win32-setup.exe
下载地址:/english/sw/ntp.htm
按默认步骤安装即可。
2.修改配置文件ntp.conf
配置文件默认路径为:C:\Program Files (x86)\NTP\etc\ntp.conf
去掉
[plain]view plaincopy #server127.127.1.0#fudge127.127.1.0stratum12
前的#
保存
笔者改完后的配置文件内容如下
[plain]view plaincopy #NTPNetworkTimeProtocol#****ATTENTION****:*YouhavetorestarttheNTPservicewhenyouchangethisfiletoactivatethechanges*#PLEASECHECKTHISFILECAREFULLYANDMODIFYITIFREQUIRED#ConfigurationFilecreatedbyWindowsBinaryDistributionInstallerRev.:1.27mbg#pleasecheckforadditionaldocumentationandbackgroundinformation#restrictaccesstoavoidabuseofNTPfortrafficamplificationattacks#seehttp://news.meinberg.de/244fordetailsrestrictdefaultnoquerynopeernomodifynotraprestrict-6defaultnoquerynopeernomodifynotrap#allowstatusqueriesandeverythingelsefromlocalhostrestrict127.0.0.1restrict-6::1#ifyouneedtoallowaccessfromaremotehost,youcanaddlineslikethis:#restrict<IPOFREMOTEHOST>#Usedriftfiledriftfile"D:\ProgramFiles(x86)\NTP\etc\ntp.drift"#yourlocalsystemclock,couldbeusedasabackup#(thisisonlyusefulifyouneedtodistributetimenomatterhowgoodorbaditis)server127.127.1.0#butitshouldoperateatahighstratumleveltolettheclientsknowandforcethemto#useanyothertimesourcetheymayhave.fudge127.127.1.0stratum12#UseaNTPserverfromthentppoolproject(seehttp://www.)#Pleasenotethatyouneedatleastfourdifferentserverstobeatleastprotectedagainst#onefalseticker.Ifyouonlyrelyoninternettime,itishighlyrecommendedtoadd#additionalservershere.#The'iburst'keywordspeedsupinitialsynchronization,pleasecheckthedocumentationformoredetails!server0.asia.iburstminpoll6maxpoll7server1.asia.iburstminpoll6maxpoll7server2.asia.iburstminpoll6maxpoll7server0.us.iburstminpoll6maxpoll7server1.us.iburstminpoll6maxpoll7server2.us.iburstminpoll6maxpoll7#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)MicrosoftCorporation。保留所有权利。C:\Windows\system32>ntpq-premoterefidsttwhenpollreachdelayoffsetjitter==============================================================================LOCAL(0).LOCL.12l4100.0000.0000.000+10.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.025C:\Windows\system32>
5.添加防火墙例外
将ntp.exe添加到防火墙例外或者将UDP的123端口添加到防火墙例外
6.再次重启服务
步骤同第三步
二、配置树莓派同步时间
首先熟悉几个关于时间命令
[plain]view plaincopy date#查看当前时间date-s"-03-3110:18:00"#设置当前时间为3月31日10:18:00date-s-03-31#设置当前日期为3月31日0:00:00date-s10:18:00#设置当前时间为10:18:00
1.安装ntpdate
[plain]view plaincopy sudoapt-getinstallntpdate2.对时
[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.87preferserver192.168.42.254iburstservercn.iburstserverasia.iburst
这个配置文件用于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.216preferserver202.199.131.1iburstserver202.120.2.100iburstservercn.iburst
如果觉得《配置树莓派3和局域网NTP服务器实现内网时间校准》对你有帮助,请点赞、收藏,并留下你的观点哦!