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.
- 1 前言
- 2 Configuration
- 3 时区
- 4 Troubleshooting
- 5 See also
- 6 External resources
- 7 References
Software clock vs Hardware clock
The hardware clock (also known as real-time clock or RTC) is typically a component on the mainboard. It runs independent of the state of the operating system all the time, also when the computer is shutdown.
UTC time vs localtime
The time for hardware clock can be modified to represent two standards: localtime or UTC time. The localtime is the real time of the time zone including DST. Preferred is UTC time because the system time gets computed by adding the time zone difference and DST. So daylight saving changes get automatically applied and changing the time zone are possible without changing the hardware clock. Exceptions can be made when using a dual-boot system; this is when the other operating system does not support or is not configured for an UTC hardware clock (by default, Windows uses localtime).
In order to keep time properly, select the proper time zone so the system knows where it is located.
systemd comes with the timedatectl command to manage the time zone:
To check the current zone:
To list available zones:
To change the time zone, e.g. for Germany:
timedatectl set-timezone Europe/Berlin
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:
To set the system clock, e.g. 12:34, May 6, 2016:
See the NTP article for information concerning the use of time servers.
systemd comes with the timedatectl command to manage the system clock time:
To check the current software clock time:
To set the system clock:
timedatectl set-time "2012-12-17 12:30:59"
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 time:
To set the hardware clock to the current system clock:
Syncing 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.
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'
To check if the hardware time is updated, install net-misc/adjtimex and run:
adjtimex --print | grep status
The 64 bit of the reported number should be unset (0). More information in hwclock man pages (search '11 minute mode').
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 real time clock.
rc-update delete hwclock boot
It 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 time standard. To change to localtime add
clock_hctosys="YES" clock_systohc="YES" # clock="local"
Restart the hwclock service and have the hardware clock init script run on system boot:
rc-service hwclock restart
rc-update add hwclock boot
systemd can be used to set the system clock on boot. Use timedatectl to manage the hardware clock:
To check the current hardware clock time:
timedatectl | grep "RTC time"
To set the hardware clock to the current system clock (UTC time standard):
timedatectl set-local-rtc 0
To set the hardware clock to the current system clock (localtime time standard):
timedatectl set-local-rtc 1
Dual booting with Windows
Systems that dual boot with another operating system, such as Windows, generally have a struggle over the hardware clock. To make Windows not adjust 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.
- Network Time Protocol
- NTP - An implementation of the Network Time Protocol.
- chrony - A versatile implementation of the Network Time Protocol.
- OpenNTPD - Lightweight NTP server ported from OpenBSD.
- 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.