OpenEuler欧拉使用方法(四)设置日期和时间之二timedatectl、date、hwclock

本文详细介绍了在OpenEuler操作系统中使用timedatectl、date和hwclock三个命令进行日期和时间设置的方法,包括查看系统时间、设置时区、调整系统时间以及如何与NTP同步。
摘要由CSDN通过智能技术生成

OpenEuler欧拉使用方法(四)设置日期和时间之二timedatectl、date、hwclock

摘要

前言

这一节介绍介绍三个基础命令timedatectl、date、hwclock,之所以称为基础是因为这些命令在最小化安装时也会在系统中存在。当然,无论之前的几章还是未来的章节中,我都已最基础的命令为根基来介绍欧拉的使用。目的就是在实际生产环境中更为实用。
但是注意,这三个命令可以在普通账户下进行查询读取操作,但是不能进行写操作。
这三个命令的区别在于,timedatectl是一个较为综合命令,显示的信息较为全面,修改能力也全面;而date更专注日期的显示,带有大量的格式参数;至于hwclock就更加专注于底层硬件时间的读取和修改了。

一、使用timedatectl

1、完全展示一下这个命令。

[root@5233localhost ~]# timedatectl -h
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-timezone ZONE        设置系统时区
  list-timezones           显示时区列表
  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.
[root@5233localhost ~]#

2、timedatectl 显示配置。

timedatectl 命令在不使用任何参数时就可以就可以显示出完整的信息,这与使用 timedatectl status 命令是相同的效果。

[root@localhost ~]# timedatectl
               Local time:2023-12-11 22:03:16 CST
           Universal time:2023-12-11 14:03:16 UTC
                 RTC time:2023-12-11 14:03:17
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@localhost ~]#
[root@localhost ~]# timedatectl status
               Local time:2023-12-11 22:03:27 CST
           Universal time:2023-12-11 14:03:27 UTC
                 RTC time:2023-12-11 14:03:28
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@localhost ~]#

Local time: 本地时间(中国时间),这是UTC加时区的时间。
Universal time: UTC时间。
RTC time: 硬件时间。在主板BIOS中配置。
Time zone: 选择的时区
System clock synchronized: 系统时钟同步。
NTP service: NTP 服务器状态(这里主要指 chronyd 服务)。两个状态 active 和 inactive 分别代表 NTP同步开启和NTP同步关闭。
RTC in local TZ: 是否使用本地时间(Local time)作为RTC时间。两个状态 yes 和 no。

3、显示时区列表及配置时区。

涉及命令:list-timezones 和 set-timezone ZONE 。
因为在配置时区的时候首先要知道有哪些时区可以用来配置,所以才先有 list-timezones 命令来显示时区列表。然后再通过 set-timezone 来配置时区。
实例如下:

