如何配置正确的时区?

RHEL-Like

1 前言

笔者最近在实践中发现CentOS 7.x中配置时区久方法配置出来有点奇怪,后来结论是systemd的问题,那systemd环境下如何配置新的时区?

2 实践部分

2.1 旧的时区配置方法

2.1.1 查询当前时区

date -R

显示如下:

Thu, 21 Dec 2017 20:59:43 +0800

或者

ls -l /etc/localtime

显示如下:

lrwxrwxrwx. 1 root root 35 Dec 21 20:14 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

注:时区的本质是一个软连接到一个特定的时区

2.1.2 更改时区

rm /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

2.2 systemd环境下的操作

2.2.1 查询时区

timedatectl

显示如下:

Warning: Ignoring the TZ variable. Reading the system's time zone setting only.

      Local time: Thu 2017-12-21 20:39:50 CST
  Universal time: Thu 2017-12-21 12:39:50 UTC
        RTC time: Thu 2017-12-21 12:39:50
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

以上显示:
– CST即是China Shanghai Time的缩写
– UTC即是Coordinated Universal Time的缩写

注:如何使用旧的配置方法,命令会显示成Time zone: America/New_York (CST, +0800),请使用下面的配置方法更正

2.2.2 得到新的时区值

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.
#? 5
Please select a country.
 1) Afghanistan           18) Israel                35) Palestine
 2) Armenia               19) Japan                 36) Philippines
 3) Azerbaijan            20) Jordan                37) Qatar
 4) Bahrain               21) Kazakhstan            38) Russia
 5) Bangladesh            22) Korea (North)         39) Saudi Arabia
 6) Bhutan                23) Korea (South)         40) Singapore
 7) Brunei                24) Kuwait                41) Sri Lanka
 8) Cambodia              25) Kyrgyzstan            42) Syria
 9) China                 26) Laos                  43) Taiwan
10) Cyprus                27) Lebanon               44) Tajikistan
11) East Timor            28) Macau                 45) Thailand
12) Georgia               29) Malaysia              46) Turkmenistan
13) Hong Kong             30) Mongolia              47) United Arab Emirates
14) India                 31) Myanmar (Burma)       48) Uzbekistan
15) Indonesia             32) Nepal                 49) Vietnam
16) Iran                  33) Oman                  50) Yemen
17) Iraq                  34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1

The following information has been given:

        China
        Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Local time is now:      Thu Dec 21 20:20:39 CST 2017.
Universal Time is now:  Thu Dec 21 12:20:39 UTC 2017.
Is the above information OK?
1) Yes
2) No
#? 1

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

注:最后一行显示要配置的时区值是“Asia/Shanghai”,其实可以如下处理:

ls /usr/share/zoneinfo/Asia/Shanghai

注:即是查询zoneinfo文件夹,适合熟悉的朋友

2.2.3 设置新的时区值

timedatectl set-timezone 'Asia/Shanghai'
没有评论

发表回复

RHEL-Like
如何配置rsyncd服务?

1 前言 一个问题,一篇文章,一出故事。 由于笔者想实现文件通过rsync自动传输,但是又不想使用o …

RHEL-Like
如何用pam_google_authenticator认证模块实现SSH 2FA?

1 前言 一个问题,一篇文章,一出故事。 笔者想开启2FA以便增强SSH服务的安全性,于是便整理此文 …

RHEL-Like
如何升级RHEL clamav杀毒?

1 前言 一个问题,一篇文章,一出故事。 笔者需要卸载旧的病毒软件,然后更新rpm包的病毒软件,于是 …