对于许多与系统相关的任务和进程,使用正确的时区是必不可少的。 例如,cron守护进程使用系统的时区运行cron作业,日志文件的时间戳基于同一系统的时区。
环境
Centos8
检查当前时区
timedatectl是命令行实用程序,您可以在其中查看和修改系统的日期和时间。
[root@localhost ~]# timedatectl
local time : tue 2020-03-3116336035336023 CST
通用时间: tue 2020-03-3108336035336023 utc
RTC time : tue 2020-03-3108336035336023
时间区域: Asia/Shanghai (CST,0800 ) ) ) ) ) )。
系统时钟同步:是
NTP服务:活动
RTC in local TZ: no
更改时区
在更改时区之前,必须找到要使用的时区名称。 要列出所有可用的时区,请使用以下命令:
[ root @ localhost~] # timedatectllist-time zones
.
Asia/Samarkand
Asia/Seoul
Asia /上海
Asia/Singapore
Asia/Srednekolymsk
.
然后,使用set-timezone选项更改时区,如Europe/Berlin:
[ root @ localhost~] # timedatectlset-time zone Europe/Berlin
创建链接以更改时区
如果运行的是Centos早期版本(如Centos6之前的版本),并且系统中不存在timedatectl命令,则/etc/localtime符号位于/usr/share/zoneinfo目录中
查看/usr/share/zoninfo下的时区文件,然后单击,
[ root @ localhost~] # ls/usr/share/zoneinfo /
[ root @ localhost~] # ls/usr/share/zoneinfo/Asia /
替换时区中的文件。
[ root @ localhost~] # ln-SF/usr/share/zoneinfo/Asia/Shanghai/etc/local time
[root@localhost ~]# date
tuemar3116:53:59CST2020
可以看出已经切换到了CST中国标准时间
总结
本文使用了两种方法来修改时区。 一种方法是使用timedatectl,另一种方法是创建链接。