Linux日期时间查看和设置相关命令

原文地址: https://www.jianshu.com/p/7a770f541f91

1. 相关命令

1. date: print or set the system date and time
2. hwclock: query or set the hardware clock (RTC)
3. timedatectl: Control the system time and date

2. 查看

2.1 date命令

# date
Thu Jul 13 16:49:54 CST 2017

2.2 hwclock命令

# hwclock -r
Thu 13 Jul 2017 04:50:49 PM CST  -0.861128 seconds

需要权限

2.3 timedatectl命令

$ timedatectl
      Local time: Thu 2017-07-13 17:29:18 CST
  Universal time: Thu 2017-07-13 09:29:18 UTC
        RTC time: Thu 2017-07-13 09:29:09
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

3. 时间设置

3.1 date命令

语法

date --set='STRING'
date -s 'STRING'

示例:

# date -s '2017-07-13 15:11:00'
# date --set='2017-07-13 15:11:00'

如果参数只填写日期则时间默认为0:

# date -s '20170811'
Fri Aug 11 00:00:00 UTC 2017

可以单独设置时间:

# date -s '14:12:32'
Fri Aug 11 14:12:32 UTC 2017

3.2 hwclock命令

将系统时间写入到hardware,这样以后重启后设置的时间仍会生效:

# hwclock -w

3.3 timedatectl命令

# timedatectl set-time 'String'

示例:

# timedatectl set-time '2015-11-23 08:10:40'
# timedatectl set-time '10:42:43'

如果设置了自动同步时间的功能,设置将会无效:

Failed to set time: Automatic time synchronization is enabled

4. 时区设置

timedatectl status // 列出系统时区和时间信息
timedatectl list-timezones // 列出所有时区
timedatectl set-timezone 'Asia/Shanghai’

Reference

  1. Linux Set Date and Time From a Command Prompt
  2. date
  3. Using the hwclock Command
  4. Linux下使用timedatectl命令时间时区操作详解
    原文作者:keith666
    原文地址: https://www.jianshu.com/p/7a770f541f91
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