TrumanWong

timedatectl

Used to set or query system time, date, time zone and other configurations in Linux.

Supplementary instructions

In Linux operation and maintenance, this command is usually used to set or change the current date, time, and time zone, or to enable automatic system clock synchronization with a remote NTP server to ensure that the Linux system always maintains the correct time.

Summary

timedatectl [OPTIONS...] COMMAND ...

The main purpose

  • Convert time to selected format, default is current.
  • Set system time.

Parameters

Query or change system time and date settings.

   -h --help Display help information.
      --version displays the package version.
      --no-pager Don't pipe output to a pager.
      --no-ask-password Don't prompt for password.
   -H --host=[USER@]HOST operates on the remote host
   -M --machine=CONTAINER Operate on local container.
      --adjust-system-clock Adjust the system clock when changing local RTC mode.
      --monitor monitors the status of systemd-timesyncd
   -p --property=NAME Show only properties with this name
   -a --all Display all attributes, including empty attributes
      --value When displaying attributes, only print the value

Commands:
   status displays the current time setting.
   set-time TIME sets the system time.
   set-timezone ZONE sets the system time zone.
   list-timezones displays known time zones.
   set-local-rtc BOOL controls whether the RTC is in local time. (The value of BOOL can be 1 / true or 0 / false)
   set-ntp BOOL Enable or disable network time synchronization. (The value of BOOL can be 1 / true or 0 / false)
   timesync-status displays the status of systemd-timesyncd.
   show-timesync Shows the properties of systemd-timesyncd.

example

Display the current system time and date

$ timedatectl status
       Local time: Friday 2022-04-08 17:06:40 CST
   Universal time: Friday 2022-04-08 09:06:40 UTC
         RTC time: Friday 2022-04-08 17:04:02
        Time zone: Asia/Shanghai (CST, +0800)
      NTP enabled: n/a
NTP synchronized: no
  RTC in local TZ: yes
       DST active: n/a

Displays all available time zones on the system

$ timedatectl show
Unknown operation show
$ timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa

Set local time zone from Asia/Shanghai to Europe/Amsterdam

$ timedatectl set-timezone "Europe/Amsterdam"

Set local time zone to Coordinated Universal Time (UTC)

$ timedatectl set-timezone UTC

Set system time (format: HH:MM:SS)

$ timedatectl set-time "07:25:46"

Set the system date (format: YYYY-MM-DD)

$ timedatectl set-time "2021-12-12"

If only the date is set, the time will be set to "00:00:00" by default (it is recommended to set the date and time at the same time)

$ timedatectl set-time "2021-12-12 07:25:46"

Set the hardware clock (RTC) to the local time zone (not recommended, it is more appropriate to use UTC for the RTC clock to avoid various problems in time zone changes and daylight saving time adjustments)

$ timedatectl set-local-rtc 1

Set the hardware clock (RTC) to Coordinated Universal Time (UTC)

$ timedatectl set-local-rtc 0

Enable NTP automatic time synchronization

$ timedatectl set-ntp true

Disable NTP automatic time synchronization

$ timedatectl set-ntp false

View the status of the systemd-timesyncd service

$ timedatectl timesync-status
        Server: 91.189.94.4 (ntp.ubuntu.com)
Poll interval: 17min 4s (min: 32s; max 34min 8s)
          Leap: normal
       Version: 4
       Stratum: 2
     Reference: 91EECB0E
     Precision: 1us (-23)
Root distance: 29.922ms (max: 5s)
        Offset: +2.497ms
         Delay: 199.540ms
        Jitter: 5.834ms
  Packet count: 6
     Frequency: +13.039ppm

systemd-timedated may be configured with Google's NTP server (such as time1.google.com) by default. In order to successfully complete network time synchronization, you can edit the /etc/systemd/timesyncd.conf file to add your own NTP server address.