在树莓派是安装并配置NTP服务

我们都知道树莓派的小巧和省电节省空间等太多的优势,这里就不一一列举了,那么树莓派就需要长时间的运行,可以7×24的方式运行,那么我们就把树莓派当作一个小的服务器来运行,可以跑一些小的应用,例如可以在局域网中当作NTP服务器,哈哈,这个想法不错!

初步设想:

1)树莓派连接网络之后,时间通过网络自动同步,那么树莓派本身的时间就正常了。(不会的自己上网查,类似的文章一堆,不是本文重点)

2)在内部局域网中的其他计算机(包含win,Linux,mac等)都可以以树莓派作为ntp服务器进行时间同步了。

 

一、配置局域网NTP服务器

1.安装ntp服务

通过如下命令安装ntp服务
sudo apt-get install ntp

 

2.修改配置文件ntp.conf

安装完成后,打开/etc/ntp.conf文件,在命令处输入:

cd /
cd etc/
sudo nano ntp.conf

显示内容如下:

复制代码
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift


# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable


# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst


# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
复制代码

以上是安装ntp服务完成后的默认配置。

我自己参考了网上其他人的配置参数,修改成了如下的样子,参考文章在最后给出了地址:

复制代码
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift


# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable


# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst
server time.asia.apple.com prefer
server ntp.sjtu.edu.cn prefer
server 127.127.1.0
fudge 127.127.1.0 stratum 10


# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
#restrictdefault nomodify


# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
复制代码

根据自己的网络情况,以及使用情况,请自行修改ntp配置,本文章末尾处给出了配置的参考连接。

3.重启NTP服务

使用命令重启ntp服务:sudo service ntp restart

4.本地测试

1)先确认ntp是否已启动,命令:ps -ef | grep ntp,应该可以看到两行内容(如果一行太长显示不完,会显示成两行,注意分辨!)

2)命令行输入:ntpq -p

结果如下,说明NTP服务器进程存在

pi@raspberrypi36:/ $ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+cn.ntp.faelix.n 185.134.196.169  2 u    1   64  377  253.107   -7.402  23.282
+d.hnd.pobot.net 255.254.0.27     2 u   15   64  155  249.128   41.977   8.036
 static-5-103-13 .INIT.          16 u    -   64    0    0.000    0.000   0.000
*time5.aliyun.co 10.137.38.86     2 u    9   64  377   30.167    3.322   3.654

重启系统

重启命令:reboot

5.在其它PC同步树莓派上的时间

在其它PC端填入我的树莓派IP。
在Windows下即设置时间时选择Internet时间,并填入树莓派的IP。

=========================================================

参考:

https://blog.csdn.net/rk2900/article/details/8658110

https://www.linuxidc.com/Linux/2016-10/135945.htm

https://blog.csdn.net/gsls200808/article/details/51057359

https://www.cnblogs.com/quchunhui/p/7658853.html

https://blog.csdn.net/yjgithub/article/details/54613172

https://www.cnblogs.com/rongpmcu/p/7662141.html

posted on   jack_Meng  阅读(8349)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2017-07-24 双色球基础分析--SQL
2017-07-24 Windows 7 中的 God Mode
2017-07-24 Free Online SQL Formatter
2017-07-24 Windows 特殊文件夹
2017-07-24 Github 访问慢 解决办法,及常用DNS列表(电信、网通)
2017-07-24 C语言词法分析:C#源码
2017-07-24 把文件以二进制写入到图形文件里

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