Linux系统更改时区

更改时区

在给国外伙伴部署机器的时候,经常会碰到需要给Linux修改时区的情况:

这里例举几个方式:

1:覆盖/etc/localtime文件内容

/usr/share/zoneinfo/ 下面都是对应的洲以及其对应的时区,如果我们需要修改时区为Shanghai的话,执行下面的操作:

[root@VM-75-60 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite `/etc/localtime'? y
[root@VM-75-60 ~]#

检测是否生效:

[root@VM-75-60 ~]# date -R
Fri, 24 Jul 2020 22:59:39 +0800

最后的参数‘+0800’就表示东八区!

 

2:tzselect指令

[root@VM-75-60 ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.

通过交互式,利用首位的数字去选择你想要选择的时区。

但这里tzselect并不能真正设置当前机器的时区,他只是一个向导,让你找到你想要找到的地区时区表示方式以及如何让它生效,指令在最后给出了:

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.

你看,他让你编辑.profile,设置个TZ变量。。。

 

第一种方法直接cp覆盖不香吗?

 

注意:升级了glibc之后的机器,crond和rsyslog等程序的读取时间都会变成新的glibc下的localtime,因此需要重新做软连接,把/etc/localtime软连接到新的glibc下的localtime

posted @ 2020-07-21 22:04  一个运维  阅读(4920)  评论(1编辑  收藏  举报