Linux时间操作及其同步
本文围绕 Linux 的时间将 ArchWiki 上 System time 页面的部分内容做简化,并整理其他文章作为补充,简化理论性,增强可操作性。概述
操作系统的时间 (clock) 由三或四部分决定:时间值;是否为 UTC 时间;时区;(如果有)夏令时。
而操作系统的时间一般有两种:硬件时间和系统时间。硬件时间
硬件时间 (也即真实时间 (Real Time Clock, RTC) 或 CMOS 时钟) 一般只存储时间值,直至 2016 年后 UEFI 硬件支持对时区和夏令时的存储。系统时间
系统时间 (也即软件时间) 可以追踪时间值、时区以及可能存在的夏令时。系统时间由 Linux 内核计算,时间值为 UTC+0 下自 1970 年 1 月 1 日午夜至今的秒数——可以在 time.is 网站查询得到。
尤其需要注意的是:操作系统完全启动后,系统时间与硬件时间是独立的。读取状态
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
我们可以得知:本地时间值和 UTC 时间值均为 Sun 2022-02-27 12:52:24 UTC;硬件时间不存在;时区为 UTC (UTC+0);已经开启系统时间同步。硬件时间
上方的示例中硬件时间 (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
对此输出不再作进一步介绍。设置硬件时间(与系统时间一致)
我们一般很少手动设置硬件时间。若要设置,可以使其与系统时间保持一致: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"时区
设置时区前,先需要知道可用的时区:timedatectl list-timezones
其中有"Asia/Shanghai",可将时区设置为此:sudo timedatectl set-timezone Asia/Shanghai同步
此处提到的同步指的是系统时间与其他服务器提供的时间同步。以下提供两种方式。systemd-timesyncd 服务配置
首先启用该服务,执行 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
删去前两行的注释记号,并作如下修改: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
但若出现类似如下的输出: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.747ppmchrony
chrony 是一个漫游友好型、且专为非所有时间在线的系统设计的程序,可以用以同步系统时间。安装
大部分软件管理器中均有该软件包。执行:sudo pacman -S chrony
这将在 /usr/bin 下放置 chrony 的两个可执行文件 chronyc 和 chronyd,分别作为客户端和服务端(保护进程)。
也可以在官网中查阅其他安装方式。配置
配置文件一般为 /etc/chrony.conf 或 /etc/chrony/chrony.conf。
打开配置文件,做如下更改:定位到 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。参考https://wiki.archlinux.org/title/System_timehttps://wiki.archlinux.org/title/Systemd-timesyncdhttps://www.tecmint.com/install-chrony-in-centos-ubuntu-linux/
转载于博客园 https://blog.imakiseki.cf/2022/02/27/techdev/linux-time-operations-and-sync/