技术分享 LINUX 查看内容

timedatectl命令使用介绍

老高 | 发布于 2023-08-07 16:41| 浏览()| 评论() | 收藏() | 点赞() | 打印

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

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

    常见用法

    使用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: yesNTP 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 ~]# 12345678910111213141516

    查看所有可用时区

    命令:timedatectl list-timezones

    • 执行示例

    [root@platform ~]# timedatectl list-timezonesAfrica/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-2912345678910111213141516171819202122232425262728293031

    设置时区

    命令: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: yesNTP synchronized: no
     RTC in local TZ: no
          DST active: n/a[root@platform ~]# 12345678910111213

    注意可以看到如果时区信息不正确的话会提示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: yesNTP 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: yesNTP synchronized: no
     RTC in local TZ: no
          DST active: n/a[root@platform ~]#1234567891011121314151617181920

    设置时间

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

    • 执行示例

    [root@platform ~]# timedatectl set-timezone "Asia/Shanghai"[root@platform ~]# dateFri 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 ~]# dateFri 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 ~]# dateFri Mar  8 20:45:01 CST 2019[root@platform ~]#1234567891011121314

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

    设置日期

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

    • 执行示例

    [root@platform ~]# timedatectl set-ntp false[root@platform ~]# timedatectl set-time 2019-03-10[root@platform ~]# dateSun Mar 10 00:00:01 CST 2019[root@platform ~]#12345

    注意: 如果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 ~]# dateMon Mar 11 20:45:01 CST 2019[root@platform ~]# 12345

    注意: 如果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: yesNTP 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: yesNTP 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 ~]#123456789101112131415161718192021222324252627

    设置硬件时钟为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: yesNTP 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: yesNTP synchronized: yes
     RTC in local TZ: no
          DST active: n/a[root@platform ~]#12345678910111213141516171819202122232425262728

    启动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: yesNTP synchronized: no
     RTC in local TZ: no
          DST active: n/a[root@platform ~]#1234567891011121314151617181920

    禁用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: yesNTP 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 ~]#1234567891011121314151617181920

    发表评论(对文章涉及的知识点还有疑问,可以在这里留言,老高看到后会及时回复的。)

    表情