Linux timedatectl command examples
Date Command Examples
The "timedatectl" command is a new replacement to the traditional date command found on Linux systems. "timedatectl" is part of the systemd system.
"timedatectl" may be used to query and change the system clock and its settings and modify your time zone. It can also enable and disable time synchronization services.
Display your current time/date and time zone settings
Issuing the "timedatectl" with no additional parameters will display the output below. Your current time and date are displayed along with your time zone. You current synchronization status is also displayed.
# timedatectl Local time: Wed 2021-06-02 21:02:38 BST Universal time: Wed 2021-06-02 20:02:38 UTC RTC time: Wed 2021-06-02 20:02:38 Time zone: Europe/London (BST, +0100) System clock synchronized: yes NTP service: active RTC in local TZ: no
Setting your time zone
Your time displayed will always be based around the time zone that has been configured on your system. To configure your time zone if it is not already set to the correct location, you can issue the following commands.
The above command will display all available time zones on your system. If you know the region you are in, you can narrow your search down by combing the command with "grep". For example If my time zone was in the "UK", I could issue the following command.
timedatectl list-timezones | grep -i london
The above command would search through all the time zones looking for the string "london". The "-i" on the "grep" command is used to ignore case.
# timedatectl list-timezones | grep -i london Europe/London
If we wanted to change the current time zone to be "America/New_York", we would issue the following command:
timedatectl set-timezone "America/New_York"
# timedatectl set-timezone "America/New_York" # timedatectl status Local time: Wed 2021-06-02 16:17:11 EDT Universal time: Wed 2021-06-02 20:17:11 UTC RTC time: Wed 2021-06-02 20:17:10 Time zone: America/New_York (EDT, -0400) System clock synchronized: yes NTP service: active RTC in local TZ: no
From the above output, we can see that the time zone has now been changed to be "America/New_York". Notice, the time has also change now to match the time zone
Manually Configuring the time
It is possible to use the "timedatectl" command to manually configure the time on your Linux system. Generally, most Linux systems will be using some form of time synchronization, on newer systems this will be "chronyd" and on older systems this will be "ntpd". Both of this time synchronization daemons will keep your systems time synchronized to an external time source for accuracy. Many larger organizations will have their own time sources, however, there are plenty of public time servers that can be used for this purpose.
If you try to change the time on a system that is using "Time Synchronization", you will receive an error similar to the one below:
Failed to set time: NTP unit is active
# timedatectl set-time 20:15:00 Failed to set time: NTP unit is active
To amend the time on a system using time synchronization, you will first need to stop the "chronyd" or "ntpd" service.
The format for changing the time is as follows:
timedatectl set-time HH:MM:SS (Hours:Minutes:Seconds)
In the example below, I will stop the "chronyd" service and then modify the time manually.
# systemctl stop chronyd # systemctl disable chronyd Removed /etc/systemd/system/multi-user.target.wants/chronyd.service. # timedatectl set-time 20:00:00 # timedatectl Local time: Wed 2021-06-02 20:00:04 BST Universal time: Wed 2021-06-02 19:00:04 UTC RTC time: Wed 2021-06-02 19:00:04 Time zone: Europe/London (BST, +0100) System clock synchronized: no NTP service: inactive RTC in local TZ: no
From the above output, we can see that the time has now been set to "20:00:00" and the NTP service is showing as inactive.
Manually setting the time and date
The time and date can be set together in one command. The syntax of the command is as follows:
timedatectl set-time 'YYYY-MM-DD HH:MM:SS'
# timedatectl set-time '2021-04-20 19:00:00' # timedatectl Local time: Tue 2021-04-20 19:00:03 BST Universal time: Tue 2021-04-20 18:00:03 UTC RTC time: Tue 2021-04-20 18:00:03 Time zone: Europe/London (BST, +0100) System clock synchronized: no NTP service: inactive RTC in local TZ: no
If you need to reactivate the "chronyd" service, issue the following commands:
# systemctl enable chronyd # systemctl start chronyd
chronyd - chrony daemon
chronyd is a daemon for synchronization of the system clock. It can synchronize the clock with NTP servers. The configuration file for "chronyd" can be found at the following location: /etc/chrony.conf".
Below is an example of a default "chrony.conf" file from a CentOS 8 system.
# These servers were defined in the installation: pool 2.centos.pool.ntp.org iburst # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift # Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3 # Enable kernel synchronization of the real-time clock (RTC). rtcsync # Enable hardware timestamping on all interfaces that support it. #hwtimestamp * # Increase the minimum number of selectable sources required to adjust # the system clock. #minsources 2 # Allow NTP client access from local network. #allow 192.168.0.0/16 # Serve time even if not synchronized to a time source. #local stratum 10 # Specify file containing keys for NTP authentication. keyfile /etc/chrony.keys # Get TAI-UTC offset and leap seconds from the system tz database. leapsectz right/UTC # Specify directory for log files. logdir /var/log/chrony # Select which information is logged. #log measurements statistics tracking
The actual time synchronization server in use is found on the second line: 2.centos.pool.ntp.org. The "pool" directive is used as the address has multiple IP addresses associated with the DNS name.:
# dig 2.centos.pool.ntp.org +short 188.8.131.52 184.108.40.206 220.127.116.11 18.104.22.168
The "iburst" option specifies that the when a server is unreachable it should send a burst of eight packets instead of the usual one packet. This process is also used for speeding up the initial synchronization process.
chronyd diagnostic commands
The above tracking command will display parameters about the system’s clock performance.
# chronyc tracking Reference ID : A29FC87B (time.cloudflare.com) Stratum : 4 Ref time (UTC) : Wed Jun 02 20:56:47 2021 System time : 0.000266695 seconds fast of NTP time Last offset : +0.000310321 seconds RMS offset : 0.001164054 seconds Frequency : 10.591 ppm fast Residual freq : +0.112 ppm Skew : 5.596 ppm Root delay : 0.027314197 seconds Root dispersion : 0.001606827 seconds Update interval : 64.9 seconds Leap status : Normal
The chronyc sources command will display the servers that are being used for time synchronization.
# chronyc sources 210 Number of sources = 4 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^? tick.ntp.infomaniak.ch 0 6 0 - +0ns[ +0ns] +/- 0ns ^* time.cloudflare.com 3 7 377 124 -331us[ -393us] +/- 15ms ^- fluffykins.positive-inte> 3 6 377 59 +463us[ +463us] +/- 62ms ^- 22.214.171.124 1 7 45 181 -1630us[-1692us] +/- 13ms
Below is a guide to the flags displayed from the "chronyc sources" command above:
M: The mode of the source.
^ means a server.
= means a peer.
# indicates a locally connected reference clock.
S: The state of the sources.
* indicates the source to which chronyd is currently synchronized.
+ indicates acceptable sources that are combined with the selected source.
- indicates acceptable sources that are excluded by the combining algorithm.
? indicates sources to which connectivity has been lost or whose packets do not pass all tests.
x indicates a clock that chronyd thinks is a false ticker, that is, its time is inconsistent with a majority of other sources.
~ indicates a source whose time appears to have too much variability.
The ? condition is also shown at start-up, until at least three samples have been gathered from it.
Name/IP address: This shows the name or the IP address of the source, or reference ID for reference clocks.