百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

【国产化】Linux命令速学之 timedatectl 命令

nanshan 2024-11-09 12:33 9 浏览 0 评论

#文章首发挑战赛# #你好,2024#

1. timedatectl 命令简介

timedatectl 通常出现在基于 systemd Linux 发行版,用于查看和设置 time date 设定。kylin / UOS / CentOS Linux 各版本通用。

Linux 系统时间是从1970 年1 月 1 日 00:00:00,UTC 起的秒数。Linux 系统时钟是基于软件的,一旦关机就无法运行,这时基于电池的硬件实时时钟可以在计算机关机时运行,以便告诉系统时钟 Linux 启动时的时间(在没有网络时间协议 NTPnetwork time protocol 服务器时)。

系统时钟始终以 UTC 为单位。任何需要获取本地时间的应用程序都需要:

  • Access the system clock and obtain UTC 访问系统时钟获取 UTC
  • Know what time zone it is in and apply the correct offset 知道所在的时区并应用正确的偏移量
  • Take into account whether daylight savings time is in effect 考虑夏令时是否有效

一句话,Linux 只知道 UTC,不同时区的本地时间都是软件行为,通过 time and date libraries 计算转换而来。


2. 语法

timedatectl [OPTIONS] COMMAND

timedatectl 很少用于设置日期、时间的,如果你真的用它这么去做了,通常收到的都是类似这样的错误:

[root@kylin ~]# timedatectl set-time 10:10:10
Failed to set time: Automatic time synchronization is enabled

如果确实需要这么做,则需要先停止时间同步服务:

systemctl stop systemd-timesyncd.service

而当你重新开启时间同步服务后,系统又会自动更新时间:

systemctl start systemd-timesyncd.service

通常推荐的做法是:将系统时钟设置为你所在的时区,将实时时钟设置为 UTC,并确保您的系统正在使用网络时间协议 NTP 服务,这也是大多数系统安装后的默认状态。


3. 常用命令

3.1. timedatectl

默认等价于 timedatectl status,用于查看当前 datetime 及相关的设定。

[root@kylin ~]# timedatectl
               Local time: 二 2024-01-16 22:13:00 CST
           Universal time: 二 2024-01-16 14:13:00 UTC
                 RTC time: 二 2024-01-16 14:13:00
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

CentOS 7,麒麟,统信效果基本一致:

  • Local Time: The time the computer thinks it is, according to its time zone.
  • Universal Time: The UTC time.
  • RTC Time: The time the real-time clock is using. Usually, this is UTC.
  • Time zone: Information regarding the configured time zone.
  • System Clock Synchronized: Whether the system clock is synchronized with an NTP server.
  • NTP Service: Whether the computer's NTP service is active.
  • RTC in local TZ: Whether the real-time clock is using the local time instead of UTC.


3.2. timedatectl list-timezones

查看 timedatectl 支持的时区。对比发现 UOS 支持 431 个时区,是三个系统中最多的。

[root@centos ~]# timedatectl list-timezones | wc -l
425
uroot@uos:~$ timedatectl list-timezones | wc -l
431
[root@kylin ~]# timedatectl list-timezones | wc -l
340
[root@centos ~]# timedatectl list-timezones | grep "Asia/" | less


3.3. timedatectl set-timezone "Asia/Shanghai"

  • CentOS 7 设置时区:Asia/Shanghai 改为 America/Chicago
  • CentOS 7 设置时区:America/Chicago 改回 Asia/Shanghai
  • Kylin 设置时区:Asia/Shanghai 改为 America/Edmonton
[root@kylin ~]# timedatectl
               Local time: 二 2024-01-16 22:53:22 CST
           Universal time: 二 2024-01-16 14:53:22 UTC
                 RTC time: 二 2024-01-16 14:53:23
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@kylin ~]# timedatectl set-timezone "America/Edmonton"
[root@kylin ~]# timedatectl
               Local time: 二 2024-01-16 07:53:43 MST
           Universal time: 二 2024-01-16 14:53:43 UTC
                 RTC time: 二 2024-01-16 14:53:44
                Time zone: America/Edmonton (MST, -0700)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
  • Kylin 设置时区:America/Edmonton 改回 Asia/Shanghai
[root@kylin ~]# timedatectl
               Local time: 二 2024-01-16 07:54:30 MST
           Universal time: 二 2024-01-16 14:54:30 UTC
                 RTC time: 二 2024-01-16 14:54:30
                Time zone: America/Edmonton (MST, -0700)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@kylin ~]# timedatectl set-timezone "Asia/Shanghai"
[root@kylin ~]# timedatectl
               Local time: 二 2024-01-16 22:54:49 CST
           Universal time: 二 2024-01-16 14:54:49 UTC
                 RTC time: 二 2024-01-16 14:54:49
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
  • UOS 设置时区:Asia/Shanghai 改为 America/Edmonton
  • UOS 设置时区:America/Edmonton 改回 Asia/Shanghai


3.4. timedatectl set-local-rtc 0

