新的 NTPD 安装保持不同步

新的 NTPD 安装保持不同步

我正在尝试使用 ntpd 让 NTP 在 Raspberry Pi 上工作。我已经禁用了 systemd-timesyncd 并使用以下命令安装了 ntp:

$ sudo apt-get install ntp

但安装后,ntp不与任何服务器同步。重新安装ntp后,有时会连接到一台或两台服务器并同步。但重置(sudo service ntp restart)后,它无法再连接到任何服务器。

我使用的是 Raspberry Pi 2 model B 和 Raspberry Pi 3 model B+。它不适用于两种设备。我还有一台 Raspberry Pi 3 B 型,它可以按预期工作。 Raspberry Pi 都连接到同一网络(因此,这不可能是网络防火墙的故障)。 Raspberry Pi 在 Raspbian Buster 上运行。

有时,当它正确同步时,我执行ntpd -p并看到只有一两个服务器。但正常工作的 Raspberry Pi 可以与 4 个以上的服务器同步。 NTP 之前在 Raspberry Pi 2 model B 上工作,但在不对系统进行任何更改的情况下停止工作。之后,我完全重新安装了 NTP,但没有成功 ( sudo apt-get purge --autoremove ntp)。这些结果是使用/etc/ntp.conf.

我不能使用,systemd-timesyncd因为我想让这个系统成为底层系统的服务器。systemd-timesyncd虽然看起来工作正常,但我需要一个服务器来实现设备之间 < 1 毫秒的时钟同步。

NTP状态

下面我放置了 NTP 的状态和结果ntpq -pn

重启后ntp的状态:

pi@raspberrypi:~ $ sudo service ntp status
● ntp.service - Network Time Service
   Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-04-24 14:48:10 CEST; 5s ago
     Docs: man:ntpd(8)
  Process: 2837 ExecStart=/usr/lib/ntp/ntp-systemd-wrapper (code=exited, status=0/SUCCESS)
 Main PID: 2843 (ntpd)
    Tasks: 2 (limit: 2200)
   Memory: 1.0M
   CGroup: /system.slice/ntp.service
           └─2843 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 109:114

Apr 24 14:48:10 raspberrypi ntpd[2843]: Listen normally on 3 eth0 192.168.0.103:123
Apr 24 14:48:10 raspberrypi ntpd[2843]: Listen normally on 4 lo [::1]:123
Apr 24 14:48:10 raspberrypi ntpd[2843]: Listen normally on 5 eth0 [fe80::1ef8:d982:66ac:4214%2]:123
Apr 24 14:48:10 raspberrypi ntpd[2843]: Listening on routing socket on fd #22 for interface updates
Apr 24 14:48:10 raspberrypi ntpd[2843]: kernel reports TIME_ERROR: 0x2041: Clock Unsynchronized
Apr 24 14:48:10 raspberrypi ntpd[2843]: kernel reports TIME_ERROR: 0x2041: Clock Unsynchronized
Apr 24 14:48:11 raspberrypi ntpd[2843]: Soliciting pool server 174.138.9.187
Apr 24 14:48:12 raspberrypi ntpd[2843]: Soliciting pool server 194.5.96.30
Apr 24 14:48:13 raspberrypi ntpd[2843]: Soliciting pool server 162.159.200.123
Apr 24 14:48:14 raspberrypi ntpd[2843]: Soliciting pool server 83.162.149.224

的结果ntpq -pn

pi@raspberrypi:~ $ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 0.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.001
 1.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.001
 2.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.001
 3.debian.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.001

的结果ntpstat

pi@raspberrypi:~ $ ntpstat
unsynchronised
   polling server every 8 s

有谁知道 NTP 与服务器不同步的原因吗?

的输出sudo ntpdate -qu 0.debian.pool.ntp.org

pi@raspberrypi:~ $ sudo ntpdate -qu 0.debian.pool.ntp.org
server 213.136.0.252, stratum 1, offset 0.017778, delay 0.04608
server 45.159.204.28, stratum 2, offset 0.016319, delay 0.04184
server 83.98.155.30, stratum 2, offset 0.016869, delay 0.04346
server 94.228.220.14, stratum 2, offset 0.017546, delay 0.04454
24 Apr 16:23:17 ntpdate[3139]: adjust time server 213.136.0.252 offset 0.017778 sec

配置在/etc/ntp.conf

pi@raspberrypi:~ $ grep '^[^#]' /etc/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
pool 0.debian.pool.ntp.org iburst
pool 1.debian.pool.ntp.org iburst
pool 2.debian.pool.ntp.org iburst
pool 3.debian.pool.ntp.org iburst
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited
restrict 127.0.0.1
restrict ::1
restrict source notrap nomodify noquery

的结果timedatectl status

pi@raspberrypi:~ $ timedatectl status
               Local time: Fri 2020-04-24 16:40:05 CEST
           Universal time: Fri 2020-04-24 14:40:05 UTC
                 RTC time: n/a
                Time zone: Europe/Amsterdam (CEST, +0200)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

答案1

问题是参数System clock synchronisedintimedatectl status应该是yes.不过,我注意到情况并非总是如此。

这是以下的正确输出timedatectl status

$ timedatectl status
               Local time: Tue 2020-04-28 11:28:44 CEST
           Universal time: Tue 2020-04-28 09:28:44 UTC
                 RTC time: n/a
                Time zone: Europe/Amsterdam (CEST, +0200)
System clock synchronized: yes
              NTP service: inactive
          RTC in local TZ: no

此问题可以通过执行以下步骤来解决。

您应该先删除ntpd,否则systemd-timesyncd可能会出现问题。这可以通过以下命令来实现(该命令不删除/etc/ntp.conf):

sudo apt-get remove ntp

之后,可以通过运行以下命令启用 ntp 并启动​​“systemd-timesyncd”:

sudo timedatectl set-ntp yes

该命令自动systemd-timesyncd打开。因此,systemd-timesyncd应停止并禁用。这可以通过以下命令来实现:

sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd

ntpd现在可以重新安装:

sudo apt-get install ntp

现在,ntpd可以使用以下命令重新启动:

sudo service ntp restart

检查是否正常工作

您可以ntp通过执行以下命令来检查是否正常工作:

ntpq -pn应该返回多个IP。

ntpstat应该说它已同步到 NTP 服务器。ntpstat可以使用以下命令安装:

sudo apt-get install ntpstat

相关内容