rhel/centos时区和本地时间设置
一、rhel6/centos6时区配置
在 rhel6/centos6及其之前的版本中设置时区和本地时间配置文件相对比较麻烦。其对应的有两个文件/etc/sysconfig/clock和/etc/localtime。其中/etc/localtime文件是从/usr/share/zoneinfo/目录下的文件软链或者直接覆盖的。如:
1[root@localhost ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
2cp: overwrite `/etc/localtime'? y
该文件无法直接打开,打开是乱码,不过可以通过strings可以看到有CST-8这样的字样,同样还可以通过date +%z 获取当前的时区信息。
而/etc/sysconfig/clock里面会有zone的配置项,这个主要用于主机重启后用于读取或者第三方应用程序使用本地时区时,通过该文件读取的。
1# cat /etc/sysconfig/clock
2# The time zone of the system is defined by the contents of /etc/localtime.
3# This file is only for evaluation by system-config-date, do not rely on its
4# contents elsewhere.
5ZONE="Asia/Shanghai"
而对于时区信息设置同样还可以通过tzselect进行设置。只不过使用tzselect配置选择后,并不会直接成功,还需要在环境变量里进行设置。
1# tzselect
2Please identify a location so that time zone rules can be set correctly.
3Please select a continent or ocean.
4 1) Africa
5 2) Americas
6 3) Antarctica
7 4) Arctic Ocean
8 5) Asia
9 6) Atlantic Ocean
10 7) Australia
11 8) Europe
12 9) Indian Ocean
1310) Pacific Ocean
1411) none - I want to specify the time zone using the Posix TZ format.
15#? 5
16Please select a country.
17 1) Afghanistan 18) Israel 35) Palestine
18 2) Armenia 19) Japan 36) Philippines
19 3) Azerbaijan 20) Jordan 37) Qatar
20 4) Bahrain 21) Kazakhstan 38) Russia
21 5) Bangladesh 22) Korea (North) 39) Saudi Arabia
22 6) Bhutan 23) Korea (South) 40) Singapore
23 7) Brunei 24) Kuwait 41) Sri Lanka
24 8) Cambodia 25) Kyrgyzstan 42) Syria
25 9) China 26) Laos 43) Taiwan
2610) Cyprus 27) Lebanon 44) Tajikistan
2711) East Timor 28) Macau 45) Thailand
2812) Georgia 29) Malaysia 46) Turkmenistan
2913) Hong Kong 30) Mongolia 47) United Arab Emirates
3014) India 31) Myanmar (Burma) 48) Uzbekistan
3115) Indonesia 32) Nepal 49) Vietnam
3216) Iran 33) Oman 50) Yemen
3317) Iraq 34) Pakistan
34#? 9
35Please select one of the following time zone regions.
361) east China - Beijing, Guangdong, Shanghai, etc.
372) Heilongjiang (except Mohe), Jilin
383) central China - Sichuan, Yunnan, Guangxi, Shaanxi, Guizhou, etc.
394) most of Tibet & Xinjiang
405) west Tibet & Xinjiang
41#? 1
42The following information has been given:
43 China
44 east China - Beijing, Guangdong, Shanghai, etc.
45Therefore TZ='Asia/Shanghai' will be used.
46Local time is now: Mon Feb 18 16:40:32 CST 2019.
47Universal Time is now: Mon Feb 18 08:40:32 UTC 2019.
48Is the above information OK?
491) Yes
502) No
51#? 1
52You can make this change permanent for yourself by appending the line
53 TZ='Asia/Shanghai'; export TZ
54to the file '.profile' in your home directory; then log out and log in again.
55Here is that TZ value again, this time on standard output so that you
56can use the /usr/bin/tzselect command in shell scripts:
57Asia/Shanghai
具体可以看上面最后面的提示,这个可以应用于比如,本地时区设置后,java等应用程序读取的还是不正确,这时候就可以在对应的用户属性文件下进行设置。
二、rhel7/centos7配置
在rhel7下由于有了timedatectl命令,对于时区的设置比较简单。直接使用timedatectl list-timezones可以列出所有的时区,同时可以使用timedatectl set-timezone 命令进行时区配置。
1# timedatectl set-timezone "Asia/Shanghai"
该命令的实际操作就是把/usr/share/zoneinfo/Asia/Shanghai 文件软链到了/etc/localtime 。如果再更改为其他时区,对应的链接关系同样进行了修改。
另外需要注意的是在rhel7/centos7上/etc/sysconfig/clock文件默认没有了。不过tzselect命令还是在的,同样对于普通用的户的时区设置还是可以使用环境变量进行配置的:
1TZ='Asia/Shanghai'; export TZ
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/centos-localtime-timezone/6002.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.