Linux 时间操作及其同步

本文将以 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 的时间状态可由 timedatectltimedatectl 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 的两个可执行文件 chronycchronyd,分别作为客户端和服务端(保护进程)。

其他安装方法也可以在官方网站上找到。

[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/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • JDK的环境变量

    jdk下载并配置下载jdk下图是自己资源管理器中jdk的安装路径,双击然后next就好,不需要改什么配置 手里没有安装包的,下载地址在这里 :https://www.oracle….

    Linux 2023年6月13日
    070
  • python的日期处理

    俗话说,工欲善其事必先利其器,所以在使用日期前要先对日期进行处理,所以时间戳和字符串的来回来去转换这个事肯定是要搞的 这次的函数有一个?有两个?有三个?有四个!上代码! 哈哈,像不…

    Linux 2023年6月6日
    085
  • Ubuntu18.04 显卡驱动安装(解决各种疑难杂症)

    步骤 下载驱动 准备工作 进行安装 检查安装 下载驱动 首先我们需要去官网下载显卡驱动 打开浏览器,在百度搜索框中搜索:显卡驱动 下载 在 手动搜索驱动程序一栏,根据自己的显卡进行…

    Linux 2023年6月14日
    0117
  • Ubuntu系统中防火墙的使用和开放端口

    sudo sudo apt-get install ufw sudo ufw status inactive: 不活跃,未开启 active:开启 sudo ufw enable …

    Linux 2023年5月27日
    085
  • 课间游戏志:斗荧光笔与扒撸咔嚓

    课间游戏志:斗荧光笔与扒撸咔嚓 写这篇博客,主要是想记录两个课间游戏,一个是我于小学四年级时发明的斗荧光笔,一个是初中时班上几个变态发明的扒撸咔嚓,自从这两个游戏被发明以后,我们班…

    Linux 2023年6月6日
    0104
  • Python List 中的append 和 extend 的区别

    方法的参数不同 append 方法是向原list的末尾添加一个对象(任意对象;如元组,字典,列表等),且只占据一个原list的索引位,添加后无返回值,直接在原列表中添加。 list…

    Linux 2023年6月7日
    090
  • 银河麒麟KYLIN安装wireshark进行抓包

    银河麒麟KYLIN安装wireshark进行抓包(前提是网络连通):sudo apt-get updatesudo apt-get install wireshark -y弹出框选…

    Linux 2023年6月13日
    0123
  • DDL(操作表和数据库)

    数据定义语言,用来定义数据库对象:数据库,表,列等 readme 注意本博客中的 操作数据库 查询 show databases; 创建 创建数据库 create database…

    Linux 2023年6月7日
    081
  • 继承、封装、多态的实现原理

    欢迎来到Java学习之继承、封装、多态的实现原理 目录 从JVM结构开始谈多态 JVM 的结构 Java 的方法调用方式 常量池(constant pool) 图 2. 常量池各表…

    Linux 2023年6月13日
    0108
  • tiddlywiki-nodejs 安装部署

    安装 tiddlywiki (root) 确认 npm 包全局&amp…

    Linux 2023年6月14日
    089
  • linux学习之shell脚本

    【实验目的】‍ ‌ 通过本实验练习,使学生了解常用SHELL的编程特点,掌握SHELL 程序设计的基础知识。对SHELL程序流程控制、SHELL程序的运行方式、bash程序的调试方…

    Linux 2023年5月27日
    0130
  • BootstrapTreeView 实现懒加载和点击事件。

    BootstrapTreeView的js下载位置:https://github.com/patternfly/patternfly-bootstrap-treeview。(注意不是…

    Linux 2023年6月7日
    0108
  • 尝试阅读理解一份linux shell脚本

    从头一二去阅读语法和命令说明,对于脚本小白来说比较枯燥,难以坚持,所以这里选择对一份完整的shell脚本代码来逐行逐段解读,希望可以一渡小白,帮助我们快速进入脚本的大门_。司机要开…

    Linux 2023年6月6日
    087
  • 一篇文章剖析设计模式中的简单工厂、工厂方法和抽象工厂

    前言 大部分的面试者在IT行业面试中,提及设计模式,可以列举一大堆,但是面试官要求细说的时候,往往部分基础不够牢固的同学只能提及简单工厂。今天我们来对面试过程中最常见的简单工厂、工…

    Linux 2023年6月13日
    099
  • MySQL表空间回收的正确姿势

    不知道大家有没有遇到这样的一种情况,线上业务在MySQL表上做增删改查操作,随着时间的推移,表里面的数据越来越多,表数据文件越来越大,数据库占用的空间自然也逐渐增长 为了缩小磁盘上…

    Linux 2023年6月13日
    092
  • Pycharm设置python文件头

    设置路径为: File->Setting->Editor->File and code Templates->Python Script 可用的预定义文件模…

    Linux 2023年6月8日
    0111
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球