启动时立即使用无纪律时钟同步 ntp

启动时立即使用无纪律时钟同步 ntp

我已经设置了一个 ntp 服务器和 5 个客户端。我想在启动时强制将客户端同步到服务器时钟。使用我当前的 ntp 设置,计算机正在同步,但需要几分钟。我不能等这么久让它们同步。我没有使用 RTC/GPS,所有机器都在 LAN 中。我需要使用什么配置或命令来强制它们在启动时立即将所有客户端与服务器时钟同步服务器时钟不规范*?

服务器 ntp.conf

driftfile /var/lib/ntp/ntp.drift
leapfile /usr/share/zoneinfo/leap-seconds.list

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

server 127.127.1.0
fudge 127.127.1.0 stratum 8

# 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

# (Again, the address is an example only.)
broadcast 192.168.0.255

客户端 ntp conf

driftfile /var/lib/ntp/ntp.drift
leapfile /usr/share/zoneinfo/leap-seconds.list

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

server 192.168.0.51

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

# 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

编辑添加建议命令的结果

root@displaypi:~# ntpd -g -x -q 192.168.0.71
23 Feb 16:36:22 ntpd[446]: ntpd [email protected] (1): Starting
23 Feb 16:36:22 ntpd[446]: Command line: ntpd -g -x -q 192.168.0.71
23 Feb 16:36:22 ntpd[446]: proto: precision = 0.815 usec (-20)
23 Feb 16:36:22 ntpd[446]: leapsecond file ('/usr/share/zoneinfo/leap-seconds.list'): good hash signature
23 Feb 16:36:22 ntpd[446]: leapsecond file ('/usr/share/zoneinfo/leap-seconds.list'): loaded, expire=2021-06-28T00:00:00Z last=2017-01-01T00:00:00Z ofs=37
23 Feb 16:36:22 ntpd[446]: Listen and drop on 0 v6wildcard [::]:123
23 Feb 16:36:22 ntpd[446]: Listen and drop on 1 v4wildcard 0.0.0.0:123
23 Feb 16:36:22 ntpd[446]: Listen normally on 2 lo 127.0.0.1:123
23 Feb 16:36:22 ntpd[446]: Listen normally on 3 eth0 192.168.0.72:123
23 Feb 16:36:22 ntpd[446]: Listen normally on 4 lo [::1]:123
23 Feb 16:36:22 ntpd[446]: Listen normally on 5 eth0 [fe80::dea6:32ff:fee4:8867%2]:123
23 Feb 16:36:22 ntpd[446]: Listening on routing socket on fd #22 for interface updates
23 Feb 16:36:22 ntpd[446]: Listen for broadcasts to 192.168.0.255 on interface #3 eth0
23 Feb 16:36:29 ntpd[446]: ntpd: time slew +25.555462 s
ntpd: time slew +25.555462s
root@displaypi:~# date
Tue Feb 23 16:36:31 IST 2021
root@displaypi:~# date
Tue Feb 23 16:36:39 IST 2021
root@displaypi:~# date
Tue Feb 23 16:36:59 IST 2021
root@displaypi:~# ntpq -p
ntpq: read: Connection refused
root@displaypi:~# service ntp start
root@displaypi:~# ntpq -p
 remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.0.71    LOCAL(0)         4 u    1   16    1    0.192  25533.4   0.001
root@displaypi:~# service ntp sto
Usage: /etc/init.d/ntp {start|stop|restart|try-restart|force-reload|status}
root@displaypi:~# service ntp stop
root@displaypi:~# ntpd -g -x -q 192.168.0.71
23 Feb 16:38:03 ntpd[513]: ntpd [email protected] (1): Starting
23 Feb 16:38:03 ntpd[513]: Command line: ntpd -g -x -q 192.168.0.71
23 Feb 16:38:03 ntpd[513]: proto: precision = 0.778 usec (-20)
23 Feb 16:38:03 ntpd[513]: leapsecond file ('/usr/share/zoneinfo/leap-seconds.list'): good hash signature
23 Feb 16:38:03 ntpd[513]: leapsecond file ('/usr/share/zoneinfo/leap-seconds.list'): loaded, expire=2021-06-28T00:00:00Z last=2017-01-01T00:00:00Z ofs=37
23 Feb 16:38:03 ntpd[513]: Listen and drop on 0 v6wildcard [::]:123
23 Feb 16:38:03 ntpd[513]: Listen and drop on 1 v4wildcard 0.0.0.0:123
23 Feb 16:38:03 ntpd[513]: Listen normally on 2 lo 127.0.0.1:123
23 Feb 16:38:03 ntpd[513]: Listen normally on 3 eth0 192.168.0.72:123
23 Feb 16:38:03 ntpd[513]: Listen normally on 4 lo [::1]:123
23 Feb 16:38:03 ntpd[513]: Listen normally on 5 eth0 [fe80::dea6:32ff:fee4:8867%2]:123
23 Feb 16:38:03 ntpd[513]: Listening on routing socket on fd #22 for interface updates
23 Feb 16:38:03 ntpd[513]: Listen for broadcasts to 192.168.0.255 on interface #3 eth0
23 Feb 16:38:10 ntpd[513]: ntpd: time slew +0.000000 s
ntpd: time slew +0.000000s

答案1

您可以使用-G参数。来自手册页:

-G

通常,如果偏移量超过恐慌阈值(默认情况下为 1000 秒),ntpd 将退出并向系统日志发送一条消息。此选项允许将时间设置为任意值而不受限制;但是,这种情况只能发生一次。如果此后超过阈值,ntpd 将退出并向系统日志发送一条消息。此选项可以与 -q 和 -x 选项一起使用。有关其他选项,请参阅 tinker 命令。

因此,在启动时,您必须运行一次(添加到某些启动脚本)以下命令(用您自己的 NTP 服务器替换 pool.ntp.org)以同步本地时钟: ntpd -g -x -q pool.ntp.org

之后您可以正常运行 ntpd。

答案2

除了-g上面描述的标志之外,您还应该在客户端上添加iburstserver。这将使它们更快地与服务器同步。

然而,你不能指望你的服务器设置能很好地工作,因为您没有外部参考时钟,并且您正在强制服务器的本地时钟为高层。您需要有一条通往 1 层参考时钟的路径,无论是在您的 LAN 上本地还是通过 Internet。

此外,如果客户端和服务器同时断电,那么它们都会跳起来(见尽管启动前进行了 NTP 同步,但系统时间仍偏差数百毫秒例如)略有不同,最终可能会导致服务器向后跳转而客户端向前跳转,这意味着当客户端同步时,它们可能会看到较大的(以 NTP 术语而言)向后跳转。如果您的服务器启动时间比客户端长(这种情况并不少见),则该值可能会更大。

您确实无法摆脱在某处拥有 1 级参考时钟的要求。如果时间对您如此重要,最好投资购买低功耗 GPS 同步设备,如 BeagleBone、Raspberry Pi 或 LeoNTP,所有这些设备都可以在小型 UPS 上运行数小时。

答案3

ntpdate您可以在开始之前使用同步一次ntpd

如果您正在运行 Debian,则该ntpdate软件包已经包含了必要的基础设施dhclient;假设似乎是没有 RTC 的设备可能也没有固定的 IP 地址。

答案4

你想做的事情是不可能的。当且仅当两个时钟的流动速率相等时,两个时钟才会同步。否则,它们就不同步。没有即时的方法来观察一个时钟的流动速率或比较两个时钟的流动速率。

给定每个时钟的时间分辨率、它们之间的通信延迟、延迟中的抖动、延迟的最大不对称性以及时钟同步所需的精度,就可以确定完成同步的最短时间。

burst如果您使用适当的 ntp 设置(例如或iburst),它很可能与您获得的任何值处于同一数量级。

相关内容