网络时间协议 - NTP

交换机上运行的 ntpd 守护进程实现 NTP 协议。它将系统时间与 /etc/ntpsec/ntp.conf 文件中的时间服务器同步。默认情况下,ntpd 守护进程在启动时启动。

如果您打算在 VRF(包括管理 VRF)中运行此服务,请按照这些步骤配置服务。

配置 NTP 服务器

默认 NTP 配置包括以下服务器,这些服务器位于 /etc/ntpsec/ntp.conf 文件中

  • server 0.cumulusnetworks.pool.ntp.org iburst
  • server 1.cumulusnetworks.pool.ntp.org iburst
  • server 2.cumulusnetworks.pool.ntp.org iburst
  • server 3.cumulusnetworks.pool.ntp.org iburst

要添加您想要使用的 NTP 服务器,请运行以下命令。包括 iburst 选项以提高同步速度。

NVUE 命令需要 VRF。以下命令在默认 VRF 中添加 NTP 服务器。

cumulus@switch:~$ nv set service ntp default server 4.cumulusnetworks.pool.ntp.org iburst on
cumulus@switch:~$ nv config apply

编辑 /etc/ntpsec/ntp.conf 文件以添加或更新 NTP 服务器信息

cumulus@switch:~$ sudo nano /etc/ntpsec/ntp.conf
# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
server 0.cumulusnetworks.pool.ntp.org iburst
server 1.cumulusnetworks.pool.ntp.org iburst
server 2.cumulusnetworks.pool.ntp.org iburst
server 3.cumulusnetworks.pool.ntp.org iburst
server 4.cumulusnetworks.pool.ntp.org iburst

要在启动 ntpd 守护进程之前使用 NTP 设置初始日期和时间,请运行 ntpd -q 命令。请注意,如果时间服务器不可访问,ntpd -q 可能会挂起。

要验证 ntpd 是否在系统上运行

cumulus@switch:~$ ps -ef | grep ntp
ntp       4074     1  0 Jun20 ?        00:00:33 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 101:102

要检查 NTP 对等状态

cumulus@switch:~$ nv show service ntp mgmt server
                 delay    iburst  jitter  offset   peer-state  poll  reach  refid         stratum  type  when
---------------  -------  ------  ------  -------  ----------  ----  -----  ------------  -------  ----  ----
23.157.160.168   67.4257          2.3843  -3.9378  -           128   377    129.6.15.28   2        u     41  
50.205.57.38     72.6007          1.0799  -1.8208  *           128   377    .GPS.         1        u     63  
h134-215-155-17  59.4988          2.3081  -2.6286  +           128   377    216.239.35.0  2        u     15  
li1150-42.membe  40.9645          0.4877  -1.9565  +           64    376    129.7.1.66    2        u     162

nv show service ntp <vrf-id> pool 命令显示有关配置的 NTP 池的信息。但是,此命令不显示与网络上 NTP 参考时钟的连接状态的准确表示。要显示交换机发现的 NTP 参考服务器的实际状态,请运行 nv show service ntp <vrf-id> server 命令。

cumulus@switch:~$ ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ec2-34-225-6-20 129.6.15.30      2 u   73 1024  377   70.414   -2.414   4.110
+lax1.m-d.net    132.163.96.1     2 u   69 1024  377   11.676    0.155   2.736
*69.195.159.158  199.102.46.72    2 u  133 1024  377   48.047   -0.457   1.856
-2.time.dbsinet. 198.60.22.240    2 u 1057 1024  377   63.973    2.182   2.692

以下示例命令删除默认 NTP 服务器

cumulus@switch:~$ nv unset service ntp default server 0.cumulusnetworks.pool.ntp.org
cumulus@switch:~$ nv unset service ntp default server 1.cumulusnetworks.pool.ntp.org
cumulus@switch:~$ nv unset service ntp default server 2.cumulusnetworks.pool.ntp.org
cumulus@switch:~$ nv unset service ntp default server 3.cumulusnetworks.pool.ntp.org
cumulus@switch:~$ nv config apply

编辑 /etc/ntpsec/ntp.conf 文件以删除 NTP 服务器。

cumulus@switch:~$ sudo nano /etc/ntpsec/ntp.conf
...
# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
server 4.cumulusnetworks.pool.ntp.org iburst
...

指定 NTP 源接口

默认情况下,NTP 使用的源接口是 eth0。以下示例命令将 NTP 源接口配置为 swp10。

cumulus@switch:~$ nv set service ntp default listen swp10
cumulus@switch:~$ nv config apply

编辑 /etc/ntpsec/ntp.conf 文件并修改 Specify interfaces 注释下的条目。

cumulus@switch:~$ sudo nano /etc/ntpsec/ntp.conf
...
# Specify interfaces
interface listen swp10
...

在 DHCP 环境中使用 NTP

