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.
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 2018-09-24 16:28:05.166779+0200
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.
hwclock utility keeps the information in the
/etc/adjtime file. This may look like
]$ cat /etc/adjtime 0.000013 1512830100 0.000000 1512830100 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.
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
--systohc parameter. You can also change whether clocks are ticked by local time
--localtime or UTC
If you want to see more details about the hardware clock status, call the utility in debug mode:
]$ hwclock -D hwclock from util-linux 2.20.1 Using /dev interface to clock. Last drift adjustment done at 1512830100 seconds after 1969 Last calibration done at 1512830100 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: 2018/09/24 14:40:07 Hw clock time : 2018/09/24 14:40:07 = 1537800007 seconds since 1969 Mon 24 Sep 2018 04:40:07 PM CEST -0.266337 seconds
If the clock is wrong, we can manually set it to the desired value:
]$ hwclock --set --utc --date='2019-09-24 14:45: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
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.
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 Aug 10 13:09 /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
In any case, the date command should now display the correct local time, including displaying the correct zone:
]$ date Mon Sep 24 16:54:45 CEST 2018
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.