Linux查看設置系統時區

    關於時區的概念,其實初中地理課已經涉及,不少人都多少了解一些,可能只是細節搞不太清楚。爲何會將地球分爲不一樣時區呢?由於地球老是自西向東自轉,東邊總比西邊先看到太陽,東邊的時間也總比西邊的早。東邊時刻與西邊時刻的差值不只要以時計,並且還要以分和秒來計算。整個地球分爲二十四時區,每一個時區都有本身的本地時間。在國際無線電通訊場合,爲了統一塊兒見,使用一個統一的時間,稱爲通用協調時(UTC, Universal Time Coordinated)。UTC與格林尼治平均時(GMT, Greenwich Mean Time)同樣,都與英國倫敦的本地時相同。 html

關於時間的幾個標準,以下所示,具體能夠Google、Baidu搜索一下。 shell

 

     CST:中國標準時間(China Standard Time),這個解釋多是針對RedHat Linux。 api

     UTC:協調世界時,又稱世界標準時間,簡稱UTC,從英文國際時間/法文協調時間」Universal Time/Temps Cordonné」而來。中國大陸、香港、澳門、臺灣、蒙古國、新加坡、馬來西亞、菲律賓、澳洲西部的時間與UTC的時差均爲+8,也就是UTC+8。 bash

     GMT:格林尼治標準時間(舊譯格林威治平均時間或格林威治標準時間;英語:Greenwich Mean Time,GMT)是指位於英國倫敦郊區的皇家格林尼治天文臺的標準時間,由於本初子午線被定義在經過那裏的經線。 app

 

關於時區分佈圖,你們能夠參考http://www.timedate.cn/time/timezone.htm,咱們國家跨越了東五區、東六區、東七區、東八區、東九區五個時區,通常都統一採用東八區計時時間。 ide

clip_image001

 

Linux系統如何查看設置所在的時區呢? 測試

 

1:使用date命令查看時區ui

[root@db-server ~]# date -R
 
Sun, 11 Jan 2015 07:10:28 -0800
 
[root@db-server ~]# 
 

如上RFC 2822 format所示,上面命令輸出了-0800表示西八區,是美國舊金山所在的時區,下面表示咱們國家的東八區(+0800)this

[root@lnx01 ~]# date -R
 
Sun, 11 Jan 2015 23:06:02 +0800
 

 

2:查看clock系統配置文件spa

[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對應的文件

clip_image002

[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 ~]# 

clip_image003

注意若是有時候,執行了上面命令後,使用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。

image

感受這個timeconfig命令在命令界面使用起來並很差用。給人的感受很是彆扭。用戶體驗很是糟糕。以下所示,看起來也很糟糕。

[root@db-server ~]# timeconfig --utc "Asia/Shanghai"

clip_image004

 

方法4:使用dpkg-reconfigure tzdata設置時區

這個僅僅適用於Debian,手頭沒有環境,沒有實驗過,在此不做過多闡述。

 

參考資料:

http://blog.sina.com.cn/s/blog_573a052b0100pgt1.html

http://www.dapig.cn/view/3511.html

相關文章
相關標籤/搜索