对外提供服务的服务器上,日期时间的准确尤为重要,有些情况系统时间不准确是由于时区设置错误引起的。
CentOS7系统修改时区仍然可以采用修改配置文件的方法,但为了保证安全习性,我们推荐只用系统预制的命令进行修改。
在CentOS7中,由systemd取代了init,部分命令发生了变化,新增了很多有用的命令,这里我们介绍datetimecel命令:
datetimectl是systemd组件中的内置命令,用来管理系统的日期,时间,时区等信息。
用法如下:
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 set-timezone
我们首选获取系统当前的时区:
timedatectl
输出如下:
可以看到当前为美国纽约时区,并开启了夏令时,我们需要修改为中国北京时间,执行以下命令:
timedatectl set-timezone Asia/Shanghai
再次查看系统时间设定,输出如下:
修改完成。
Tips:如果不清楚需要修改的时区的名字,可以用以下命令查看系统支持的所有时区:
timedatectl list-timezones