Centos7:timedatectl命令

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'


转载于:https://my.oschina.net/yulongblog/blog/661622

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值