[root@localhost ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
……
Asia/Saigon
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk

lines 1-77

[root@localhost ~]# timedatectl set-timezone Asia/Shanghai
[root@localhost ~]#

4、配置系统时间

在使用 timedatectl 配置时间的时候必须关闭 NTP 自动时间同步。关闭命令:timedatectl set-ntp BOOL
然后再使用 set-time 来设置时间。
实例如下:

(1)、配置日期
// 关闭 NTP 自动同步
[root@localhost ~]# timedatectl set-ntp no

// 设置系统日期
[root@localhost ~]# timedatectl set-time 2024-01-16
[root@localhost ~]# timedatectl
               Local time: Tue 2024-01-16 00:00:06 CST
           Universal time: Mon 2024-01-15 16:00:06 UTC
                 RTC time: Mon 2024-01-15 16:00:06
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no
[root@localhost ~]#
(2)、配置时间
[root@localhost ~]# timedatectl set-time 14:15:00
[root@localhost ~]# timedatectl
               Local time: Tue 2024-01-16 14:15:05 CST
           Universal time: Tue 2024-01-16 06:15:05 UTC
                 RTC time: Tue 2024-01-16 06:15:05
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no
[root@localhost ~]#

(3)、配置日期和时间

注意:同时配置日期和时间需要使用 “” 号把需要配置的日期和时间包括起来。
出错的配置:

[root@localhost ~]# timedatectl set-time 2024-01-25 1:35:15
Too many arguments.

正确的配置:

[root@localhost ~]# timedatectl set-time "2024-01-25 1:35:15"
[root@localhost ~]# timedatectl
               Local time: Thu 2024-01-25 01:35:23 CST
           Universal time: Wed 2024-01-24 17:35:23 UTC
                 RTC time: Wed 2024-01-24 17:35:24
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no
[root@localhost ~]#

二、使用 date

这个命令以给定<格式>字符串的形式显示当前时间,或者设置系统日期。我在这里就不全面的解析这个命令了。想仔细研究这个命令的可以使用帮助来获取具体的使用方法,因为这个命令是有中文帮助说明书的。

1、使用 date --help 来获取中文帮助

[root@localhost ~]# date --help
用法:date [选项]... [+格式]
 或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
以给定<格式>字符串的形式显示当前时间,或者设置系统日期。

必选参数对长短选项同时适用。
  -d, --date=字符串          显示给定<字符串>描述的时间,而非“当前时间”
      --debug                对解析的日期添加注释,
                              对不规范的使用方式进行警告,并输出警告信息到
                              标准错误
  -f, --file=日期文件        类似 --date;使用给定<日期文件>,一次处理一行
  -I[FMT], --iso-8601[=FMT]  以 ISO 8601 格式输出日期/时间。
                               FMT='date' 时只输出日期(默认行为),
                               当其为 'hours''minutes''seconds''ns' 时
                               则按照所指示的精确度显示日期和时间。
                               示例:2006-08-14T02:34:56-06:00
  -R, --rfc-email            以 RFC 5322 格式输出日期和时间。
                               例如:Mon, 14 Aug 2006 02:34:56 -0600
      --rfc-3339=格式        以 RFC 3339 格式输出日期/时间。
                               <格式>字符串可以是'date''seconds''ns',
                               用于指定日期和时间显示的精确度。
                               例如:2006-08-14 02:34:56-06:00
  -r, --reference=文件       显示指定<文件>的最后修改时间
  -s, --set=字符串           按照给定<字符串>描述的时间来设置时间
  -u, --utc, --universal     按照协调世界时(UTC)显示或设置时间
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出

2、用自定义格式显示时间

注意这个语法:date [选项]… [+格式] ;在格式前面的 + 号非常关键,也容易在使用的时候被遗忘。

// 错误使用,缺少 + 号的错误提示。
[root@localhost ~]# date "%d-%Y-%m %H:%M:%S"
date: 无效的日期 “%d-%Y-%m %H:%M:%S”
[root@localhost ~]#

// 正确使用方法:
[root@localhost ~]# date "+%d-%Y-%m %H:%M:%S"
06-2024-01 22:11:11
[root@localhost ~]#
(1)、显示日期
// 按月计的日期;等价于%m/%d/%y
[root@localhost ~]# date +%D
01/06/24
// 完整日期格式,等价于 %+4Y-%m-%d
[root@localhost ~]# date +%F
2024-01-06
// 年份最后两位数位 (00-99)
[root@localhost ~]# date +%y
24
// 完整的年份
[root@localhost ~]# date +%Y
2024
// 月份
[root@localhost ~]# date +%m
01
// 按月计的日(例如:01)
[root@localhost ~]# date +%d
06
[root@localhost ~]#
(2)、显示星期
[root@localhost ~]# date +%a
六
[root@localhost ~]# date +%A
星期六
// 一星期中的第几日(0-6),0 代表周一
[root@localhost ~]# date +%w
6
// 一年中的第几周,以周一为每星期第一天(00-53)
[root@localhost ~]# date +%W
01
[root@localhost ~]#
(3)、显示系统时区
[root@localhost ~]# date +%z
+0800
[root@localhost ~]# date +%Z
CST
[root@localhost ~]#

3、设置时间

[root@localhost ~]# date -s '2024-02-11 13:15:03'
20240211日 星期日 13:15:03 CST
[root@localhost ~]# date
20240211日 星期日 13:15:07 CST
[root@localhost ~]#

也可以使用 date 分别设置时间和日期。但是在设置日期时要注意这里面有坑。

(1)、修改时间

date --set HH:MM:SS 分别是 HH小时;MM分钟;SS秒。

[root@localhost ~]# date --set 21:22:40
20240109日 星期二 21:22:40 CST
[root@localhost ~]#
(2)、修改日期

date --set YYYY-MM-DD 参数分别是 YYYY年;MM代表月;DD代表日。
注意:在单独设置日期时,时间默认会重置为 00:00:00

// 查看设置之前的日期,时间还是正常的。
[root@localhost ~]# date
20240109日 星期二 21:26:01 CST
// 注意:设置日期之后,时间被重置为 00:00:00
[root@localhost ~]# date --set 2024-03-15
20240315日 星期五 00:00:00 CST

三、使用 hwclock

此命令主要用途是配置硬件的时间,特指主板BIOS时间。

1、使用 hwclock -h 来获取中文帮助

[root@localhost ~]# hwclock -h

用法:
 hwclock [function] [option...]

Time clocks utility.

功能:
 -r, --show           显示 RTC 时间
     --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 <时间戳>  指定用于 --set 和 --predict 的日期/时间输入
     --delay <sec>    delay used when set new RTC time
     --update-drift   update the RTC drift factor
     --noadjfile      不使用 /etc/adjtime
     --adjfile <file> use an alternate file to /etc/adjtime
     --test           测试运行;隐含启用 --verbose
 -v, --verbose        显示更多细节

 -h, --help           显示此帮助
 -V, --version        显示版本

更多信息请参阅 hwclock(8)[root@localhost ~]#

2、设置时间

// 设置时间
[root@localhost ~]# hwclock --set --date '2024-03-15 16:26:33'
// 查看设置的时间
[root@localhost ~]# hwclock -r
2024-03-15 16:26:49.338651+08:00
// 使用 timedatectl 查看系统中时间的状态
[root@localhost ~]# timedatectl
               Local time:2024-02-11 14:15:07 CST
           Universal time:2024-02-11 06:15:07 UTC
                 RTC time:2024-03-15 08:29:06
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no
[root@localhost ~]#

四、总结(并使用NTP恢复被我搞乱的时间)

以上关于时间的配置就学习到这,最后使用命令恢复被我搞乱的时间。所以,使用NTP来自动同步时间对于服务器的时间稳定至关重要,建议有条件的机房都配置专业的NTP服务器。

// 查看当前被我弄乱的时间,无论是本地时间还是硬件时间都已经不正确。
[root@localhost ~]# timedatectl
               Local time:2024-02-11 14:15:07 CST
           Universal time:2024-02-11 06:15:07 UTC
                 RTC time:2024-03-15 08:29:06
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

// 启用 ntp 配置,系统会自动同步本地时间,但是注意(RTC time)硬件时间并没有恢复,因为配置没有自动同步硬件时间。
[root@localhost ~]# timedatectl set-ntp yes
[root@localhost ~]# timedatectl
               Local time:2024-01-07 00:09:48 CST
           Universal time:2024-01-06 16:09:48 UTC
                 RTC time:2024-03-15 08:31:22
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

// 为了恢复硬件时间,修改配置使硬件时间也可以自动同步。
[root@localhost ~]# timedatectl set-local-rtc yes

// 重新查看时间,硬件时间已经恢复。
[root@localhost ~]# timedatectl
               Local time:2024-01-07 00:11:37 CST
           Universal time:2024-01-06 16:11:37 UTC
                 RTC time:2024-01-07 00:11:37
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              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'.
[root@localhost ~]#

  • 25
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m976382

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值