admin管理员组

文章数量:1794759

Linux基础:timedatectl命令使用介绍

Linux基础:timedatectl命令使用介绍

timedatectl是Linux下的一条命令,用于控制系统时间和日期。可以用来查询和更改系统时钟于设定,同时可以设定和修改时区信。这篇文章简单介绍一下如何使用timedatectl进行常见操作。

基础知识

关于RTC/系统时钟/NTP/时区等基本信和设定的介绍,可参看如下文章,本文不再赘述。

  • blog.csdn/liumiaocn/article/details/79250793
常见用法

使用timedatectl可以进行如下常见操作

  • 查看当前时间/日期/时区:timedatectl或者timedatectl status
  • 查看所有可用时区:timedatectl list-timezones
  • 设置时区:timedatectl set-timezone “时区信”
  • 设置UTC:timedatectl set-timezone UTC
  • 设置时间:timedatectl set-time HH:MM:SS
  • 设置日期:timedatectl set-time YYYY-MM-DD
  • 设置日期时间:timedatectl set-time “YYYY-MM-DD HH:MM:SS”
  • 设置硬件时钟为本地时间:timedatectl set-local-rtc 1
  • 设置硬件时钟为UTC时间:timedatectl set-local-rtc 0
  • 启动NTP时间同步(启用NTP服务或者Chrony服务):timedatectl set-ntp true
  • 禁用NTP时间同步:timedatectl set-ntp false
使用示例 查看当前时间/日期/时区

命令:timedatectl或者timedatectl status

  • 执行示例
[root@platform ~]# timedatectl status Local time: Fri 2019-03-08 06:07:19 EST Universal time: Fri 2019-03-08 11:07:19 UTC RTC time: Fri 2019-03-08 09:41:48 Time zone: America/New_York (EST, -0500) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: no Last DST change: DST ended at Sun 2018-11-04 01:59:59 EDT Sun 2018-11-04 01:00:00 EST Next DST change: DST begins (the clock jumps one hour forward) at Sun 2019-03-10 01:59:59 EST Sun 2019-03-10 03:00:00 EDT [root@platform ~]# 查看所有可用时区

命令:timedatectl list-timezones

  • 执行示例
[root@platform ~]# timedatectl list-timezones Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmara Africa/Bamako Africa/Bangui Africa/Banjul Africa/Bissau Africa/Blantyre Africa/Brazzaville Africa/Bujumbura Africa/Cairo Africa/Casablanca Africa/Ceuta Africa/Conakry Africa/Dakar Africa/Dar_es_Salaam Africa/Djibouti Africa/Douala Africa/El_Aaiun Africa/Freetown Africa/Gaborone Africa/Harare Africa/Johannesburg Africa/Juba Africa/Kampala Africa/Khartoum Africa/Kigali lines 1-29 设置时区

命令:timedatectl set-timezone “时区信”

  • 执行示例
[root@platform ~]# timedatectl set-timezone "Asia/ShangHai" Failed to set time zone: Invalid time zone 'Asia/ShangHai' [root@platform ~]# timedatectl set-timezone "Asia/Shanghai" [root@platform ~]# timedatectl Local time: Fri 2019-03-08 19:13:38 CST Universal time: Fri 2019-03-08 11:13:38 UTC RTC time: Fri 2019-03-08 09:47:38 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a [root@platform ~]#

注意可以看到如果时区信不正确的话会提示Invalid time zone的信

设置UTC

命令:timedatectl set-timezone UTC

  • 执行示例
[root@platform ~]# timedatectl Local time: Fri 2019-03-08 19:14:48 CST Universal time: Fri 2019-03-08 11:14:48 UTC RTC time: Fri 2019-03-08 09:48:42 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a [root@platform ~]# timedatectl set-timezone UTC [root@platform ~]# timedatectl status Local time: Fri 2019-03-08 11:15:05 UTC Universal time: Fri 2019-03-08 11:15:05 UTC RTC time: Fri 2019-03-08 09:48:59 Time zone: UTC (UTC, +0000) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a [root@platform ~]# 设置时间

命令:timedatectl set-time HH:MM:SS

  • 执行示例
