NTP设置多计算机时间同步,有外网简单设置/纯内网进阶设置

简单需求

ROS下的多计算机通信需要时间同步,以笔记本A和工控机B为例,时间同步只需要一个向另一个看齐即可,这里我让B向A看齐,那么需要在笔记本上A安装ntp,用作时间同步的服务器:

sudo apt-get install ntp

然后在工控机B上安装ntpdate

sudo apt-get install ntpdate

装好后运行

sudo /usr/sbin/ntpdate -u 192.168.2.1(此ip是A的)
sudo /sbin/hwclock -w

这样就完成了两者的时间同步

进阶需求和测试

笔记本只是拿来测试用的,最终是两台工控机进行时间同步,工控机A有外网,B没有,AB之间网线连接通讯。开始用A做ntp的服务器,让B来和A通讯,
基本能够稳定跑。后续根据其它需求,B要做服务器,A要向B同步,问题就出现了,因为作为服务器的B没有外网连接,它会在同步时间的时候试图去同步
外网的服务器,得不到响应后就无法正常工作。所以要把B设置为局域网服务器server,让B始终以自己为标准,不用去访问外网获取时间了。由此需要修改
ntp的配置文件,让A/B有不同的工作模式。

在没有外网的服务器server配置如下:

sudo gedit /etc/ntp.conf

主要要做的修改如下:

#修改本机为服务器,127.127.1.0代表本机,stratum代表层级,设置服务器的初始层级为5
#这里本机ip用127.0.0.1似乎效果不好,可以都试试
server 127.127.1.0
fudge 127.127.1.0 stratum 5

#注释掉原有外网的server
#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
#pool ntp.ubuntu.com

#给本地局域网的用户开放权限,后面啥也不加,表示开放所有权限
restrict 192.168.2.0 mask 255.255.255.0

#禁用认证功能,不知道有用没有,放不放都行
disable auth

配置好之后,重启ntp服务

service ntp restart

然后开始去配置客户端client,同样编辑ntp.conf文件,主要的修改如下:

#指定服务器的固定ip,并设为优先级prefer;如果找不到服务器,再用本地时间,本地时间的层级是8
server 192.168.2.100 prefer
server 127.127.1.0
fudge 127.127.1.0 stratum 8

#其它同上

同样需要重启一下,然后观察服务器链接状态,用

watch ntpq -p

好多文章没说watch,只说ntpq -p,一直提示找不到命令,无语了
运行得到的结果如下,*表示优先,可以看到st一栏的LOCAL是按照我们设置的值5和8在运行的,客户端在服务器的下一级,所以st增加了1。
这里有一个大坑,就是st可能会显示16,超出了正常0-15的范围,表示没有连通。错误no server suitable for synchronization found通常
也是由于这个原因造成的。这种情况需要耐心等5分钟,因为服务器重置后需要320s来重新配置之类的。
等服务器重启配置好后,就可以正常进行时间同步了。

on server:
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.           5 l  125   64  377    0.000    0.000   0.000

on client:
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*tegra-ubuntu-s2 LOCAL(0)         6 u   58   64   37    0.607   -0.443   0.548
 LOCAL(0)        .LOCL.           8 l  125   64   36    0.000    0.000   0.001

时间同步的命令如下:-d是debug模式,可以换成-u,ip是服务器的地址,根据自己情况修改就行

sudo /usr/sbin/ntpdate -d 192.168.2.100

references

简单设置
复杂设置

server 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

# Specify one or more NTP servers.
#server 192.168.2.100
server 127.127.1.0
fudge 127.127.1.0 stratum 5

# 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.

# 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.2.0 mask 255.255.255.0


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.2.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

disable auth

client 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

# Specify one or more NTP servers.
server 192.168.2.100 prefer
server 127.127.1.0
fudge 127.127.1.0 stratum 8

# 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.

# 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.2.0 mask 255.255.255.0

# 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

disable auth
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值