上面大家看到 UOS 把实时时钟 RTC (real-time clock) 设置为了本地时区 LTZ (local timezone),这是不被推荐的,可以看到很明确的警告信息。可以通过如下命令设置回来。

# 把实时时钟 RTC 设置为本地时区 LTZ
timedatectl set-local-rtc 1

# 把实时时钟 RTC 设置回 UTC
timedatectl set-local-rtc 0


4. timedatectl 详解

timedatectl 详细参数说明如下:

相关推荐

今晚拿下PHP反序列化的一系列操作

引言在CTF中反序列化类型的题目还是比较常见的,之前有学习过简单的反序列化,以及简单pop链的构造。这次学习内容为php内置的原生类的反序列化以及一点进阶知识。在题目给的的代码中找不到可利用的类时,这...

Win10安装Apache和PHP(apache安装php模块)

说明:虽然PHPStudy之类的软件给我们提供了方便的集成环境,但是其使用的是非线程安全的PHP版本(NotThreadSafe,NTS),某些功能不可以使用。所以,我们还需要自己再安装一个Apa...

腾讯云云函数部署laravel项目(腾讯云函数 selenium)

1、购买函数套餐包在Serverless控制台,选择函数套餐包进行购买2、新建函数服务2.1、模板创建选择函数URL:新建函数URL,启用公网访问:2.1.1、postman访问首页2.1.2、pos...

站长教你搭建属于自己的网站(站长教你搭建属于自己的网站是什么)

每个人都希望可以有自己的网站,因为那样高端大气上档次,低调奢华有内涵,尤其是公司用户,一般会有自己的网站。而个人呢,也可以搭建自己的网站博客,论坛等,但是一般个人都是搭建博客的。好了,那么下面站长教你...

微信公众号开发出现 cURL error 60: SSL certificate problem ssl证书

在phpstudy的环境下如果出现这样的报错cURLerror60:SSLcertificateproblem:unabletogetlocalissuercertificat...

【网络安全】关于PHP Study nginx解析高危漏洞的预警通报

网络安全近日,山石网科安全研究院监测发现PHPStudyWindows最新版本存在nginx解析漏洞,可以造成任意代码执行。一、漏洞情况phpStudy是一个PHP调试环境的程序集成包,该程序包集成...

PHP 环境 搭建教程(php环境搭建教程linux)

PHP是一种编程语言,很多网站都用PHP语言编写,我们有时候需要测试一个网站,就需要PHP环境才能运行,又要安装Apache、又要安装MySQL……真的非常麻烦。其实我们可以使用PHP集成...

黑客搭建钓鱼平台,手把手教你如何钓鱼?

跨站脚本攻击XSS:通过HTML注入篡改了网页,插入了恶意的脚本,从而用户浏览网页时,控制用户浏览器的一种攻击那么,我们搭建一个XSS钓鱼平台吧,注意:这个平台仅用于学习和测试,小伙伴们不要动有坏心思...

php源码网站搭建方法和过程(php网站源码完整)

web网站是我们上网的窗口,而网站是如何搭建的呢?今天我们来做一个介绍,以php代码为例来进行介绍(后续会介绍一下java代码搭建,如果想要我这里涉及的工具或源码请私信我)。1、首先你需要去网上下载你...

使用VS Code调试PhpStudy环境里的代码

最近几个月把所有项目都迁过来VSCode了(除了因为Unity调试问题反而用回了VisualStudio),PHP也就抛弃了最强的PhpStorm。这段时间抽空在帮朋友处理PHP项目,然...

phpstudy搭建PHP+Mysql服务(用phpstudy搭建服务器)

PHP是一种创建动态交互性站点的强有力的服务器端脚本语言。PHP是免费的,并且使用非常广泛。同时,对于像微软ASP这样的竞争者来说,PHP无疑是另一种高效率的选项。(1)PHP环境搭建使用V...

Windows安装phpstudy(Windows安装mysql)

说明:phpstudy是一个PHP+MySQL+Apache的集成环境,可以减少单独部署各个所需软件的麻烦,以及更加方便地切换版本。phpenv、wamp等软件的作用一样。由于环境的不同,安装过程中可...

phpstudy安装及简单使用教程(phpstudy安装教程详解)

phpstudy前不久爆出有后门,我的看法是,去看下是哪个版本有后门,为啥会有后门,怎么解决掉这个后门,而不是听到后门就弃用了。毕竟phpstudy绿色安装,配置简单,多版本融合,真香。前言:关于开发...

如何对dedeCMS的开源程序进行二次开发

二次开发,简单的说就是在现有的软件上进行定制修改,功能的扩展,然后达到自己想要的功能和效果,一般来说都不会改变原有系统的内核。为了让更多人了解二次开发,并更方便的了解DedeCMS的二次开发,下面将会...

mysql基础问题三问(底层逻辑;正在执行;日志观察)

背景:经常面试会遇到且实际工作中也会应用到的三个场景:目录:一.mysql查询时的底层原理是什么?二.如何查看正在执行的mysql语句?三.如何观察mysql运行过程中的日志信息?-----...

取消回复欢迎 发表评论: