做开发时我们通常会在虚拟机里跑一套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.
- Africa
- Americas
- Antarctica
- Arctic Ocean
- Asia
- Atlantic Ocean
- Australia
- Europe
- Indian Ocean
- Pacific Ocean
- none - I want to specify the time zone using the Posix TZ format. #? 5 Please select a country.
- Afghanistan 18) Israel 35) Palestine
- Armenia 19) Japan 36) Philippines
- Azerbaijan 20) Jordan 37) Qatar
- Bahrain 21) Kazakhstan 38) Russia
- Bangladesh 22) Korea (North) 39) Saudi Arabia
- Bhutan 23) Korea (South) 40) Singapore
- Brunei 24) Kuwait 41) Sri Lanka
- Cambodia 25) Kyrgyzstan 42) Syria
- China 26) Laos 43) Taiwan
- Cyprus 27) Lebanon 44) Tajikistan
- East Timor 28) Macau 45) Thailand
- Georgia 29) Malaysia 46) Turkmenistan
- Hong Kong 30) Mongolia 47) United Arab Emirates
- India 31) Myanmar (Burma) 48) Uzbekistan
- Indonesia 32) Nepal 49) Vietnam
- Iran 33) Oman 50) Yemen
- Iraq 34) Pakistan #? 9 Please select one of the following time zone regions.
- Beijing Time
- 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?
- Yes
- 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