作为 systemd 家族的一员,timedatectl 命令可以帮助我们查询以及修改系统的时间和设置。它在 systemd 的 service 是 systemd-timedated.service

查看系统当前时间、日期以及时区,和相关设置。

$ timedatectl status
Local time: Sat 2018-08-11 13:04:19 UTC
Universal time: Sat 2018-08-11 13:04:19 UTC
RTC time: Sat 2018-08-11 13:04:19
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no

也可以直接使用 timedatectl 命令,效果与加上 status 一致。

查看所有可用的时区。

$ timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
...

Ubuntu 系统支持的时区中,北京时间被命名为 "Asia/Shanghai"。

$ timedatectl list-timezones | grep -o "Asia/Sh.*"
Asia/Shanghai

设置时区为上海时区。

$ timedatectl set-timezone "Asia/Shanghai"
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-timezone ===
Authentication is required to set the system timezone.
Authenticating as: Jerry Bian (jerry)
Password:
==== AUTHENTICATION COMPLETE ===
$ timedatectl
Local time: Sat 2018-08-11 21:20:31 CST
Universal time: Sat 2018-08-11 13:20:31 UTC
RTC time: Sat 2018-08-11 13:20:31
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no

CST 的全称是:China Standard Time。

修改系统日期。

如果仔细观察上面的 status 命令的输出结果,有一行信息是 systemd-timesyncd.service active: yes,这表明当前系统会自动与网络同步最新的时间。

$ timedatectl set-time 2015-10-10
Failed to set time: Automatic time synchronization is enabled

我们可以把自动同步关掉,这样设置日期以及时间才可以得以执行。

$ timedatectl set-ntp 0
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-ntp ===
network time synchronization shall be enabled.
Authenticating as: Jerry Bian (jerry)
Password:
==== AUTHENTICATION COMPLETE ===

$ timedatectl set-time 2015-10-10
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-time ===
Authentication is required to set the system time.
Authenticating as: Jerry Bian (jerry)
Password:
==== AUTHENTICATION COMPLETE ===

$ timedatectl
Local time: Sat 2015-10-10 00:00:06 CST
Universal time: Fri 2015-10-09 16:00:06 UTC
RTC time: Sat 2018-08-11 13:31:06
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
systemd-timesyncd.service active: no
RTC in local TZ: no

修改系统时间。

$ timedatectl set-time 15:12:12
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-time ===
Authentication is required to set the system time.
Authenticating as: Jerry Bian (jerry)
Password:
==== AUTHENTICATION COMPLETE ===

$ timedatectl
Local time: Sat 2015-10-10 15:12:16 CST
Universal time: Sat 2015-10-10 07:12:16 UTC
RTC time: Sat 2018-08-11 13:32:51
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
systemd-timesyncd.service active: no
RTC in local TZ: no

同时设置日期和时间。

$ timedatectl set-time "2016-01-01 10:12:32"
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-time ===
Authentication is required to set the system time.
Authenticating as: Jerry Bian (jerry)
Password:
==== AUTHENTICATION COMPLETE ===

$ timedatectl
Local time: Fri 2016-01-01 10:12:36 CST
Universal time: Fri 2016-01-01 02:12:36 UTC
RTC time: Sat 2018-08-11 13:34:22
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
systemd-timesyncd.service active: no
RTC in local TZ: no

修改硬件时间

我们通常所见到时间,包括上文设置或修改的时间都属于软件时钟(software clocks),一般计算机中还有一个时间叫做硬件时钟(hardware clocks),它是由电池驱动并且独立于操作系统。甚至于当前并没有系统安装或运行,这个硬件时钟也可以通过 BIOS 来设置。

之所以分软件时钟和硬件时钟,是因为查看硬件的信息通常都是很复杂以及耗时的。以 Linux 系统为例,当系统启动时,内核会设置软件时钟为硬件时钟,从那以后就不用访问硬件以独立运行了。

status 输出信息中 RTC time: Sat 2018-08-11 13:34:22 便是硬件时钟。RTC 是 Real Time Clock 的缩写。RTC in local TZ: no 表明当前硬件时间不是以系统时区来显示的(以 UTC)。

我们可以改变这个设置。

$ timedatectl set-local-rtc 1
==== AUTHENTICATING FOR org.freedesktop.timedate1.set-local-rtc ===
the RTC stores the local or UTC time.
Authenticating as: Jerry Bian (jerry)
Password:
==== AUTHENTICATION COMPLETE ===

$ timedatectl
Local time: Fri 2016-01-01 10:29:08 CST
Universal time: Fri 2016-01-01 02:29:08 UTC
RTC time: Sat 2018-08-11 13:50:54
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
systemd-timesyncd.service active: no
RTC in local TZ: yes

Warning: The system is configured to read the RTC time in the local time zone.
This mode can not be fully supported. It will create various problems
with time zone changes and daylight saving time adjustments. The RTC
time is never updated, it relies on external facilities to maintain it.
If at all possible, use RTC in UTC by calling
'timedatectl set-local-rtc 0'.

当然修改硬件时间时区为本地时区是不推荐的,因为这会在夏令时间的时候没法自动调整,UTC 时区就没有夏令时的问题。

(本文完)