[root@platform ~]# timedatectl set-timezone "Asia/Shanghai" [root@platform ~]# date Fri Mar 8 19:15:36 CST 2019 [root@platform ~]# [root@platform ~]# timedatectl set-time "20:45:00" Failed to set time: Automatic time synchronization is enabled [root@platform ~]# date Fri Mar 8 19:16:20 CST 2019 [root@platform ~]# [root@platform ~]# timedatectl set-ntp false [root@platform ~]# timedatectl set-time "20:45:00" [root@platform ~]# date Fri Mar 8 20:45:01 CST 2019 [root@platform ~]#

注意: 如果ntp时间同步为true时无法修改时间设定

设置日期

命令:timedatectl set-time YYYY-MM-DD

  • 执行示例
[root@platform ~]# timedatectl set-ntp false [root@platform ~]# timedatectl set-time 2019-03-10 [root@platform ~]# date Sun Mar 10 00:00:01 CST 2019 [root@platform ~]#

注意: 如果ntp时间同步为true时无法修改时间设定

设置日期时间

命令:timedatectl set-time “YYYY-MM-DD HH:MM:SS”

  • 执行示例
[root@platform ~]# timedatectl set-ntp false [root@platform ~]# timedatectl set-time "2019-03-11 20:45:00" [root@platform ~]# date Mon Mar 11 20:45:01 CST 2019 [root@platform ~]#

注意: 如果ntp时间同步为true时无法修改时间设定

设置硬件时钟为本地时间

命令:timedatectl set-local-rtc 1

  • 执行示例
[root@platform ~]# timedatectl Local time: Mon 2019-03-11 20:58:12 CST Universal time: Mon 2019-03-11 12:58:12 UTC RTC time: Mon 2019-03-11 12:46:14 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: n/a [root@platform ~]# timedatectl set-local-rtc 1 [root@platform ~]# timedatectl status Local time: Mon 2019-03-11 20:58:16 CST Universal time: Mon 2019-03-11 12:58:16 UTC RTC time: Mon 2019-03-11 20:58:16 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: yes RTC in local TZ: yes DST active: n/a 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'. [root@platform ~]# 设置硬件时钟为UTC时间

命令:timedatectl set-local-rtc 0

  • 执行示例
[root@platform ~]# timedatectl Local time: Mon 2019-03-11 20:59:18 CST Universal time: Mon 2019-03-11 12:59:18 UTC RTC time: Mon 2019-03-11 20:59:17 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: yes RTC in local TZ: yes DST active: n/a 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'. [root@platform ~]# [root@platform ~]# timedatectl set-local-rtc 0 [root@platform ~]# timedatectl status Local time: Mon 2019-03-11 20:59:27 CST Universal time: Mon 2019-03-11 12:59:27 UTC RTC time: Mon 2019-03-11 12:59:27 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: n/a [root@platform ~]# 启动NTP时间同步(启用NTP服务或者Chrony服务)

命令:timedatectl set-ntp true

  • 执行示例
[root@platform ~]# timedatectl status Local time: Mon 2019-03-11 21:00:19 CST Universal time: Mon 2019-03-11 13:00:19 UTC RTC time: Mon 2019-03-11 13:00:19 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: no NTP synchronized: yes RTC in local TZ: no DST active: n/a [root@platform ~]# timedatectl set-ntp true [root@platform ~]# timedatectl status Local time: Mon 2019-03-11 21:00:24 CST Universal time: Mon 2019-03-11 13:00:24 UTC RTC time: Mon 2019-03-11 13:00:24 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a [root@platform ~]# 禁用NTP时间同步

命令:timedatectl set-ntp false

  • 执行示例
[root@platform ~]# timedatectl status Local time: Mon 2019-03-11 21:00:45 CST Universal time: Mon 2019-03-11 13:00:45 UTC RTC time: Mon 2019-03-11 13:00:44 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: n/a [root@platform ~]# timedatectl set-ntp false [root@platform ~]# timedatectl status Local time: Mon 2019-03-11 21:00:50 CST Universal time: Mon 2019-03-11 13:00:50 UTC RTC time: Mon 2019-03-11 13:00:50 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: no NTP synchronized: yes RTC in local TZ: no DST active: n/a [root@platform ~]# 参考文章

blog.csdn/liumiaocn/article/details/79250793

本文标签: 命令基础Linuxtimedatectl