您可以使用 DHCP 来指定您的 NTP 服务器。确保 DHCP 生成的配置文件 /run/ntp.conf.dhcp 存在。/etc/dhcp/dhclient-exit-hooks.d/ntp 脚本生成此文件,该文件是默认 /etc/ntpsec/ntp.conf 文件的副本,其中包含来自 DHCP 服务器的修改后的服务器列表。如果此文件不存在,并且您计划将来使用 DHCP,您可以将当前的 /etc/ntpsec/ntp.conf 文件复制到 DHCP 文件的位置。

要使用 DHCP 指定您的 NTP 服务器,请运行 sudo -E systemctl edit ntpsec.service 命令并添加 ExecStart=

cumulus@switch:~$ sudo -E systemctl edit ntpsec.service
[Service]
ExecStart=
ExecStart=/usr/sbin/ntpd -n -u ntp:ntp -g -c /run/ntp.conf.dhcp

即使您使用 ntp@mgmt.servicesudo -E systemctl edit ntpsec.service 命令始终更新基本 ntpsec.servicentpsec@mgmt.service 会自动重新生成。

要验证您的配置,请运行以下命令

cumulus@switch:~$ sudo systemctl restart ntp
cumulus@switch:~$ sudo systemctl status -n0 ntpsec.service

如果状态不是 *Active*,或者备用配置文件未出现在 ntp 命令行中,则可能是您犯了配置错误。更正错误并重新运行上述命令以进行验证。

配置带有授权密钥的 NTP

为了增加安全性,您可以配置 NTP 以使用授权密钥。

配置 NTP 服务器

  1. 创建一个 .keys 文件,例如 /etc/ntp.keys。指定密钥标识符(1 到 65535 之间的数字)、加密方法(M 代表 MD5)和密码。以下提供了一个示例
```
#
# PLEASE DO NOT USE THE DEFAULT VALUES HERE.
#
#65535  M  akey
#1      M  pass

1  M  CumulusLinux!
```
  1. /etc/ntpsec/ntp.conf 文件中,添加指向您上面创建的 /etc/ntp.keys 文件的指针,并指定密钥标识符。例如

    keys /etc/ntp/ntp.keys
    trustedkey 1
    controlkey 1
    requestkey 1
    
  2. 使用 sudo systemctl restart ntp 命令重启 NTP。

配置 NTP 客户端

NTP 客户端是 Cumulus Linux 交换机。

  1. 创建与您在 NTP 服务器上创建的相同的 .keys 文件 (/etc/ntp.keys)。例如
```
cumulus@switch:~$  sudo nano /etc/ntp.keys
#
# DO NOT USE THE DEFAULT VALUES HERE.
#
#65535  M  akey
#1      M  pass

1  M  CumulusLinux!
```
  1. 编辑 /etc/ntpsec/ntp.conf 文件以指定您要使用的服务器、密钥标识符以及指向您在步骤 1 中创建的 /etc/ntp.keys 文件的指针。例如

    cumulus@switch:~$ sudo nano /etc/ntpsec/ntp.conf
    ...
    # You do need to talk to an NTP server or two (or three).
    #pool ntp.your-provider.example
    # OR
    #server ntp.your-provider.example
    
    # pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
    # pick a different set every time it starts up.  Please consider joining the
    # pool: <http://www.pool.ntp.org/join.html>
    #server 0.cumulusnetworks.pool.ntp.org iburst
    #server 1.cumulusnetworks.pool.ntp.org iburst
    #server 2.cumulusnetworks.pool.ntp.org iburst
    #server 3.cumulusnetworks.pool.ntp.org iburst
    server 10.50.23.121 key 1
    
    #keys
    keys /etc/ntp.keys
    trustedkey 1
    controlkey 1
    requestkey 1
    ...
    
  2. 在活动 VRF(默认或管理)中重启 NTP。例如

    cumulus@switch:~$ systemctl restart ntp@mgmt.service
    
  3. 等待几分钟,然后运行 ntpq -c as 命令以验证配置

    cumulus@switch:~$ ntpq -c as
    
    ind assid status  conf reach auth condition  last_event cnt
    ===========================================================
      1 40828  f014   yes   yes   ok     reject   reachable  1
    

    成功授权后,您会看到以下命令输出

    cumulus@switch:~$ ntpq -c as
    
    ind assid status  conf reach auth condition  last_event cnt
    ===========================================================
      1 40828  f61a   yes   yes   ok   sys.peer    sys_peer  1
    

考虑事项

Cumulus Linux 中的 NTP 使用 /usr/share/zoneinfo/leap-seconds.list 文件,该文件会定期过期,并导致生成有关过期的日志消息。当文件过期时,请从 https://www.ietf.org/timezones/data/leap-seconds.list 更新它,或将 tzdata 软件包升级到最新版本。