网站首页 > 技术教程 正文
#文章首发挑战赛# #你好,2024#
1. timedatectl 命令简介
timedatectl 通常出现在基于 systemd 的 Linux 发行版,用于查看和设置 time 和 date 设定。kylin / UOS / CentOS 等 Linux 各版本通用。
Linux 系统时间是从1970 年1 月 1 日 00:00:00,UTC 起的秒数。Linux 系统时钟是基于软件的,一旦关机就无法运行,这时基于电池的硬件实时时钟可以在计算机关机时运行,以便告诉系统时钟 Linux 启动时的时间(在没有网络时间协议 NTP,network 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,用于查看当前 date 和 time 及相关的设定。
[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 详细参数说明如下:
猜你喜欢
- 2025-05-08 Linux高性能服务器设计(linux高性能服务器编程:使用muduo c++网络库)
- 2025-05-08 CentOS系统时间 ( utc)与现在时间(cst)相差8小时解决方法
- 2025-05-08 linux实例之设置时区的方式有哪些
- 2025-05-08 CentOS7下使用Lsyncd实现文件实时同步
- 2025-05-08 Redis数据持久化机制、主从同步原理、常见规范与优化详解
- 2025-05-08 linux服务器开发之网关服务器的实现
- 2025-05-08 耗时很长的程序忘加nohup就运行了怎么办?
- 2025-05-08 手绘10张图,细谈Redis 持久化,详解RDB和AOF及混合机制
- 2025-05-08 开源的Linux文件同步工具有哪些?(linux 文件同步方案)
- 2025-05-08 如何使用Rsync同步两个网站服务器的内容
你 发表评论:
欢迎- 最近发表
-
- linux日志文件的管理、备份及日志服务器的搭建
- Linux下挂载windows的共享目录操作方法
- Linux系统中的备份文件命令(linux系统中的备份文件命令有哪些)
- 麒麟KYLINOS|通过不同方法设置用户访问文件及目录权限
- 「Linux笔记」系统目录结构(linux目录的结构及含义)
- linux中修改归属权chown命令和chgrp命令
- 工作日报 2021.10.27 Android-SEAndroid权限问题指南
- Windows和Linux环境下,修改Ollama的模型默认保存路径
- 如何强制用户在 Linux 上下次登录时更改密码?
- 如何删除Linux文件夹中除某些扩展名之外的所有文件?
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)