在有网络连接的环境下,我们可以通过一些授时服务器获取标准时间,以达到所有设备同步时间的目的。但是有时候我们仅仅使用了本地局域网,无法使用外部服务器,此时就需要解决本地同步时间的目的。
使用的方法为为NTP(Network Time Protocol)。NTP服务器【Network Time Protocol(NTP)】是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。(摘自百度百科)

环境介绍

在一个局域网下,有一个运行Ubuntu 18的X86主机和一个运行着Ubuntu 18的Jetson Nano。主机IP地址为192.168.3.3,Jetson Nano的IP地址为192.168.3.12。我们需要将主机设置为服务器,Jetson Nano将会向主机同步其时间。即主机配置为NTP服务器,其余设备(Jetson Nano,可以更多)为客户端,向其同步时间。

操作流程

配置主机服务

主机需要安装ntp和ntpdate两个包:

sudo apt install ntp ntpdate

然后修改器配置文件/etc/ntp.conf如下:

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

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# 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

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
pool ntp.ubuntu.com

# 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.
restrict 192.168.3.0 mask 255.255.0.0 nomodify notrap

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

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

# Needed for adding pool entries
restrict source notrap nomodify noquery

server 210.72.145.44 perfer
server 202.112.10.36
server 59.124.196.83
# allow update time by the upper server
restrict 210.72.145.44 nomodify notrap noquery
restrict 202.112.10.36 nomodify notrap noquery
restrict 59.124.196.83 nomodify notrap noquery
#local server
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

# 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

#Changes recquired to use pps synchonisation as explained in documentation:
#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918

#server 127.127.8.1 mode 135 prefer    # Meinberg GPS167 with PPS
#fudge 127.127.8.1 time1 0.0042        # relative to PPS for my hardware

#server 127.127.22.1                   # ATOM(PPS)
#fudge 127.127.22.1 flag3 1            # enable PPS API

修改部分为48行和61-70行,修改完成后保存退出即可了。

测试从机同步

从机需要安装ntpdate:

sudo apt install ntpdate

之后直接在终端中输入如下指令即可同步时间:

sudo ntpdate 192.168.3.3

如果想实现开机自启可以参考Ubuntu18下配置开机以sudo运行的指令
注意文中所有IP设置均要修改成你实际的IP和网段。此外可能会有一些其他问题,我暂时还没遇到过,有的话欢迎交流。交流Q群:376147821。
参考链接:https://blog.csdn.net/weixin_38717571/article/details/84638044