#文章首发挑战赛# #你好,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 详细参数说明如下: