RHEL7、CentOS7提供三种命令行方式方式来设置和显示日期、时间。timedatectl是在RHEL7及CentOS7中新增的systemd的一部分,date是传统的日期时间设置命令,hwclock单元访问的是硬件时钟。
一、timedatectl
[root@Geeklp-Administrator ~]# timedatectl
Local time: 六 2017-12-16 19:49:53 CST
Universal time: 六 2017-12-16 11:49:53 UTC
RTC time: 六 2017-12-16 11:43:53
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
默认是开启你ntp时间同步的,在修改NTP或chrony的时间设置之后不会立即生效,需要重启timedatectl服务。
[root@Geeklp-Administrator ~]# systemctl restart systemd-timedated.service
(1)修改当前时间
timedatectl set-time HH:MM:SS
这个命令同时更新系统时间和硬件时钟,结果类似于date –set 和 hwclock –systohc 命令。开启NTP时间同步是不能用这个命令来设置时间,这一点需要注意一下。
[root@Geeklp-Administrator ~]# timedatectl set-time 13:00:27
Failed to set time: Automatic time synchronization is enabled
[root@Geeklp-Administrator ~]# timedatectl set-ntp no
[root@Geeklp-Administrator ~]# timedatectl set-time 13:00:27
[root@Geeklp-Administrator ~]# timedatectl
Local time: 六 2017-12-16 13:01:12 CST
Universal time: 六 2017-12-16 05:01:12 UTC
RTC time: 六 2017-12-16 05:01:13
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
系统默认的时间设置使用的UTC,设置系统时间使用本地时间。
timedatectl set-local-rtc boolean
boolean值为:yes (或者y, true, t, 1)。当boolean值为no时使用的是UTC时间,此时的值为:no(或者n, false, f, 0),默认值为no。
[root@Geeklp-Admi