Ubuntu系统使用NTP同步局域网时间

本文档详细介绍了如何在Ubuntu系统中,使用NTP服务使Jetson板卡作为主站,通过局域网为工控机授时。主站只需安装NTP服务并配置不自动更新网络时间;基站需安装NTP和NTPDATE服务,配置ntp.conf文件,指定主站IP,并进行时间同步。常见问题包括服务器同步失败,可能原因是stratum层过高或权限配置不当。
摘要由CSDN通过智能技术生成

Jetson板卡与工控机(Ubuntu系统)在局域网下时间同步

因为项目需要,用英伟达Jetson AGX板卡作为主站,接收外部触发信号同步自身时间(即英伟达板卡为标准时间),通过局域网的方式为各基站(工控机等设备)授时,令局域网内系统时间对齐。

一、主站(Jetson板卡)系统配置

主站无需太多配置,只需要安装NTP服务即可:

sudo apt-get install ntp

安装后其实已经不用做多余的操作,但为了本身项目需要,防止每次NTP服务启动时自动更新网络时间(连接互联网情况下),我们对/etc/ntp.conf文件进行配置,主要注释掉网络时间服务器。

sudo gedit /etc/ntp.conf 

需要注释的代码部分此处的几个语句需要被注释,前面加“#”即可。(在不在意开启NTP服务自动更新网络时间的情况下,本步骤可忽略)

sudo date -s "dd/mm/yy hh:mm:ss"   #修改系统时间,日月年,时分秒

二、基站(工控机等)系统配置

相对于主站的配置,基站要稍微复杂一些。

1. 首先基站也要安装一些服务,不过除了NTP外,还要安装NTPDATE服务。
sudo apt-get install ntp ntpdate
2. 对ntp.conf文件进行配置

主要是下图中的一些位置(ubuntu系统不好编辑图片,直接说行数),图中有字的第9行与倒数4-7行(其中倒数6/7两行可以没有)。在这里插入图片描述以下为修改后的文件,注意使用ifconfig查看自己电脑IP后替换。

# /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.31.0 mask 255.255.255.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

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

server 192.168.31.204 prefer
fudge 192.168.31.204 stratum 8
server 127.127.1.0
fudge 127.127.1.0 stratum 10

# 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

替换后打开终端,输入:

service ntp restart   #重启NTP服务
service ntp status    #查看状态,active即可 
sudo ntpdate -u times.aliyun.com   #阿里云时间同步测试
sudo ntpdate -u 192.168.31.204        #替换为主站(Jeston)IP地址,同步为主站时间

同步成功。
在这里插入图片描述

三、常见问题

1. ntpdate[3813]: no server suitable for synchronization found

使用如下命令查询:

ntpdate -d 192.168.31.204   #替换为主站IP

在这里插入图片描述可以看到,其中报错为:Server dropped: strata too high
stratum层高为16,及未与主站同步,需要查看ntp.conf文件,注意两点:

server 127.127.1.0
fudge 127.127.1.0 stratum 10     #此处的数字必须为0-15
restrict 192.168.31.0 mask 255.255.255.0 nomodify  #绝对不能加notrap修饰,也不能有notrust修饰

如果修改后成功的回馈应该是:
在这里插入图片描述这个问题我目前查到的所有资料均为提及,请注意。

2.其他问题

之前遇到过忘记截图,基本上已经全部处理清楚,如有其他问题先重启NTP服务尝试:

service ntp restart

如果无法解决,建议使用如下语句全部卸载NTP与NTPDATE服务后按照上述步骤重新走一遍:

sudo apt-get --purge remove ntp ntpdate
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值