本文将以 Arch Linux 为例,讨论 Linux 的时间操作和同步方法。
操作系统的时间 (clock) 由三或四部分决定:
- 时间值;
- 是否为 UTC 时间;
- 时区;
- (如果有)夏令时。
操作系统时间一般有两种:硬件时间和系统时间。
[En]
There are generally two kinds of operating system time: hardware time and system time.
硬件时间 (也即真实时间 (Real Time Clock, RTC) 或 CMOS 时钟) 一般只存储时间值,直至 2016 年后 UEFI 硬件支持对时区和夏令时的存储。
系统时间 (也即软件时间) 可以追踪时间值、时区以及可能存在的夏令时。系统时间由 Linux 内核计算,时间值为 UTC+0 下自 1970 年 1 月 1 日午夜至今的秒数——可以在 time.is 网站查询得到。
特别需要注意的是,操作系统完全启动后,系统时间和硬件时间是独立的。
[En]
It is particularly important to note that after the operating system is fully booted, the system time and hardware time are * independent * .
Linux 的时间状态可由 timedatectl
或 timedatectl status
命令获取。输出类似于:
Local time: Sun 2022-02-27 12:52:24 UTC
Universal time: Sun 2022-02-27 12:52:24 UTC
RTC time: n/a
Time zone: UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
我们可以得知:
上方的示例中硬件时间 (RTC time) 显示为”n/a”,也就是不存在。在一部存在硬件时间的 Linux 设备上,可以执行 hwclock --show
查看硬件时间:
$ hwclock --show
2022-02-27 21:14:16.129670+08:00
比 timedatectl
更进一步的是, hwclock
可以显示有关硬件时间的更详细信息:
$ hwclock --verbose
hwclock from util-linux 2.34
System Time: 1645967662.035542
Trying to open: /dev/rtc0
Using the rtc interface to the clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2022/02/27 13:14:24
Hw clock time : 2022/02/27 13:14:24 = 1645967664 seconds since 1969
Time since last adjustment is 1645967664 seconds
Calculated Hardware Clock drift is 0.000000 seconds
2022-02-27 21:14:23.020178+08:00
不再对此输出进行进一步介绍。
[En]
No further introduction to this output.
我们很少手动设置硬件时间。要设置它,可以使其与系统时间一致:
[En]
We rarely set the hardware time manually. To set it, you can make it consistent with the system time:
sudo hwclock --systohc
这会新建或更新 /etc/adjtime
的内容,示例如下:
$ cat /etc/adjtime
0.000000 1645969156 0.000000
1645969156
LOCAL
sudo timedatectl set-time "yyyy-MM-dd hh:mm:ss"
例如:
sudo timedatectl set-time "2014-05-26 11:13:54"
在设置时区之前,您需要了解可用的时区:
[En]
Before setting the time zone, you need to know the available time zone:
timedatectl list-timezones
其中有”Asia/Shanghai”,可将时区设置为此:
sudo timedatectl set-timezone Asia/Shanghai
这里所说的同步是指系统时间与其他服务器提供的时间同步。有两种方法可以做到这一点。
[En]
The synchronization mentioned here refers to the synchronization of system time with the time provided by other servers. There are two ways to do this.
首先启用该服务,执行 systemctl start systemd-timesyncd
。可以选择加入自启动项。
打开 /etc/systemd/timesyncd.conf
文件,其中的内容可能如下:
[Time]
#NTP=
#FallbackNTP=0.arch.pool.ntp.org 1.arch.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
#SaveIntervalSec=60
删除前两行中的注释记号,并按如下方式进行修改:
[En]
Delete the keynotes in the first two lines and modify them as follows:
NTP=0.cn.pool.ntp.org 1.cn.pool.ntp.org 2.cn.pool.ntp.org 3.cn.pool.ntp.org
FallbackNTP=0.arch.pool.ntp.org 1.arch.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org
若要验证配置,执行 timedatectl show-timesync --all
。一般输出类似于:
LinkNTPServers=
SystemNTPServers=
FallbackNTPServers=0.arch.pool.ntp.org 1.arch.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org
ServerName=0.arch.pool.ntp.org
ServerAddress=103.47.76.177
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=1min 4s
NTPMessage={ Leap=0, Version=4, Mode=4, Stratum=2, Precision=-21, RootDelay=177.398ms, RootDispersion=142.196ms, Reference=C342F10A, OriginateTimestamp=Mon 2018-07-16 13:53:43 +08, ReceiveTimestamp=Mon 2018-07-16 13:53:43 +08, TransmitTimestamp=Mon 2018-07-16 13:53:43 +08, DestinationTimestamp=Mon 2018-07-16 13:53:43 +08, Ignored=no PacketCount=1, Jitter=0 }
Frequency=22520548
但是,如果出现类似以下内容的输出:
[En]
However, if there is output similar to the following:
LinkNTPServers=
SystemNTPServers=0.cn.pool.ntp.org 1.cn.pool.ntp.org 2.cn.pool.ntp.org 3.cn.pool.ntp.org
FallbackNTPServers=0.pool.ntp.org 1.pool.ntp.org 2.arch.pool.ntp.org 3.arch.pool.ntp.org
ServerName=
ServerAddress=
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=0
Frequency=0
请跳过本小节,跳转至 chrony。
若要使配置生效,执行 timedatectl set-ntp true
。
同步过程需要持续一段时间。若要检查同步状态,执行 timedatectl status
。输出类似于:
Local time: Thu 2015-07-09 18:21:33 CEST
Universal time: Thu 2015-07-09 16:21:33 UTC
RTC time: Thu 2015-07-09 16:21:33
Time zone: Europe/Amsterdam (CEST, +0200)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
若要查看详细信息,执行 timedatectl timesync-status
。输出类似于:
Server: 103.47.76.177 (0.arch.pool.ntp.org)
Poll interval: 2min 8s (min: 32s; max 34min 8s)
Leap: normal
Version: 4
Stratum: 2
Reference: C342F10A
Precision: 1us (-21)
Root distance: 231.856ms (max: 5s)
Offset: -19.428ms
Delay: 36.717ms
Jitter: 7.343ms
Packet count: 2
Frequency: +267.747ppm
此包在大多数软件管理器中都可用。执行:
[En]
This package is available in most software managers. Execute:
sudo pacman -S chrony
这将在 /usr/bin
下放置 chrony 的两个可执行文件 chronyc
和 chronyd
,分别作为客户端和服务端(保护进程)。
其他安装方法也可以在官方网站上找到。
[En]
Other installation methods can also be found on the official website.
配置文件一般为 /etc/chrony.conf
或 /etc/chrony/chrony.conf
。
打开配置文件并进行以下更改:
[En]
Open the configuration file and make the following changes:
- 定位到
pool
配置项,修改其后的服务器地址为0.cn.pool.ntp.org
; - 定位到
makestep
配置项,根据需求修改; - 第一个数字:进行”时间跳跃”的阈值——同步时若系统时间与服务器返回结果相差在阈值内,则程序会逐渐调整当前系统时间;反之则会直接将系统时间设为正确时间( 注意:“时间跳跃”可能会对其他程序造成负面影响,不宜将此值调至过小);
- 第二个数字:可进行时间调整的范围——设置为
n
表示仅前n
次时间更新过程可发生这种调整。 - 定位到
logdir
配置项,删去配置记号; - 定位到
rtcsync
配置项,删去注释记号。
启用该服务,执行 systemctl start chronyd
。可以选择加入自启动项。重启,读取系统时间检查:
Local time: Sun 2022-02-27 23:16:28 CST
Universal time: Sun 2022-02-27 15:16:28 UTC
RTC time: n/a
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
观察到”System clock synchronized”栏的输出是”yes”。
若要立刻更改系统时间,执行 chronyc makestep
。
Original: https://www.cnblogs.com/littleye233/p/15943989.html
Author: 小叶Little_Ye
Title: Linux 时间操作及其同步
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/524054/
转载文章受原作者版权保护。转载请注明原作者出处!