System time/cs
The system time, backed by the system clock, is used in Unix systems to keep track of time. It can be set by an onboard hardware clock or by an external time server.
Preface
Software clock vs Hardware clock
The system clock, provided by the kernel, is the amount of time that has elapsed since the 1 January 1970 00:00:00 UTC epoch. This is called Unix time.
hardware clock (také znám jako RTC) je většinou komponent na základní desce. Běži nonstop — i když je počítač odpojen z elektrické sítě.
In this case, the system will have the slightly incorrect time from the RTC only briefly, with NTP correcting it shortly after networking is established. The RTC can then be corrected, and in this manner will only drift significantly if the machine is left switched off for prolonged periods.
Some systems such as the Raspberry Pi (models up to 4) lack an RTC altogether. As such, these rely on NTP to start up with the correct time automatically.
UTC vs local time
Systémový čas je vždy nastaven na localtime, který je zjištěn pomocí nastaveného časového pásma time zone. Localtime bere do úvahy daylight saving time (DST).
Čas, který bude držet BIOS může být upraven aby reprezentoval lokální čas, nebo Koordinovaný světový čas (UTC). UTC je preferovaný, protože není závislý na časových zónách a DST. Vyjímky lze učinit při používání dual-bootu, když další operační systém nepředpokládá čas v BIOSu na UTC, například Windows (toto může být pozměněno — přečtěte si sekci Dual booting s Windows).
Configuration
Time zone
In order to keep time properly, select the proper time zone so the system knows where it is located.
OpenRC
systemd
systemd comes with the timedatectl command to manage the time zone:
To check the current zone:
user $timedatectlTo list available zones:
user $timedatectl list-timezonesTo change the time zone, e.g. for Germany:
root #timedatectl set-timezone Europe/BerlinLC_TIME
This environment variable defines formatting of dates and times. For more details see The GNU C Library
System clock
Typically the system clock time is set up by the hardware clock on boot. Alternatively it is possible to manually set the system clock or use a network time server.
The date command can be used to manage the system clock time:
To check the current software clock time:
user $dateTo set the system clock, e.g. 12:34, May 6, 2016:
root #date 050612342016Time server
See the Chrony or Network Time Protocol articles for information concerning the use of time servers.
systemd
systemd comes with the timedatectl command to manage the system clock:
To check the current software clock:
user $timedatectlTo set the system clock:
root #timedatectl set-time "2012-12-17 12:30:59"Hardware clock
To have a hardware clock, the following kernel options must be activated:
Device Drivers --->
[*] Real Time Clock --->
[ ] Set system time from RTC on startup and resume
[ ] Set the RTC time based on NTP synchronization
[*] /sys/class/rtc/rtcN (sysfs)
[*] /proc/driver/rtc (procfs for rtc0)
[*] /dev/rtcN (character devices)
<*> PC-style 'CMOS'
At runtime, to check the current hardware clock:
root #hwclock --showTo set the hardware clock to the current system clock:
root #hwclock --systohcSyncing the hardware clock and system time
Typically the hardware clock is used to setup the system clock on boot. This can be done by the kernel itself or by a boot service (init script). Also on shutdown the kernel or a service can write the software clock to the hardware clock. This aids the system in having the correct time on boot.
In-kernel method
On a sufficiently modern kernel (3.9 or newer), Linux can be configured to handle setting the system time automatically. To do so, enable the Set system time from RTC on startup and resume (CONFIG_RTC_HCTOSYS) and Set the RTC time based on NTP synchronization (CONFIG_RTC_SYSTOHC) kernel options:
Device Drivers --->
[*] Real Time Clock --->
[*] Set system time from RTC on startup and resume
(rtc0) RTC used to set the system time
[*] Set the RTC time based on NTP synchronization
(rtc0) RTC used to synchronize NTP adjustment
[*] /sys/class/rtc/rtcN (sysfs)
[*] /proc/driver/rtc (procfs for rtc0)
[*] /dev/rtcN (character devices)
<*> PC-style 'CMOS'
The Set the RTC time based on NTP synchronization kernel option is currently supported by chrony[1], NTP and OpenNTPD since version 5.9p1[2].
To check the hardware time was updated, install net-misc/adjtimex and run:
root #adjtimex --print | grep statusBit 6 of the reported number should be unset (0). More information in hwclock man pages (search '11 minute mode').
OpenRC
When using OpenRC the hwclock init script can set the system clock on boot and sync system time to the hardware clock on shutdown. The service is enabled by default and should be disabled in favor of the above mentioned in-kernel method. The hwclock script should not be run when using the kernel's RTC.
root #rc-update delete hwclock bootIt could however happen hwclock being started as dependency of another rc-service, e.g. sysklogd. In this case the rc-service osclock should be added to the same runlevel as the dependent rc-service.
If however there is a need for using the OpenRC, set both clock_hctosys and clock_systohc to YES in /etc/conf.d/hwclock. By default the service is configured for UTC. To change to local time add clock="local".
/etc/conf.d/hwclockAdding hardware clock syncclock_hctosys="YES"
clock_systohc="YES"
# clock="local"
Restart the hwclock service and have the hardware clock init script run on system boot:
root #rc-service hwclock restart
root #rc-update add hwclock boot
systemd
systemd can be used to set the system clock on boot. Use timedatectl to manage the hardware clock:
To check the current hardware clock:
user $timedatectl | grep "RTC time"To set the hardware clock to the current system clock (in UTC):
root #timedatectl set-local-rtc 0To set the hardware clock to the current system clock (in the local time):
root #timedatectl set-local-rtc 1Troubleshooting
Dual booting with Windows
Historically, Unix systems have set their RTC to represent UTC. Other operating system, such as Windows, expect the RTC to represent local time by default.
This can lead to a difficulty when dual-booting: the time being correct on one operating system causes it to be incorrect on the other. When either uses NTP to obtain the time, they then 'correct' the RTC, only for the situation to revert when the other does the same, seemingly 'fighting' over the RTC, and resulting in the clock being incorrect by several hours.
To avoid Windows adjusting the hardware clock back to local time, add the following registry entry:
For 64-bit Windows, open regedit then browse to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation. Create a new QWORD entry called RealTimeIsUniversal, then set its value to 1. Reboot the system. The clock should now be in UTC time. For 32-bit Windows, follow the 64-bit instructions except use DWORD instead of QWORD.
See also
- Network Time Protocol — used to synchronize the system time with other devices over the network.
- Chrony — a versatile implementation of the Network Time Protocol (NTP).
- OpenNTPD — a lightweight NTP server ported from OpenBSD.
External resources
- https://lifehacker.com/5742148/fix-windows-clock-issues-when-dual-booting-with-os-x - Dual booting with MS Windows, set RealTimeIsUniversal. Also tested with Windows 10.
- http://tldp.org/HOWTO/Clock-2.html - The Clock Mini-HOWTO.
References
- ↑ Comparison of NTP implementations, chrony, March 1st, 2017. Retrieved on July 11th, 2017.
- ↑ OpenNTPD. OpenNTPD 5.9p1, OpenNTPD, March 29th, 2016. Retrieved on July 11th, 2017.