设置日期和时间

本节讨论如何设置时区,以及如何在交换机上的软件时钟上设置日期和时间。要配置 NTP,请参阅网络时间协议 - NTP。要配置 PTP,请参阅精确时间协议 - PTP

设置时区以及软件时钟上的日期和时间需要 root 权限;使用 sudo

显示当前时区、日期和时间

在交换机上显示当前时区、日期和时间

cumulus@switch:~$ nv show system time
                           operational                  
-------------------------  -----------------------------
llocal-time                 Wed 2024-08-21 17:39:44 EDT
universal-time             Wed 2024-08-21 21:39:44 UTC
rtc-time                   Fri 2024-08-16 16:50:06    
time-zone                  US/Eastern (EDT, -0400)    
system-clock-synchronized  no                         
ntp-service                n/a                        
rtc-in-local-tz            no                         
unix-time                  1724276384.1403222 
cumulus@switch:~$ date
Wed 11 Oct 2023 12:18:33 PM UTC

要仅显示时区,请运行 date +%Z 命令

cumulus@switch:~$ date +%Z
UTC

设置时区

您可以使用以下方法之一在交换机上设置时区

  • 运行 NVUE 命令。
  • 使用引导式向导。
  • 编辑 /etc/timezone 文件。

运行 nv set system timezone <timezone> 命令。要查看所有可用的时区,请运行 nv set system timezone 并按 Tab 键。以下示例将时区设置为 US/Eastern

cumulus@switch:~$ nv set system timezone US/Eastern
cumulus@switch:~$ nv config apply
  1. 在终端中,运行以下命令

    cumulus@switch:~$ sudo dpkg-reconfigure tzdata
    
  2. 按照屏幕上的菜单选项选择地理区域和地区。

  1. 编辑 /etc/timezone 文件以添加所需的时区。有关有效时区的列表,请参阅 tz 数据库时区

    cumulus@switch:~$ sudo vi /etc/timezone
    US/Eastern
    
  2. 应用新的时区

    cumulus@switch:~$ sudo dpkg-reconfigure --frontend noninteractive tzdata
    
  3. 更改 /etc/localtime 以反映当前时区

    sudo ln -sf /usr/share/zoneinfo/US/Eastern /etc/localtime
    

设置日期和时间

交换机包含电池供电的硬件时钟,可在交换机断电和重启之间保持时间。当交换机运行时,Cumulus Linux 操作系统维护其自己的软件时钟。

在启动过程中,交换机将时间从硬件时钟复制到操作系统软件时钟。软件时钟负责所有计时。在系统关机期间,交换机将软件时钟复制回电池供电的硬件时钟。

如果您需要重新配置当前时区,请参阅以上说明。

要根据配置的时区设置软件时钟

运行 nv action change system time <clock-date> <clock-time> 命令。以 YYYY-MM-DD 格式指定 <clock-date>,以 HH:MM:SS 格式指定 <clock-time>

cumulus@switch:~$ nv action change system time 2023-12-04 2:33:30
System Date-time changed successfully
Local Time is now Mon 2023-12-04 02:33:30 UTC
Action succeeded
cumulus@switch:~$ sudo date -s "Tue Jan 26 00:37:13 2021"

您可以使用 hwclock 命令将软件时钟的当前值写入硬件时钟

cumulus@switch:~$ sudo hwclock -w

有关更多信息,请参阅 man hwclock(8)