• Linux timedatectl控制系统时间和日期


    timedatectl命令对于RHEL / CentOS 7和基于Fedora 21+的分布式系统来说,是一个新工具,它作为systemd系统和服务管理器的一部分,代替旧的传统的用在基于Linux分布式系统的sysvinit守护进程的date命令。
    timedatectl命令可以查询和更改系统时钟和设置,你可以使用此命令来设置或更改当前的日期,时间和时区,或实现与远程NTP服务器的自动系统时钟同步。

    Man手册

    [root@emretest-deployment ~]# timedatectl -h
      -h --help                Show this help message
         --version             Show package version
         --no-pager            Do not pipe output into a pager
         --no-ask-password     Do not prompt for password
      -H --host=[USER@]HOST    在远程主机上操作
      -M --machine=CONTAINER   操作本地容器
         --adjust-system-clock 修改本地RTC模式时,需要调整系统时钟
    Commands:
      status                   显示系统时钟和 RTC 的当前设置,包括网络时间同步是否处于活动状态。如果未指定命令,则这是隐含的默认值
      set-time TIME            将系统时钟设置为指定时间。这也将相应地更新RTC时间。可以用格式“2012-10-30 18:17:16”来指定时间
      set-timezone ZONE        将系统时区设置为指定值。可用时区可以与list-timezones一起列出。如果RTC配置为本地时间,这也将更新RTC时间。这个调用将改变/etc/localtime符号链接
      list-timezones           列出可用的时区,每行一个。可以使用set-timezone将列表中的条目设置为系统时区
      set-local-rtc BOOL       接受一个布尔参数。如果为“0”,系统被配置为以世界时维护RTC。如果为“1”,它将改为以本地时间维护RTC
      set-ntp BOOL             接受一个布尔参数。控制网络时间同步是否激活和启用(如果可用)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    ☑ 设定时间

    # 命令													作用
    timedatectl set-time “2020-04-29 11:30:00”		设定系统时间
    timedatectl list-timezones						显示系统的所有时区
    timedatectl set-timezone “Asia/Shanghai”		设定系统时区
    timedatectl set-local-rtc 0/1					设定系统时间计算方式,0表示使用utc时间计算方式
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ☑ 举例

    [root@master ~]# timedatectl status
          Local time: Mon 2022-09-19 08:50:16 CST	    本地时间,初始值来自于RTC,由内核维护系统启动之后和RTC就没有关系,通常等于RTC+时区值
      Universal time: Mon 2022-09-19 00:50:16 UTC		伦敦时间(系统时间永远是UTC,在应用程序使用的时候转换为本地时间)
            RTC time: Mon 2022-09-19 00:52:09			硬件时间
    指硬件时间,一般是主板上的特殊电路,专用于记录时间,有电池供电,不受服务器和操作系统的开启关闭影响。也称作BIOS时间。
    需要注意的是:当/etc/sysconfig/clock 文件中 UTC=true时, BIOS 使用UTC时间; UTC=false时, BIOS 使用本地时间。
    hwclock --localtime		#显示 BIOS 中实际的时间				  hwclock -w 				#将系统时间同步到硬件时间
           Time zone: Asia/Shanghai (CST, +0800)		时区:亚洲上海(东8区)UTC时间加上8等于当前时间
         NTP enabled: no								状态为“yes”,表示开启NTP同步。
    网络时间协议,英文名称Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正。
    timedatectl set-ntp true		#开启ntp同步                 timedatectl set-ntp false		   #禁止ntp同步
    NTP synchronized: no								为“yes”,表示NTP同步完成。
     RTC in local TZ: no								设置RTC时间,为"no"表示未设置
     	timedatectl set-local-rtc 1		#将RTC设置为本地时间       timedatectl set-local-rtc 0		#将RTC设置为UTC
          DST active: n/a
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Exit status
    如果成功,则返回0,否则返回非零失败代码。
    启用网络时间同步:

    $ timedatectl set-ntp true
    ==== AUTHENTICATING FOR org.freedesktop.timedate1.set-ntp ===
    Authentication is required to control whether network time synchronization shall be enabled.
    Authenticating as: user
    Password: ********
    ==== AUTHENTICATION COMPLETE ===
    
    $ systemctl status systemd-timesyncd.service
    ● systemd-timesyncd.service - Network Time Synchronization
       Loaded: loaded (/usr/lib/systemd/system/systemd-timesyncd.service; enabled)
       Active: active (running) since Mo 2015-03-30 14:20:38 CEST; 5s ago
         Docs: man:systemd-timesyncd.service(8)
     Main PID: 595 (systemd-timesyn)
       Status: "Using Time Server 216.239.38.15:123 (time4.google.com)."
       CGroup: /system.slice/systemd-timesyncd.service
               └─595 /usr/lib/systemd/systemd-timesyncd
    …
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    显示systemd-timesyncd.service的当前状态

    $ timedatectl timesync-status
           Server: 216.239.38.15 (time4.google.com)
    Poll interval: 1min 4s (min: 32s; max 34min 8s)
             Leap: normal
          Version: 4
          Stratum: 1
        Reference: GPS
        Precision: 1us (-20)
    Root distance: 335us (max: 5s)
           Offset: +316us
            Delay: 349us
           Jitter: 0
     Packet count: 1
        Frequency: -8.802ppm
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ☑ 扩展
    systemd-timesyncd.service, systemd-timesyncd — 网络时间同步
    • 提要
    systemd-timesyncd.service
    /usr/lib/systemd/systemd-timesyncd

    • Description
    systemd-timesyncd是一种系统服务,可用于将本地系统时钟与远程网络时间协议(NTP)服务器同步。它还在每次时钟同步时将本地时间保存到磁盘,并在随后的重新启动中使用它来提前系统实时时钟,以确保它(大致)单调前进,即使系统缺少电池缓冲的RTC芯片。

    systemd-timesyncd服务只实现SNTP。这种极简的服务将为较大的偏移量调整系统时钟,或为较小的增量缓慢调整系统时钟。systemd-timesyncd不包括需要完全NTP支持的复杂用例(在这些用例中SNTP不够用)。

    systemd-timesyncd初始化会延迟在time-set.target之后订购的单元的开始时间,直到从/var/lib/systemd/time sync/clock更新了本地时间,以便使其大致保持单调。它不会延迟其他单元,直到达到与精确参考时间源的同步。使用systemd-time-wait-sync . service来实现这一点,这将延迟在time-sync.target之后订购的装置的启动,直到达到与精确参考时钟的同步。

    • Files
    /var/lib/systemd/timesync/clock
    根据timesyncd.conf中的规定,此文件的修改时间(“mtime”)会在每次成功的NTP同步时更新,或者在每次SaveIntervalSec=时间间隔后更新。至少它将被设置为systemd构建日期。它用于确保在没有本地RTC可用的情况下,系统时钟在重启后大致保持单调。
    /run/systemd/timesync/synchronized
    一种文件,在每次成功同步时被访问,以帮助系统时间等待同步和其它应用程序检测与精确参考时钟的同步。

    I will succeed because I intend to succeed.

  • 相关阅读:
    java毕业设计房产交易系统Mybatis+系统+数据库+调试部署
    运行java命令出现 Error: Invalid or corrupt jarfile XXX.jar
    聚类算法:kmeans和dbscan
    【C++】友元函数 ( 友元函数简介 | 友元函数声明 | 友元函数语法 | 友元函数声明不受访问控制限制 | 友元函数参数要求 )
    UI设计师的主要工作内容优漫动游
    前端做自动化测试 —— 用TDD的思想做组件测试
    Cesium屏幕中心坐标和相机位置坐标计算
    戴尔笔记本重装系统按f几进入
    win11电脑怎么设置定时关机
    错误处理函数 / 模板上下文处理函数
  • 原文地址:https://blog.csdn.net/qq_50573146/article/details/126927425