做开发时我们通常会在虚拟机里跑一套Linux系统,比如常用的Centos,如果我们默认英文安装时时区和时间设置通常与我们本地系统时间有所差异,此时为了以后运营的服务端系统准确对时,我们需要把虚拟机里的系统时间与我们本地系统同步。下面,青衿就教大家如何修改centos的时区和时间。

第一步:使用如下命令来选择我们的位置,以便设置时区规则。

tzselect

第二步:依次选择大洲,5(Asia)——选择国家,9(China)——选择时区,1(Beijing Time),最后确定选择,1(YES),可参考如下打印过程。 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.
  12. Afghanistan 18) Israel 35) Palestine
  13. Armenia 19) Japan 36) Philippines
  14. Azerbaijan 20) Jordan 37) Qatar
  15. Bahrain 21) Kazakhstan 38) Russia
  16. Bangladesh 22) Korea (North) 39) Saudi Arabia
  17. Bhutan 23) Korea (South) 40) Singapore
  18. Brunei 24) Kuwait 41) Sri Lanka
  19. Cambodia 25) Kyrgyzstan 42) Syria
  20. China 26) Laos 43) Taiwan
  21. Cyprus 27) Lebanon 44) Tajikistan
  22. East Timor 28) Macau 45) Thailand
  23. Georgia 29) Malaysia 46) Turkmenistan
  24. Hong Kong 30) Mongolia 47) United Arab Emirates
  25. India 31) Myanmar (Burma) 48) Uzbekistan
  26. Indonesia 32) Nepal 49) Vietnam
  27. Iran 33) Oman 50) Yemen
  28. Iraq 34) Pakistan #? 9 Please select one of the following time zone regions.
  29. Beijing Time
  30. Xinjiang Time #? 1

The following information has been given:

China
Beijing Time

Therefore TZ='Asia/Shanghai' will be used. Local time is now: Tue Jul 5 10:54:49 CST 2016. Universal Time is now: Tue Jul 5 02:54:49 UTC 2016. 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

第三步:设置环境变量值使修改永久生效。

使用vi命令编辑**/etc/profile**文件,在最底部添加 TZ='Asia/Shanghai'; export TZ 保存即可。

我们再次使用date命令查看虚拟机系统的时间,发现已经与我们本地系统时间一致了。

date

Tue Jul  5 11:34:22 CST 2016