麒麟桌面系统时间相关命令介绍
麒麟桌面系统时间相关命令介绍
·
麒麟桌面系统时间相关命令介绍
1. date 命令
date 命令可以用来显示或设定系统的日期与时间。
- 显示当前计算机系统上的日期和时间
kylin@kylin-vm:~$ date
2022年 07月 28日 星期四 12:23:39 CST
- 设置当前计算机系统上的日期和时间
kylin@kylin-vm:~$ sudo date -s "20220729 13:00:00"
[sudo] kylin 的密码:
2022年 07月 29日 星期五 13:00:00 CST
2. hwclock 命令
在 Linux 中有硬件时钟与系统时钟两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在 BIOS 中设定的时钟。系统时钟则是指 kernel 中的时钟。当 Linux 启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。
为了保持硬件时钟与系统时钟的一致性, Linux 每隔一段时间会将系统时间写入硬件时钟。由于该同步是每隔一段时间(大约是11分钟)进行的,所以在我们执行 date 命令修改系统时间之后,如果马上重起机器,修改的时间就有可能没有被写入硬件时钟 。这时就需要我们使用 hwclock 命令即时将系统时间写入硬件时钟 。
- hwclock 命令语法
kylin@kylin-vm:~$ hwclock --help
用法:
hwclock [function] [option...]
Time clocks utility.
功能:
-r, --show display the RTC time
--get display drift corrected RTC time
--set set the RTC according to --date
-s, --hctosys set the system time from the RTC
-w, --systohc set the RTC from the system time
--systz send timescale configurations to the kernel
-a, --adjust adjust the RTC to account for systematic drift
--predict predict the drifted RTC time according to --date
选项:
-u, --utc the RTC timescale is UTC
-l, --localtime the RTC timescale is Local
-f, --rtc <file> use an alternate file to /dev/rtc0
--directisa use the ISA bus instead of /dev/rtc0 access
--date <time> date/time input for --set and --predict
--delay <sec> delay used when set new RTC time
--update-drift update the RTC drift factor
--noadjfile do not use /etc/adjtime
--adjfile <file> use an alternate file to /etc/adjtime
--test dry run; implies --verbose
-v, --verbose display more details
-h, --help display this help
-V, --version display version
更多信息请参阅 hwclock(8)。
下面对其中常用的几个参数进行解释:
- 显示当前硬件日期和时间
kylin@kylin-vm:~$ sudo hwclock -r
[sudo] kylin 的密码:
2022-07-28 12:44:18.586694+08:00
- 将当前系统时间写入硬件时钟
kylin@kylin-vm:~$ sudo hwclock -w
#再次查看硬件日期和时间,显示已经成功写入
kylin@kylin-vm:~$ sudo hwclock -r
2022-07-29 13:19:07.334861+08:00
3. timedatectl 命令
timedatectl 命令用于在 Linux 中查询或者设置系统时间、日期、时区等配置。
- timedatectl 命令语法
kylin@kylin-vm:~$ timedatectl --help
timedatectl [OPTIONS...] COMMAND ...
Query or change system time and date settings.
Commands:
status Show current time settings
show Show properties of systemd-timedated
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Enable or disable network time synchronization
systemd-timesyncd Commands:
timesync-status Show status of systemd-timesyncd
show-timesync Show properties of systemd-timesyncd
Options:
-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 Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
--monitor Monitor status of systemd-timesyncd
-p --property=NAME Show only properties by this name
-a --all Show all properties, including empty ones
--value When showing properties, only print the value
See the timedatectl(1) man page for details.
下面对其中几个常用的参数进行解释:
- 查看当前时间/日期/时区
#timedatectl或者timedatectl status
kylin@kylin-vm:~$ timedatectl status
Local time: 五 2022-07-29 13:43:11 CST
Universal time: 五 2022-07-29 05:43:11 UTC
RTC time: 五 2022-07-29 13:43:11
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: active
RTC in local TZ: yes
Warning: The system is configured to read the RTC time in the local time zone.
This mode cannot 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'.
下面对输出结果中的内容进行解释:
* GMT:Greenwich Mean Time,即格林尼治(本初子午线上)标准时间,也就是世界时。GMT 以地球自转为基础的时间计量系统,但由于地球自转不均匀,导致 GMT 不精确,现在已经不再作为世界标准时间使用。
* UTC:Universal Time Coordinated,即协调世界时,又称世界标准时间。UTC 是以原子时秒长为基础,在时刻上尽量接近于 GMT 的一种时间计量系统。为确保 UTC 与 GMT 相差不会超过 0.9 秒,在有需要的情况下会在 UTC 内加上正或负闰秒。UTC 现在作为世界标准时间使用。
* CST:China Standard Time,即中国标准时间。因为北京位于东八区,所以北京时间为 UTC + 8。
* Local time 当地时间,此处便是 CST 时间。
* RTC:Real-Time Clock,即实时时钟,在计算机领域作为硬件时钟的简称。
注:此处可以看到麒麟桌面系统是将 RTC (硬件时间) 直接读取为 Local time (当地时间)。其实 windows 系统也是这样做的,但是部分 Linux 系统(如 Ubuntu)则是将 RTC (硬件时间) 读取为 UTC (世界标准时间),然后再在 UTC 的基础上加上8小时,得到 Local time (当地时间)。这也是为什么 win + ubuntu 双系统切换时系统时间会出现8个小时的差距。
- 查看所有可用时区
kylin@kylin-vm:~$ timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Algiers
Africa/Bissau
Africa/Cairo
Africa/Casablanca
Africa/Ceuta
Africa/El_Aaiun
Africa/Johannesburg
Africa/Juba
Africa/Khartoum
Africa/Lagos
Africa/Maputo
Africa/Monrovia
Africa/Nairobi
Africa/Ndjamena
Africa/Sao_Tome
Africa/Tripoli
Africa/Tunis
Africa/Windhoek
America/Adak
America/Anchorage
America/Araguaina
America/Argentina/Buenos_Aires
America/Argentina/Catamarca
America/Argentina/Cordoba
America/Argentina/Jujuy
America/Argentina/La_Rioja
America/Argentina/Mendoza
America/Argentina/Rio_Gallegos
America/Argentina/Salta
America/Argentina/San_Juan
America/Argentina/San_Luis
America/Argentina/Tucuman
America/Argentina/Ushuaia
America/Asuncion
America/Atikokan
America/Bahia
America/Bahia_Banderas
America/Barbados
.....
- 启用、禁用 NTP 自动时间同步
kylin@kylin-vm:~$ timedatectl set-ntp true
kylin@kylin-vm:~$
kylin@kylin-vm:~$ timedatectl set-ntp false
kylin@kylin-vm:~$
麒麟桌面系统默认使用 systemd-timesyncd 服务同步系统时间,默认使用阿里云的时钟同步服务器。我们也可以根据自己的需要,编辑配置文件 /etc/systemd/timesyncd.conf 来添加自己的时钟同步服务器,修改完成之后重 systemd-timesyncd 服务即可。
kylin@kylin-vm:~$ cat /etc/systemd/timesyncd.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.
[Time]
#NTP=
#FallbackNTP=ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com ntp4.aliyun.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
kylin@kylin-vm:~$ sudo systemctl restart systemd-timesyncd.service
kylin@kylin-vm:~$
- 设置时间、日期、时区
#时间格式:HH:MM:SS
kylin@kylin-vm:~$ timedatectl set-time "18:00:00"
kylin@kylin-vm:~$
#日期格式:YYYY-MM-DD
kylin@kylin-vm:~$ timedatectl set-time "2022-08-01"
kylin@kylin-vm:~$
kylin@kylin-vm:~$ timedatectl set-time "2022-08-02 19:00:00"
kylin@kylin-vm:~$
kylin@kylin-vm:~$ timedatectl
Local time: 二 2022-08-02 19:00:03 CST
Universal time: 二 2022-08-02 11:00:03 UTC
RTC time: 二 2022-08-02 19:00:04
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: yes
Warning: The system is configured to read the RTC time in the local time zone.
This mode cannot 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'.
kylin@kylin-vm:~$ timedatectl set-timezone UTC
kylin@kylin-vm:~$
kylin@kylin-vm:~$ timedatectl
Local time: 二 2022-08-02 11:00:57 UTC
Universal time: 二 2022-08-02 11:00:57 UTC
RTC time: 二 2022-08-02 11:00:57
Time zone: UTC (UTC, +0000)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: yes
Warning: The system is configured to read the RTC time in the local time zone.
This mode cannot 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'.
- 设置硬件时钟(RTC)为本地时间
kylin@kylin-vm:~$ timedatectl set-local-rtc 1
注:对于 win + ubuntu 双系统时间不一致的问题,使用这条命令,将硬件时钟设置为本地即可解决。
- 设置硬件时钟(RTC)为协调世界时间(UTC)
kylin@kylin-vm:~$ timedatectl set-local-rtc 0
更多推荐
已为社区贡献2条内容
所有评论(0)