Linux(Ubuntu) 下时区的修改方法

刚搞到一台HK的Ubuntu,花了N小时一个个配置好nginx+php+mysql+redis环境之后,才想起有一键安装包这种东西ORZ。。

本文内容于Ubuntu14.04下尝试,不保证本文内容在其他版本的Linux下适用。

言归正传,发现默认的时区是GMT+000,每次看时间不对,很不爽有木有

更改方法如下:
(我是在root下执行的,如果不能正确设置,请尝试使用切换到root用户或使用sudo命令)

1.确定时区编号

运行tzselect,按提示选择编号,最后会告诉你一个TZ value

root:~# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, “coord”, or “TZ”.

  1. Africa
  2. Americas
  3. Antarctica
  4. Arctic Ocean
    5) Asia
  5. Atlantic Ocean
  6. Australia
  7. Europe
  8. Indian Ocean
  9. Pacific Ocean
  10. coord - I want to use geographical coordinates.
  11. TZ - I want to specify the time zone using the Posix TZ format.

#? 5

Please select a country whose clocks agree with yours.

  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
  9. Cyprus 27) Lebanon 44) Tajikistan
  10. East Timor 28) Macau 45) Thailand
  11. Georgia 29) Malaysia 46) Turkmenistan
  12. Hong Kong 30) Mongolia 47) United Arab Emirates
  13. India 31) Myanmar (Burma) 48) Uzbekistan
  14. Indonesia 32) Nepal 49) Vietnam
  15. Iran 33) Oman 50) Yemen
  16. 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: Sat Oct 17 13:09:34 CST 2015.
Universal Time is now: Sat Oct 17 05:09:34 UTC 2015.
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

设置localtime

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
粗体部分为上一步得到的时区编号,至此,更改完毕。

检查当前时区和时间

执行date -R

Sat, 17 Oct 2015 13:28:13 +0800

可以看到时区已经变为+0800
如果时区改了之后时间还不正确,可以试着同步时间ntpdate time.windows.com,时间服务器可以任意使用其他可用的替换(如time.nist.gov)

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~

支付宝
微信