1. 程式人生 > 實用技巧 >CentOS 7修改系統時間及硬體時間

CentOS 7修改系統時間及硬體時間

在新的centos7裡,關於時間的指令除了保留了之前版本中常用到的date、hwclock等命令外,還增加了一個統一的命令timedatactl。下面結合其用法進行下小結。

先看下命令幫助:

[root@nginx ~]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

  -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

Commands:
  status                   Show current time settings
  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             Control whether NTP is enabled

檢視當前設定,直接輸入timedatectl等同於timedatectl status

[root@tgcdn-qd-133 /root]
#timedatectl 
      Local time: Tue 2018-03-13 10:41:20 CST
  Universal time: Tue 2018-03-13 02:41:20 UTC
        RTC time: Tue 2018-03-13 02:42:29
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

設定當前時間:

timedatectl set-time HH:MM:SS

設定當前日期:

timedatectl set-time YYYY-MM-DD

預設的,系統是使用UTC時間的,可以用以下命令開啟和關閉UTC時間:

timedatectl set-local-rtc boolean

把 boolean 替換成yes則表示使用本地時間,替換成no則表示是UTC時間

注:這裡的boolean值也可以用true、flase

設定時區:

檢視所有的時區:

timedatectl list-timezones

設定時區

timedatectl set-timezone time_zone

遠端NTP伺服器同步

timedatectl還可以設定是否開啟NTP選項

timedatectl set-ntp boolean

這裡的boolean是yes或者no

除了以上用法,timedatectl 還支援通過-H引數進行遠端主機的時間設定。

檢視系統時間

[root@nginx ~]# date
2018年 03月 13日 星期二 10:58:41 CST

檢視硬體時間

[root@nginx ~]# hwclock --show
2018年03月13日 星期二 11時00分16秒  -0.086030 秒

假如系統時間和硬體時間都不準確

更新時區

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

更新日期

[root@localhost ~]# timedatectl set-time 2017-06-13

更新時間

[root@localhost ~]# timedatectl set-time 2017-06-13

將系統時間和硬體時間調整為一致

[root@localhost ~]# timedatectl set-local-rtc 1
或者
[root@localhost ~]# hwclock --systohc --localtime

將日期寫入CMOS

[root@localhost ~]# clock –w

轉載至http://blog.csdn.net/shudaqi2010/article/details/77977532