配置树莓派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 plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #server 127.127.1.0  
  2. #fudge 127.127.1.0 stratum 12  

前的#
保存

笔者改完后的配置文件内容如下

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. # NTP Network Time Protocol   
  2. # **** ATTENTION ****: *You have to restart the NTP service when you change this file to activate the changes*   
  3. # PLEASE CHECK THIS FILE CAREFULLY AND MODIFY IT IF REQUIRED   
  4. # Configuration File created by Windows Binary Distribution Installer Rev.: 1.27  mbg  
  5. # please check http://www.ntp.org for additional documentation and background information  
  6. # restrict access to avoid abuse of NTP for traffic amplification attacks   
  7. # see http://news.meinberg.de/244 for details    
  8. restrict default noquery nopeer nomodify notrap    
  9. restrict -6 default noquery nopeer nomodify notrap    
  10.    
  11. # allow status queries and everything else from localhost   
  12. restrict 127.0.0.1   
  13. restrict -6 ::1   
  14.    
  15. # if you need to allow access from a remote host, you can add lines like this:   
  16. # restrict <IP OF REMOTE HOST>   
  17.    
  18. # Use drift file   
  19. driftfile "D:Program Files (x86)NTPetc tp.drift"  
  20.   
  21. # your local system clock, could be used as a backup  
  22. # (this is only useful if you need to distribute time no matter how good or bad it is)  
  23. server 127.127.1.0  
  24. # but it should operate at a high stratum level to let the clients know and force them to  
  25. # use any other timesource they may have.  
  26. fudge 127.127.1.0 stratum 12  
  27.   
  28. # Use a NTP server from the ntp pool project (see http://www.pool.ntp.org)  
  29. # Please note that you need at least four different servers to be at least protected against  
  30. # one falseticker. If you only rely on internet time, it is highly recommended to add  
  31. # additional servers here.   
  32. # The 'iburst' keyword speeds up initial synchronization, please check the documentation for more details!  
  33.  server 0.asia.pool.ntp.org iburst minpoll 6 maxpoll 7  
  34.  server 1.asia.pool.ntp.org iburst minpoll 6 maxpoll 7  
  35.  server 2.asia.pool.ntp.org iburst minpoll 6 maxpoll 7  
  36.  server 0.us.pool.ntp.org iburst minpoll 6 maxpoll 7  
  37.  server 1.us.pool.ntp.org iburst minpoll 6 maxpoll 7  
  38.  server 2.us.pool.ntp.org iburst minpoll 6 maxpoll 7  
  39.   
  40.   
  41. # 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服务器进程存在

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. Microsoft Windows [版本 6.3.9600]  
  2. (c) 2013 Microsoft Corporation。保留所有权利。  
  3.   
  4. C:Windowssystem32>ntpq -p  
  5.      remote           refid      st t when poll reach   delay   offset  jitter  
  6. ==============================================================================  
  7.  LOCAL(0)        .LOCL.          12 l  201   64   10    0.000    0.000   0.000  
  8. +ntp2.aliyun.com 10.137.38.86     2 u   61   64    7   55.655    1.918   7.038  
  9. *118.189.211.186 .PPS.            1 u   62   64    7  117.009   10.206   3.703  
  10. +shim.active-app 218.186.3.36     2 u  124   64    2  105.239   -4.597   2.797  
  11. -104.156.99.226  192.12.19.20     2 u   61   64    5  256.067   -2.231   7.130  
  12. -y.ns.gin.ntt.ne 249.224.99.213   2 u   10   64    5  138.076   11.235   8.025  
  13.   
  14. C:Windowssystem32>  

5.添加防火墙例外

将ntp.exe添加到防火墙例外或者将UDP的123端口添加到防火墙例外

6.再次重启服务

步骤同第三步



二、配置树莓派同步时间

首先熟悉几个关于时间命令

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. date #查看当前时间  
  2. date -s "2016-03-31 10:18:00" #设置当前时间为2016年3月31日10:18:00  
  3. date -s 2016-03-31 #设置当前日期为2016年3月31日0:00:00  
  4. date -s 10:18:00 #设置当前时间为10:18:00  

1.安装ntpdate

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo apt-get install ntpdate  

2.对时

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo ntpdate 172.26.69.87  

其中172.26.69.87为局域网NTP服务器的IP地址
对时后可用date命令查看时间

tip1:如果遇到the NTP socket is in use, exiting的提示,这是因为ntpd也是用的UDP123端口更新时间,我们先将ntp这个服务关掉

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo service ntp stop  

然后再执行

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo ntpdate 172.26.69.87  


tip2:如果遇到no server suitable for synchronization found的提示,多半是因为网络不通或者对应的NTP服务器没有启动。

3.修改时区

默认情况下树莓派使用的是UTC时间,与北京时间相差8小时,所以需要修改时区
tzselect命令并不能真正的修改
正确的做法是替换掉时区文件

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  

修改之后用date查看到的是CST时间

4.添加局域网NTP服务器地址

修改配置文件ntp.conf

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo nano /etc/ntp.conf  

在server项目前面添加如下内容

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. server 172.26.69.87 prefer  
  2. server 192.168.42.254 iburst  
  3. server cn.pool.ntp.org iburst  
  4. server asia.pool.ntp.org iburst  
  5. server pool.ntp.org iburst  

这个配置文件用于ntpd程序同步时间,每次树莓派开机启动后都会启动这个程序,同步的时间需要5分钟。

5.查看时间

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. date  

三、配置开机自启

在实际运行中,如果计算机的时间与网络时间相差超过30分钟,那么ntpd就不会自动同步了,笔者处于每天断电7~8个小时的校园网,所以每次开机必须先用ntpdate强制同步时间,但这个进程不能执行得太早,太早的话可能还没联网。所以加了个延迟40秒启动。

编辑

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo nano /usr/bin/synctime  


内容

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #! /bin/sh  
  2.   
  3. #延迟40秒启动  
  4. sleep 40s  
  5.   
  6. #停止ntpd服务  
  7. killall ntpd  
  8.   
  9. #对时  
  10. ntpdate -u 202.199.131.1  
  11.   
  12. #开启ntpd服务器  
  13. ntpd -c /etc/ntp.conf  

保存
修改脚本执行权限

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo chmod a+x /usr/bin/synctime  

加入开机启动

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo nano /etc/rc.local  

#在exit 0前面添加

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo /usr/bin/synctime > /dev/null 2>&1  

保存
修改执行权限

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. sudo chmod +x /etc/rc.local    

重启

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. reboot  

附:

DJTU内网推荐NTP配置

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. server 222.26.224.216 prefer  
    2. server 202.199.131.1 iburst  
    3. server 202.120.2.100 iburst  
    4. server cn.pool.ntp.org iburst  
原文地址:https://www.cnblogs.com/Pond-ZZC/p/6680257.html