How to set and keep the exact time in sync on your Debian computer using systemd-timesyncd

botond published March 2024, 01, Thu - 18:20 time

Content

 

Introductory

In the world of modern information technology, the use of accurate time is a critical aspect. In today's Linux operating systems, there are many solutions to ensure that our system's clock is always synchronized with world time. But why is this so important?

  • Many security protocols, such as TLS/SSL, which form the basis of Internet communications, rely heavily on precise timing. Time differences can open security holes that attackers can exploit.
  • Database management systems, file systems, and other critical systems also rely on accurate timestamps. Time differences can lead to data loss or inconsistency.
  • Accurate time is essential when communicating between servers and client systems. This is especially important for network operations such as logging and debugging.
  • Financial transactions and other time-sensitive business processes also rely on reliable time synchronization.
  • When creating system logs, it is also important that the time stamps of data coming from different sources (for example, from other computers, devices) are consistent.

In this short description, we will see how to set the date, the exact time and our time zone manually, and how to activate time synchronization with the Internet.

 

 

Query date and time

The date and time are most easily set by a data we can query using the command:

date

Query date and time using the date command

If we want to display more information, then a timdatectl command can be used:

timedatectl

Query date and time using the timedatectl command

When the Linux operating system starts, it reads the time from the RTC and initializes its own system clock, which is managed by software. The system clock is implemented in the kernel and is based on high-precision timing hardware devices (such as HPET, High Precision Event Timer). This system clock counts the time since the system was turned on and is used by the operating system and applications to determine the current time.

The above commands are therefore a system time is polled, which is polled by the Linux system from the hardware clock (RTC) at startup, and measured by the kernel in software during its run.

It is also possible to query the hardware clock itself, this rootwe can do as hwclock command:

sudo hwclock --show

Query the hardware clock using the hwclock command

Most computers have a small battery powered on their motherboard hardware clock, which is the so-called RTC (Real Time Clock) or CMOS clock. This clock runs continuously, even when the computer is turned off or in a low power state (such as hibernation), and maintains basic time and date information.

 

 

Set date and time

The most convenient way to set the date and exact time is also with the date command:

date -s "YYYY-MM-DD HH:MM:SS"

 Where the setting format is year-month-day hour:minute:second.

Setting date and time using the date command

After successful setting, the command returns the set date and time on its output.

The time can also be set with the timedatectl command:

timedatectl set-time "YYYY-MM-DD HH:MM:SS"

Setting the date and time using the timedatectl command

However, as shown in the picture, if the NTP synchronization (more on this later), then you cannot set the time manually, in which case you receive this error message:

Failed to set time: Automatic time synchronization is enabled

In such cases, NTP synchronization must first be switched off and then switched back on. Of course, this doesn't make any sense in practice, since when NTP synchronization is turned back on, it sets the exact time from the Internet regardless of what we set manually before. With this example, we can only understand the logic of the time management functions more easily.

It is important to note here that these time setting commands only set the system time and not the hardware clock.

On Linux systems, the system clock and the hardware clock (RTC) are usually synchronized at shutdown. This means that if we manually set the system clock using the date or timedatectl commands and then shut down the computer, the system will by default update the RTC with the system clock values ​​during the shutdown process. Therefore, at the next startup, the RTC and the system clock show the same time.

However, if the computer shuts down due to a sudden power failure or if the operating system does not run the shutdown routines properly, it is possible that the changes to the system clock are not updated to the RTC and on the next startup the system clock is reset to the value read from the RTC.

The essence of the process is that when the computer shuts down, the system "writes back" the current system time to the RTC, thus ensuring that the hardware clock is accurate at the next startup. This is important because the RTC is what keeps time when the computer is turned off or in a low power state.

This approach ensures that consistency between system time and hardware time is maintained between reboots and system shutdowns.

The exact behavior depends on your system configuration and the distribution you are using. Some systems are configured to update the RTC based on the system clock only for manual intervention or to run specific services. Manually synchronizing the system clock and hardware clock (RTC) back and forth is covered below.

 

Time zone setting

In addition to setting the date and time, you can also change the time zone if the setting is not correct. The available time zones can be listed with the following command:

timedatectl list-timezones

Listing time zones

List of time zones - Europe/Budapest

If you have found the time zone you want to set in the list, you can set it with the following command:

timedatectl set-timezone Europe/Budapest

It also sets the system clock, so the hardware clock (RTC) is set on shutdown or when synchronized manually.

 

 

Hardware clock (RTC) manual synchronization

If you want to synchronize the hardware clock (RTC) manually, you can do it in both directions using the following commands:

Update system clock based on hardware clock:

sudo hwclock --hctosys

Hardware clock (RTC) manual synchronization - System clock update

Here we can see an example where I first polled the system clock and the hardware clock in a single command line to get the most accurate times. Here you can see that the hardware clock is one second behind the system clock. I then ran the --hctosys option, which set the hardware clock time in the system clock. Then I queried the two times again at the same time. At that time, the seconds also matched.

Update hardware clock based on system clock:

sudo hwclock --systohc

The reverse of the operation, by definition, sets the time of the system clock to the hardware clock (RTC).

A hwclock manual quoted from:

[...]
DATE-TIME CONFIGURATION
   Keeping Time without External Synchronization
       This discussion is based on the following conditions:

       • Nothing is running that alters the date-time clocks, such as NTP daemon or a cron job."

       • The system timezone is configured for the correct local time.  See below, under POSIX vs 'RIGHT'.

       • Early during startup the following are called, in this order:
         adjtimex --tick value --frequency value
         hwclock --hctosys

       • During shutdown the following is called:
         hwclock --systohc
[...]

The point here is in the last item of the list, so the system performs this operation when the system shuts down, so when the computer is turned off, it updates the hardware clock with the exact time of the system clock, as it was already discussed above.

 

Synchronize date and time from the Internet using systemd-timesyncd

With the help of various services from the Internet, we can synchronize the exact world time to our computer, with the help of which the time of our system becomes completely accurate, so that even during communication with other computers, the various network protocols, etc. will work accurately.

In this chapter, we will see how we can activate time synchronization from the Internet systemd-timesyncd using the service.

systemd-timesyncd is a simple NTP (Network Time Protocol) protocol-based client that aims to provide a basic but efficient time synchronization solution for Linux systems without the need to install and configure a full-fledged NTP daemon (such as ntpd or chrony).

To install the service, run the following command:

sudo apt-get install systemd-timesyncd

Install the systemd-timesyncd service

Then we can check whether the synchronization works with the timedatectl command:

timedatectl

Check date and time synchronization

And here we can already see in the "yes" and "active" lines that the synchronization works.

 

 

Conclusion

In this short description, we learned how to view the system time and hardware clock (RTC), how to set them, and how to synchronize our computer's time using the systemd-timesyncd service so that our system's clock is always accurate.