System time

System time on an Ubuntu Core 20+ device is set and updated over several potential steps, depending on network access and the availability of a real-time clock (RTC).

Regardless of how system time is set, it always moves forward monotonically and is never permitted to move backwards.


System time update process

The system time value and update process is tightly coupled to the boots process and device capabilities. From a fresh boot of the device, the process for setting the time is as follows:

  1. initrd
    When the device boots, initrd first moves system time forward to match the time initrd was built.

    • snap-bootstrap
      snap-bootstrap is the main executable run during the early initrd boot stage. It will itself move time forward to that of the most recent assertion it trusts, signed with Canonical’s key.
  2. user-space
    After the snap environment is re-executed and the boot process moves to user-space, systemd’s timesyncd will attempt to update the system time:

    • on devices with an RTC
      System time is moved forward to match that of the on-board RTC.
    • on devices without an RTC
      System time is taken from the modified file timestamp (mtime) of /var/lib/systemd/timesync/clock on the data partition (which may be encrypted). The timestamp for this file is updated periodically after a network connection has been established, and before the system reboots.

    When the network becomes available, the device will begin to use NTP to periodically update the system time. See Network time synchronisation below for more details.

Current time

The current system time and date can be retrieved with the timedatectl command:

$ timedatectl
               Local time: Mon 2022-03-14 12:23:24 UTC
           Universal time: Mon 2022-03-14 12:23:24 UTC
                 RTC time: Mon 2022-03-14 12:23:25
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Real-time clock (RTC) check

If no real-time clock is present or accessible, the output from timedatectl will include RTC time: n/a:

$ timedatectl
               Local time: Mon 2022-03-14 12:23:24 UTC
           Universal time: Mon 2022-03-14 12:23:24 UTC
                 RTC time: n/a
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Setting a timezone

The timezone can be configured with the system.timezone system value:

$ snap set system system.timezone="America/Chicago"
$ timedatectl | grep zone
                Time zone: America/Chicago (CDT, -0500)

Network time synchronisation

By default, timesyncd is configured to use the Network Time Protocol (NTP) for network time synchronisation as soon as the network becomes available.

The NTP server and update frequency can be checked with timedatectl timesync-status:

$ timedatectl timesync-status
       Server: 91.189.89.198 (ntp.ubuntu.com)
Poll interval: 4min 16s (min: 32s; max 34min 8s)
         Leap: normal
      Version: 4
      Stratum: 2
    Reference: 11FD22FB
    Precision: 1us (-23)
Root distance: 30.234ms (max: 5s)
       Offset: +33.938ms
        Delay: 14.975ms
       Jitter: 37.394ms
 Packet count: 4
    Frequency: -14.982ppm

timedatectl can also be used to set the timezone. I feel this is an important alternative to using snap.

First, the list of available timezones can be obtained using:

timedatectl list-timezones

Then, the timezone can be set using:

sudo timedatectl set-timezone Africa/Kampala

while this is indeed true, UbuntuCore rarely has even shell access or user accounts enabled in production systems, while there is a REST API and most likely a management snap that can use the API equivalent of “snap set”…

(note that we are in the UbuntuCore documentation category here … a system that is 100% snap-only)

1 Like

@degville, I think the following information is wrong.

When the device boots, initrd first moves system time forward to match that of the time when the kernel snap was built.

It should be initrd build date rather than the kernel-snap build date.

1 Like