Linux查看设置系统时区
關于時區的概念,其實初中地理課已經涉及,很多人都多少了解一些,可能只是細節搞不太清楚。為什么會將地球分為不同時區呢?因為地球總是自西向東自轉,東邊總比西邊先看到太陽,東邊的時間也總比西邊的早。東邊時刻與西邊時刻的差值不僅要以時計,而且還要以分和秒來計算。整個地球分為二十四時區,每個時區都有自己的本地時間。在國際無線電通信場合,為了統一起見,使用一個統一的時間,稱為通用協調時(UTC, Universal Time Coordinated)。UTC與格林尼治平均時(GMT, Greenwich Mean Time)一樣,都與英國倫敦的本地時相同。
關于時間的幾個標準,如下所示,具體可以Google、Baidu搜索一下。
?
???? CST:中國標準時間(China Standard Time),這個解釋可能是針對RedHat Linux。
???? UTC:協調世界時,又稱世界標準時間,簡稱UTC,從英文國際時間/法文協調時間”Universal Time/Temps Cordonné”而來。中國大陸、香港、澳門、臺灣、蒙古國、新加坡、馬來西亞、菲律賓、澳洲西部的時間與UTC的時差均為+8,也就是UTC+8。
???? GMT:格林尼治標準時間(舊譯格林威治平均時間或格林威治標準時間;英語:Greenwich Mean Time,GMT)是指位于英國倫敦郊區的皇家格林尼治天文臺的標準時間,因為本初子午線被定義在通過那里的經線。
?
關于時區分布圖,大家可以參考http://www.timedate.cn/time/timezone.htm,我們國家跨越了東五區、東六區、東七區、東八區、東九區五個時區,一般都統一采用東八區計時時間。
?
Linux系統如何查看設置所在的時區呢?
?
1:使用date命令查看時區
[root@db-server ~]# date -R ? Sun, 11 Jan 2015 07:10:28 -0800 ? [root@db-server ~]# ?如上RFC 2822 format所示,上面命令輸出了-0800表示西八區,是美國舊金山所在的時區,下面表示我們國家的東八區(+0800)
[root@lnx01 ~]# date -R ? Sun, 11 Jan 2015 23:06:02 +0800 ??
2:查看clock系統配置文件
[root@db-server ~]# more /etc/sysconfig/clock ZONE="America/Los_Angeles" UTC=true ARC=false?
如上所示,表示該系統設置的時區為“America/Los_Angeles”,也就是西八區。
?
Linux系統如何設置系統所在的時區呢?
方法1:使用tzselect設置時區
例子,將系統時區設置為東八區(北京時間)
[root@db-server ~]# 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) east China - Beijing, Guangdong, Shanghai, etc. 2) Heilongjiang (except Mohe), Jilin 3) central China - Sichuan, Yunnan, Guangxi, Shaanxi, Guizhou, etc. 4) most of Tibet & Xinjiang 5) west Tibet & Xinjiang #? 1 ? The following information has been given: ? ??????? China ??????? east China - Beijing, Guangdong, Shanghai, etc. ? Therefore TZ='Asia/Shanghai' will be used. Local time is now:????? Sun Jan 11 23:31:51 CST 2015. Universal Time is now:? Sun Jan 11 15:31:51 UTC 2015. Is the above information OK? 1) Yes 2) No #? yes Please enter 1 for Yes, or 2 for 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注意:tzselect命令只告訴你選擇的時區的寫法,并不會生效。所以現在它還不是東8區北京時間。你可以在.profile、.bash_profile或者/etc/profile中設置正確的TZ環境變量并導出。 例如在.bash_profile里面設置 TZ='Asia/Shanghai'; export TZ并使其生效。
[root@db-server ~]# source .bash_profile ? [root@db-server ~]# date ? Sun Jan 11 23:37:40 CST 2015 ??
方法2:復制相應的時區文件,替換系統時區文件;或者創建鏈接文件
?
在/usr/share/zoneinfo/下面有很多時區文件,如下所示,可以復制這些時區文件覆蓋/etc/localtime文件,或修改符號鏈接/etc/locatime對應的文件
[root@db-server ~]# date -R Mon, 12 Jan 2015 10:42:26 +0800 [root@db-server ~]# cp /usr/share/zoneinfo/America/Los_Angeles /etc/localtime cp: overwrite `/etc/localtime'? y [root@db-server ~]# date -R Sun, 11 Jan 2015 18:42:49 -0800 [root@db-server ~]#
注意如果有時候,執行了上面命令后,使用date -R發現時區設置沒有生效,有可能是因為你在profile或.bash_profile里面設置了TZ,如下案例所示:
[root@db-server ~]# date -R Sun, 11 Jan 2015 18:42:49 -0800 [root@db-server ~]# vi .bash_profile # .bash_profile ? # Get the aliases and functions if [ -f ~/.bashrc ]; then ??????? . ~/.bashrc fi ? # User specific environment and startup programs ? PATH=$PATH:$HOME/bin ? TZ='Asia/Shanghai'; export TZ export PATH unset USERNAME ~ ~ ".bash_profile" 14L, 221C written [root@db-server ~]# source .bash_profile [root@db-server ~]# date -R Mon, 12 Jan 2015 10:44:35 +0800?
方式2:修改鏈接/etc/locatime的時區文件
?
[root@db-server ~]# ln /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ln: creating hard link `/etc/localtime' to `/usr/share/zoneinfo/Asia/Shanghai': File exists
[root@db-server ~]# rm /etc/localtime
rm: remove regular file `/etc/localtime'? y
[root@db-server ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@db-server ~]# date -R
Mon, 12 Jan 2015 10:56:10 +0800
[root@db-server ~]#
?
方法3:使用timeconfig設置時區
timeconfig命令僅限于RedHat Linux 和 CentOS。
參 數:
--arc 使用Alpha硬件結構的格式存儲系統時間。
--back 在互動式界面里,顯示Back鈕而非Cancel鈕。
--test 僅作測試,并不真的改變系統的時區。
--utc 把硬件時鐘上的時間視為CUT,有時也稱為UTC或UCT。
感覺這個timeconfig命令在命令界面使用起來并不好用。給人的感覺非常別扭。用戶體驗非常糟糕。如下所示,看起來也很糟糕。
[root@db-server ~]# timeconfig --utc "Asia/Shanghai"
?
方法4:使用dpkg-reconfigure tzdata設置時區
這個僅僅適用于Debian,手頭沒有環境,沒有實驗過,在此不作過多闡述。
總結
以上是生活随笔為你收集整理的Linux查看设置系统时区的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 30万的埃尔法加价卖100万 丰田章男:
- 下一篇: 源码安装mysql5.1.51