Linux设置时区

1. 首先要确定时区

时区设置用tzselect 命令来实现。但是通过tzselect命令设置TZ这个环境变量来选择的时区,需要将变量添加到.profile文件中。

[root@test tmp]# tzselect 
执行tzselect命令 --> 选择Asia --> 选择China --> 选择Beijing Time--> 选择Yes。

选择后会提示:

You can make this change permanent for yourself by appending the line
	TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
 
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai

2. 配置时区

执行完tzselect命令选择时区后,时区并没有更改,只是在命令最后提示你可以执行 TZ=’Asia/Shanghai’; export TZ 并将这行命令添加到.profile中。

TZ='Asia/Shanghai'; export TZ

执行vim指令打开profile文件:

vim /etc/profile

进入文件编辑器,然后按下键盘的i进入insert状态进行编辑。下面有一个insert显示的就是状态,然后就可以把光标移到最下面内容添加:

TZ='Asia/Shanghai'; export TZ

修改完之后按Esc键退出编辑页面,此时下面的insert已经没有了,表示已经退出了编辑状态。然后按住shift键加:(冒号),此时下面是可输入的状态,输入wq就退出了编辑器。

在 profile 文件尾部追加上面的配置信息后。然后执行source指令使配置生效。

source /etc/profile

执行 date 查看时间:

[root@test tmp]# date
Sat Mar 14 22:34:56 CST 2020

可以看到和当前时间是一致的。说明配置已经生效。

 

 

参考文章:https://www.cnblogs.com/yunian139/p/11990475.html

https://www.cnblogs.com/xiaonian8/p/13766992.html

https://blog.csdn.net/ali_lili/article/details/93037858