Linux 时间操作及其同步
完整格式支持链接:https://blog.imakiseki.cf/2022/02/27/techdev/linux-time-operations-and-sync/
本文将以 Arch Linux 为例,讨论 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.747ppm
chrony
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_time
- https://wiki.archlinux.org/title/Systemd-timesyncd
- https://www.tecmint.com/install-chrony-in-centos-ubuntu-linux/
Linux 时间操作及其同步的更多相关文章
- Linux 时间设置和同步服务
修改日期时间的工具 date hwclock timedatectl date工具的使用 作用:显示和设置系统时间 选项: -d <字符串> 显示字符串所指的日期与时间,比如:" ...
- Linux时间设置及同步
Linux系统安装时选择的UTC时间是国际标准时间,而中国处于UTC+8时区,因此安装系统时不要选择UTC时区. 还有就是Linux有两个时钟: 1.Bios时钟及硬件时间 2.Kernel时钟及系统 ...
- linux系统时间修改及同步
时间修改date 月日时分年.秒date -s可以直接设置系统时间 比如将系统时间设定成1996年6月10日的命令如下.#date -s 06/10/96将系统时间设定成下午1点12分0秒的命令如下. ...
- linux下时间操作1
本文是对我之前写的文章:C++时间操作 的更深入补充.之前那个文章就是一个快速入门的东西,后面力图把一些更深入的细节补充完整. 时间分类的基本介绍 在介绍一些时间相关的操作函数之前,先来介绍一下lin ...
- linux时间无法同步的解决方案
问题描述: 在家里的ubuntu和windows上始终无法同步时间,也参考了很多资料还是不行.无意中的一次把笔记本拿到公司,发现可以同步时间,但是就觉得奇怪了.对比之后才发现是移动的网络没法连接ntp ...
- linux 设置与网络同步的时间
#设置linux时间 与网络时间同步安装命令yum install -y ntp同步网络时间ntpdate ntp1.aliyun.com
- linux时间不同步问题
怪问题: 时间同步失效 系统: centos 6.6 2.6.32-504.el6.x86_64 情况: 定时任务中写了每分钟同步一次系统时间,定时任务执行成功,时间却未同步,奇怪? 现象: [ro ...
- linux 打印系统时间操作
版权为个人所有,如需转载请说明出处.(东北大亨) http://www.cnblogs.com/northeastTycoon/p/5511498.html 1. 打开shell脚本 例子1:输出两天 ...
- Linux时间子系统之(一):时间的基本概念
专题文档汇总目录 Notes:Linux时间基准点:Linux时间和broken-down time(struct tm):不同精度的时间表示time_t.timeval.timespec. 原文地址 ...
随机推荐
- Git使用:版本回退
在Git中,我们可以用 git log命令查看我们修改的历史记录 C:\Users\Administrator\Documents\GitHub\learngit [master]> git l ...
- Go语言测试:testing
学习参考来源:https://www.liwenzhou.com/posts/Go/16_test/ go test工具 必须导入包: import "testing" go te ...
- Hadoop 代码实现文件上传
本项目主要实现Windows下利用代码实现Hadoop中文件上传至HDFS 实现上传文本文件中单词个数的计数 1.项目结构 2.相关代码 CopyFromLocalFile 1 package com ...
- STC8H开发(七): I2C驱动MPU6050三轴加速度+三轴角速度检测模块
目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...
- 加深对AQS原理的理解示例二:自己设计一个同步工具,同一时刻最多只有两个线程能访问,超过线程将被阻塞
/** *@Desc 设计一个同步工具,同一时刻最多只有两个线程能访问,超过线程将被阻塞<br> * 思路分析: * 1.共享锁 两个线程及以内能成功获取到锁 * 2. *@Author ...
- 怎么重载网卡?ip修改 HHS服务器
目录 一:目录结构知识详述 1.网卡配置文件 2,ip修改 3.重载网卡信息 4.关闭网络管理器(因为已经有了network)所有要关闭NetworkManager不然会发生冲突 5.判断SSH服务是 ...
- 有关softmax函数代码实现的思考
有关softmax函数代码实现的思考 softmax函数 def softmax2(x): if x.ndim == 2: x = x.T x = x - np.max(x, axis=0) y = ...
- CSS之常见布局|常用单位|水平垂直居中
常见布局: 1. 流式布局:百分比布局,宽高.margin.pinding都是百分比 2. 固定布局:盒子的宽高固定,如:margin.padding等 3. 浮动布局:float 4. 弹性布局:f ...
- Sklearn 与 TensorFlow 机器学习实用指南第二版
零.前言 一.机器学习概览 二.一个完整的机器学习项目 三.分类 四.训练模型 五.支持向量机 六.决策树 七.集成学习和随机森林 八.降维 十.使用 Keras 搭建人工神经网络 十一.训练深度神经 ...
- JVM学习六:堆分析
一.内存溢出(OOM)的原因 在JVM中,有哪些内存区间? 堆溢出 public static void main(String args[]){ ArrayList<byte[]> li ...