Time zone and precise time setting in Linux

Time zone and precise time setting in Linux

Properly set system time is important for the user, but also for the system itself. Let's explain how Linux accesses time zones and how to set the exact time to get you right.

There is a lot of wailing on the Internet that shows poor clocks after the system is installed. They are usually shifted for a few hours and beginners are desperate for the situation. In fact, there is nothing so complicated, just have the correct system time and the correct time zone.

Hardware time

The Linux kernel will take time of hardware from its startup. Real-time clocks (RTCs) are usually integrated on the motherboard, powered by a battery, and are also running when the computer is turned off. They are not particularly accurate, but they are sufficient for the initial orientation. You can see their settings when entering Bios.

Hardware clocks can also be set from the operating system, with Linux using the hwclock utility. If you run it without parameters, you'll learn what the time is like:

~] hwclock
2024-01-29 14:35:30.995321+01:00

The utility hwclock automatically recalculates the time according to the your local time zone, if necessary. There are two different approaches: either hardware clocks point to local time or more often universal UTC time. Due to daylight saving time when the computer is off, it is strongly recommended to use UTC hardware hours. Beware, however, that this may cause a dualboot problem with Windows, which in turn only counts clocks in local time.

The hwclock utility keeps the information in the /etc/adjtime file. This may look like

~] cat /etc/adjtime
0.000000 1693830353 0.000000
1693830353
UTC

The first line contains information about the systematic hardware clock error at the beginning. Although they are not accurate, but their error is stable and the value is stored in seconds per day (of course it may be negative). In addition, there is also the time the last clocks of the system were set (in seconds of UNIX time, from January 1, 1970).

The last line then shows information about whether hardware clocks show local time or universal. LOCAL or UTC may be entered here. If the file is missing or this information is not available, it is considered the default universal time.

The utility overwrites the file while it sets the hardware clock. You can force this by using the -w or --systohc parameter. You can also change whether clocks are ticked by local time --localtime or UTC --utc.

If you want to see more details about the hardware clock status, call the utility in debug mode:

~] hwclock --verbose
hwclock from util-linux 2.38.1
System Time: 1706535576.159993
Trying to open: /dev/rtc0
Using the rtc interface to the clock.
Last drift adjustment done at 1693830353 seconds after 1969
Last calibration done at 1693830353 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2024/01/29 13:39:37
Hw clock time : 2024/01/29 13:39:37 = 1706535577 seconds since 1969
Time since last adjustment is 12705224 seconds
Calculated Hardware Clock drift is 0.000000 seconds
2024-01-29 14:39:35.993830+01:00

If the clock is wrong, we can manually set it to the desired value:

~] hwclock --set --utc --date='2024-01-29 13:14:05'

So clocks are ticking right and the system can load them at startup. If we want to request transmission from hardware to system clocks, we can use the -s or long version -hctosys.

Time Zones

At this point, the system knows how many hours in UTC is. To show us local time, he must know where we are and what time zone to use. The so-called tz database, sometimes referred to as the tsunami, zoneinfo database or IANA time zone database, is used for this purpose. It is created as a public domain and is available on the IANA website .

It is, in fact, a database of time zones divided by country. It also contains all historical data, so that it can be correctly recalculated to local time also for historical time stamps. The database is updated on a regular basis as information about time zones changes increases.

The database is distributed in human-readable text, including comments, for use in Linux it must be compiled into a platform-independent binary form.
This will be ensured, for example, by the zic compiler found in the basic libc-bin system package.

But you will not need it unless you plan to modify time zones for some special purpose. The tzdata package now comes with compiled time zones divided by continents into directories. You can find them in /usr/share/zoneinfo directory. The data is loaded primarily from the GNU C library (glibc), which then offers standard time acquisition features.

Time Zone Setup

The time zone used in your linux distro is in the /etc/localtime file. Typically, data is not copied here, only a symbolic link is created that points to the correct file:

~] ls -alFh /etc/localtime
lrwxrwxrwx 1 root root 33 Jan 29 14:02 /etc/localtime -> /usr/share/zoneinfo/Europe/Prague

The second option is to use the timedatectl configuration utility in modern distributions, which offers an interface for listing and setting time zones. It does, of course, the same, in the alternative, in the end.

~] timedatectl list-timezones
...
Europe/Athens
Europe/Belgrade
Europe/Berlin
Europe/Bratislava
Europe/Brussels
Europe/Bucharest
Europe/Budapest
Europe/Busingen
Europe/Chisinau
Europe/Copenhagen
Europe/Dublin
Europe/Gibraltar
...

You can use dpkg-reconfigure utility for debian based distros for time zone change too.

~] dpkg-reconfigure tzdata

change debian/ubuntu timezone change debian/ubuntu timezone

In any case, the date command should now display the correct local time, including displaying the correct zone:

~] date
Mon Jan 29 02:50:41 PM CET 2024

Automatic Time Setting

If we have the right time zone and set up our watch correctly, we definitely want our time to be kept automatically. To do this, use the NTP protocol, which can use the time servers on the Internet to customize system clock settings.

To synchronize, you'll need a daemon that controls the NTP protocol and can adjust the clock speed in the kernel. Various implementations can be used, including NTP, OpenNTPD, or Chrony. The Chrony is default ntp server in RHEL and Derivatives today, and is recommended for faster synchronization, more stable operation on sleeping systems, and better disconnection from the network.

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus