系统时间

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page System time and the translation is 45% complete.
Outdated translations are marked like this.
Other languages:


系统时间,由系统时钟支持,在 Unix 系统中用于跟踪时间。它可以由板载硬件时钟或外部时间服务器设置.

软件时钟 vs 硬件时钟

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.

The hardware clock (also known as real-time clock or RTC) is typically a component on the mainboard, which keeps time while the computer is switched off. The accuracy of most RTCs varies, it is typical for them to gain or lose several seconds per day. While adequate for most purposes, systems connected to the Internet usually use Network Time Protocol which achieves accuracy to within a few milliseconds.

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 本地时间

System time is always set to local time as determined by the user's time zone, taking daylight saving time (DST) into account.

The hardware clock can represent either local time or Coordinated Universal Time (UTC). UTC is preferred because it is independent of time zones and DST. Some operating systems (most notably Windows) use local time by default, which can cause conflicts on dual-boot systems. Windows can be reconfigured to use UTC, allowing it to stay in sync with Linux; see section Dual booting with Windows.

配置

时区

为了确保时间正确,请选择正确的时区以便系统知道它所在的位置。

OpenRC

systemd

systemd comes with the timedatectl command to manage the time zone:

要检查当前区域:

user $timedatectl

列出可用区域:

user $timedatectl list-timezones

更改时区,例如德国:

root #timedatectl set-timezone Europe/Berlin

LC_TIME

This environment variable defines formatting of dates and times. For more details see The GNU C Library

系统时钟

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.

date 命令可用于管理系统时钟时间:

要检查当前的软件时钟时间:

user $date

设置系统时钟,例如 2016 年 5 月 6 日 12:34:

root #date 050612342016

时间服务器

See the Chrony or Network Time Protocol articles for information concerning the use of time servers.

systemd

systemd 带有 timedatectl 命令来管理系统时钟时间:

要检查当前的软件时钟时间:

user $timedatectl

设置系统时钟:

root #timedatectl set-time "2012-12-17 12:30:59"

硬件时钟

要启用硬件时钟,必须激活以下内核选项:

内核 Necessary kernel options for a hardware clock
    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'

在运行时,检查当前硬件时钟时间:

root #hwclock --show

将硬件时钟设置为当前系统时钟:

root #hwclock --systohc

同步硬件时钟和系统时间

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:

内核 Letting the kernel sync the system clock
    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 status

Bit 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 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. To change to local time add clock="local".

文件 /etc/conf.d/hwclockAdding hardware clock sync
clock_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:

要检查当前硬件时钟时间:

user $timedatectl | grep "RTC time"

将硬件时钟设置为当前系统时钟(UTC 时间标准):

root #timedatectl set-local-rtc 0

将硬件时钟设置为当前系统时钟(本地时间标准):

root #timedatectl set-local-rtc 1

故障排除

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.

另请参阅

外部资源

参考

  1. Comparison of NTP implementations, chrony, March 1st, 2017. Retrieved on July 11th, 2017.
  2. OpenNTPD. OpenNTPD 5.9p1, OpenNTPD, March 29th, 2016. Retrieved on July 11th, 2017.