Linux查看设置系统时区
關(guān)于時(shí)區(qū)的概念,其實(shí)初中地理課已經(jīng)涉及,很多人都多少了解一些,可能只是細(xì)節(jié)搞不太清楚。為什么會(huì)將地球分為不同時(shí)區(qū)呢?因?yàn)榈厍蚩偸亲晕飨驏|自轉(zhuǎn),東邊總比西邊先看到太陽(yáng),東邊的時(shí)間也總比西邊的早。東邊時(shí)刻與西邊時(shí)刻的差值不僅要以時(shí)計(jì),而且還要以分和秒來(lái)計(jì)算。整個(gè)地球分為二十四時(shí)區(qū),每個(gè)時(shí)區(qū)都有自己的本地時(shí)間。在國(guó)際無(wú)線電通信場(chǎng)合,為了統(tǒng)一起見(jiàn),使用一個(gè)統(tǒng)一的時(shí)間,稱為通用協(xié)調(diào)時(shí)(UTC, Universal Time Coordinated)。UTC與格林尼治平均時(shí)(GMT, Greenwich Mean Time)一樣,都與英國(guó)倫敦的本地時(shí)相同。
關(guān)于時(shí)間的幾個(gè)標(biāo)準(zhǔn),如下所示,具體可以Google、Baidu搜索一下。
?
???? CST:中國(guó)標(biāo)準(zhǔn)時(shí)間(China Standard Time),這個(gè)解釋可能是針對(duì)RedHat Linux。
???? UTC:協(xié)調(diào)世界時(shí),又稱世界標(biāo)準(zhǔn)時(shí)間,簡(jiǎn)稱UTC,從英文國(guó)際時(shí)間/法文協(xié)調(diào)時(shí)間”Universal Time/Temps Cordonné”而來(lái)。中國(guó)大陸、香港、澳門、臺(tái)灣、蒙古國(guó)、新加坡、馬來(lái)西亞、菲律賓、澳洲西部的時(shí)間與UTC的時(shí)差均為+8,也就是UTC+8。
???? GMT:格林尼治標(biāo)準(zhǔn)時(shí)間(舊譯格林威治平均時(shí)間或格林威治標(biāo)準(zhǔn)時(shí)間;英語(yǔ):Greenwich Mean Time,GMT)是指位于英國(guó)倫敦郊區(qū)的皇家格林尼治天文臺(tái)的標(biāo)準(zhǔn)時(shí)間,因?yàn)楸境踝游缇€被定義在通過(guò)那里的經(jīng)線。
?
關(guān)于時(shí)區(qū)分布圖,大家可以參考http://www.timedate.cn/time/timezone.htm,我們國(guó)家跨越了東五區(qū)、東六區(qū)、東七區(qū)、東八區(qū)、東九區(qū)五個(gè)時(shí)區(qū),一般都統(tǒng)一采用東八區(qū)計(jì)時(shí)時(shí)間。
?
Linux系統(tǒng)如何查看設(shè)置所在的時(shí)區(qū)呢?
?
1:使用date命令查看時(shí)區(qū)
[root@db-server ~]# date -R ? Sun, 11 Jan 2015 07:10:28 -0800 ? [root@db-server ~]# ?如上RFC 2822 format所示,上面命令輸出了-0800表示西八區(qū),是美國(guó)舊金山所在的時(shí)區(qū),下面表示我們國(guó)家的東八區(qū)(+0800)
[root@lnx01 ~]# date -R ? Sun, 11 Jan 2015 23:06:02 +0800 ??
2:查看clock系統(tǒng)配置文件
[root@db-server ~]# more /etc/sysconfig/clock ZONE="America/Los_Angeles" UTC=true ARC=false?
如上所示,表示該系統(tǒng)設(shè)置的時(shí)區(qū)為“America/Los_Angeles”,也就是西八區(qū)。
?
Linux系統(tǒng)如何設(shè)置系統(tǒng)所在的時(shí)區(qū)呢?
方法1:使用tzselect設(shè)置時(shí)區(qū)
例子,將系統(tǒng)時(shí)區(qū)設(shè)置為東八區(qū)(北京時(shí)間)
[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命令只告訴你選擇的時(shí)區(qū)的寫法,并不會(huì)生效。所以現(xiàn)在它還不是東8區(qū)北京時(shí)間。你可以在.profile、.bash_profile或者/etc/profile中設(shè)置正確的TZ環(huán)境變量并導(dǎo)出。 例如在.bash_profile里面設(shè)置 TZ='Asia/Shanghai'; export TZ并使其生效。
[root@db-server ~]# source .bash_profile ? [root@db-server ~]# date ? Sun Jan 11 23:37:40 CST 2015 ??
方法2:復(fù)制相應(yīng)的時(shí)區(qū)文件,替換系統(tǒng)時(shí)區(qū)文件;或者創(chuàng)建鏈接文件
?
在/usr/share/zoneinfo/下面有很多時(shí)區(qū)文件,如下所示,可以復(fù)制這些時(shí)區(qū)文件覆蓋/etc/localtime文件,或修改符號(hào)鏈接/etc/locatime對(duì)應(yīng)的文件
[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 ~]#
注意如果有時(shí)候,執(zhí)行了上面命令后,使用date -R發(fā)現(xiàn)時(shí)區(qū)設(shè)置沒(méi)有生效,有可能是因?yàn)槟阍趐rofile或.bash_profile里面設(shè)置了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的時(shí)區(qū)文件
?
[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設(shè)置時(shí)區(qū)
timeconfig命令僅限于RedHat Linux 和 CentOS。
參 數(shù):
--arc 使用Alpha硬件結(jié)構(gòu)的格式存儲(chǔ)系統(tǒng)時(shí)間。
--back 在互動(dòng)式界面里,顯示Back鈕而非Cancel鈕。
--test 僅作測(cè)試,并不真的改變系統(tǒng)的時(shí)區(qū)。
--utc 把硬件時(shí)鐘上的時(shí)間視為CUT,有時(shí)也稱為UTC或UCT。
感覺(jué)這個(gè)timeconfig命令在命令界面使用起來(lái)并不好用。給人的感覺(jué)非常別扭。用戶體驗(yàn)非常糟糕。如下所示,看起來(lái)也很糟糕。
[root@db-server ~]# timeconfig --utc "Asia/Shanghai"
?
方法4:使用dpkg-reconfigure tzdata設(shè)置時(shí)區(qū)
這個(gè)僅僅適用于Debian,手頭沒(méi)有環(huán)境,沒(méi)有實(shí)驗(yàn)過(guò),在此不作過(guò)多闡述。
總結(jié)
以上是生活随笔為你收集整理的Linux查看设置系统时区的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 30万的埃尔法加价卖100万 丰田章男:
- 下一篇: 实例解析linux内核I2C体系结构(2