Centos7中增加了timedate命令來時間對日期、時間和時區的控制。
[root@node1 ~]# 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
一、顯示日期、時間、時區等相關信息
[root@node1 ~]# timedatectl Local time: 日 2016-04-17 00:20:06 CST Universal time: 六 2016-04-16 16:20:06 UTC RTC time: 六 2016-04-16 16:20:06 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: n/a
二、修改時間
執行如下命令修改時間:
timedatectl set-time HH:MM:SS
備注:同時修改hardware clock和system clock.
例子:
[root@node1 ~]# timedatectl set-time 09:57:30 [root@node1 ~]# date 2016年 04月 17日 星期日 09:57:32 CST
三、修改日期
執行如下命令修改日期:
timedatectl set-time YYYY-MM-DD
備注:修改日志不同時修改時間,將重置現在的時間為00:00:00.
例子:
[root@node1 ~]# timedatectl set-time 2016-04-18 [root@node1 ~]# date 2016年 04月 18日 星期一 00:00:01 CST
四、同時修改日期和時間
執行如下命令同時修改日期和時間:
timedatectl set-time 'YYYY-MM-DD HH:MM:SS'
例子:
[root@node1 ~]# timedatectl set-time '2016-04-18 10:01:20' [root@node1 ~]# date 2016年 04月 18日 星期一 10:01:21 CST
五、修改時區
執行如下命令顯示可用的時區:
timedatectl list-timezones
[root@node1 ~]# timedatectl list-timezones Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmara Africa/Bamako Africa/Bangui Africa/Banjul ...
執行如下命令設置使用的時區:
timedatectl set-timezone time_zone
[root@node1 ~]# timedatectl set-timezone Asia/Shanghai [root@node1 ~]# date 2016年 04月 18日 星期一 10:05:28 CST [root@node1 ~]# ls -lrt /etc/localtime lrwxrwxrwx 1 root root 35 4月 17 00:08 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
六、同步時間到一個遠程服務器
timedatectl命令可以用來控制是否開啟NTP,開啟NTP將啟動chronyd或者ntpd服務,依賴於被安裝的那個。
timedatectl set-ntp boolean
開啟:
timedatectl set-ntp yes
關閉:
timedatectl set-ntp no
備注:
-
執行set-ntp時會同時開啟或關閉ntpd或者chronyd服務。但是ntpd服務和chronyd可以通過systemctl命令來單獨控制,不是必須使用timedatectl來進行控制。
-
如果使用set-ntp是yes的狀態(即:timedatectl命令中NTP enabled狀態顯示為yes,那么將不能同時使用set-time來修改時間。
[root@node3 ~]# timedatectl Local time: 一 2016-04-18 10:25:48 CST Universal time: 一 2016-04-18 02:25:48 UTC RTC time: 六 2016-04-16 17:09:34 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: yes NTP synchronized: no RTC in local TZ: no DST active: n/a [root@node3 ~]# timedatectl set-time '2016-04-18 10:25:58' Failed to set time: Automatic time synchronization is enabled [root@node3 ~]# timedatectl set-ntp no [root@node3 ~]# timedatectl Local time: 一 2016-04-18 10:26:16 CST Universal time: 一 2016-04-18 02:26:16 UTC RTC time: 六 2016-04-16 17:10:02 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a [root@node3 ~]# timedatectl set-time '2016-04-18 10:26:28'