Linux 下面的时区与时间错误修复过程记载(血泪篇)
1、問題產(chǎn)生
昨晚后臺(tái)的圖片服務(wù)掛了,折騰之后發(fā)現(xiàn)是服務(wù)器的時(shí)間不對(duì),于是看了一下Linux下面關(guān)于時(shí)區(qū)與時(shí)間相關(guān)的命令
[root@root:~] $ date -R Tue, 31 Mar 2015 13:58:25 +0400 [root@root:~] $北京時(shí)間為 +8000 時(shí)區(qū),所以上述的時(shí)區(qū)設(shè)置不對(duì)
于是使用 tzselect 命令設(shè)置時(shí)區(qū)
首先選擇洲
[root@root:~] $ tzselect Please identify a location so that time zone rules can be set correctly. Please select a continent or ocean.1) Africa2) Americas3) Antarctica4) Arctic Ocean5) Asia6) Atlantic Ocean7) Australia8) Europe9) Indian Ocean 10) Pacific Ocean 11) none - I want to specify the time zone using the Posix TZ format. #? 5選擇國(guó)家
Please select a country.1) Afghanistan 18) Israel 35) Palestine2) Armenia 19) Japan 36) Philippines3) Azerbaijan 20) Jordan 37) Qatar4) Bahrain 21) Kazakhstan 38) Russia5) Bangladesh 22) Korea (North) 39) Saudi Arabia6) Bhutan 23) Korea (South) 40) Singapore7) Brunei 24) Kuwait 41) Sri Lanka8) Cambodia 25) Kyrgyzstan 42) Syria9) 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:Chinaeast China - Beijing, Guangdong, Shanghai, etc.Therefore TZ='Asia/Shanghai' will be used. Local time is now: Tue Mar 31 14:04:49 CST 2015. Universal Time is now: Tue Mar 31 06:04:49 UTC 2015. Is the above information OK? 1) Yes 2) No #? 1
系統(tǒng)給出提示
You can make this change permanent for yourself by appending the lineTZ='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我們對(duì)當(dāng)前用戶改變時(shí)區(qū),所以需要修改 /ect/profile 文件 ,并用source 命令使他生效
[root@root:~] $ vim /etc/profile 在 profile 文件中追加 TZ='Asia/Shanghai'; export TZ [root@root:~] $ source /etc/profile做了上述操作之后服務(wù)器的時(shí)間正確了,圖片服務(wù)也沒有出現(xiàn)因?yàn)闀r(shí)間不對(duì)簽名校驗(yàn)失敗的情況,圖片正常上傳
[root@root~]# date -R Thu, 02 Apr 2015 11:36:44 +08002、第二天問題重現(xiàn)
第二天到公司發(fā)現(xiàn)在開發(fā)中的APP 接口參數(shù)簽名錯(cuò)誤,結(jié)果發(fā)現(xiàn)又是時(shí)間不對(duì),上服務(wù)器一看,時(shí)間不正確了,明明昨天已經(jīng)調(diào)回到東八區(qū)正確的時(shí)間了?
團(tuán)隊(duì)其他人處理這個(gè)問題,沒搞定,于是自己接著解決,上服務(wù)器一看:
[root@root~]# date -R date: multiple output formats specifieddate -R 以及 date 下面所有帶參數(shù)命令失效,無語了
[root@root~]# clock Thu 02 Apr 2015 11:47:33 AM UTC -0.395946 secondsUTC 如果使用UTC,那么也應(yīng)該倒退8個(gè)小時(shí)才行(因?yàn)楸本r(shí)間 UTC +8),決定直接調(diào)整為 CTS(China Standard Time) 時(shí)間
網(wǎng)上搜索需要修改 /ect/localtime 文件
[root@root/]# cd /etc/ [root@root/]# ll lrwxrwxrwx 1 root root 34 Mar 31 15:09 localtime -> /usr/share/zoneinfo/Asia/Shanghai發(fā)現(xiàn)該文件軟連接到了 /usr/share/zoneinfo/Asia/Shanghai 現(xiàn)在這個(gè)指向?qū)嶋H上是昨天通過 tzselect 命令選擇的
但是時(shí)間還是不對(duì),我們查看 /usr/share/zoneinfo/Asia/Shanghai 文件
仔細(xì)比對(duì)另一臺(tái)時(shí)間正確的服務(wù)器發(fā)現(xiàn)上述文件中末尾的 UTC-8 時(shí)間上應(yīng)該是CTS-8
最后詢問后得知是同事修改了該文件,果斷把該文件末尾修改為 CTS-8
最關(guān)建的一步來了
我們一般使用“date -s”命令來修改系統(tǒng)時(shí)間。
比如將系統(tǒng)時(shí)間設(shè)定成2011年07月15日的命令如下。#date -s 07/15/2011
將系統(tǒng)時(shí)間設(shè)定成下午3點(diǎn)12分0秒的命令如下。#date -s 15:12:00
注意,這里說的是系統(tǒng)時(shí)間,是linux由操作系統(tǒng)維護(hù)的。
至此,時(shí)間終于正確了,下一次Linux cmos寫入也不會(huì)將時(shí)間改錯(cuò)了,實(shí)際上前一天做的工作就差這最后一步。
3、date -R 以及其他帶參數(shù)命令錯(cuò)誤的解決
搞了半天不知道什么原因造成的,上來提問
大神建議:
看看是不是環(huán)境變量被污染了,用“which date”看看你啟動(dòng)的這個(gè)date命令是不是built-in的date命令,再用"whereis
date"看看哪些目錄都有"date"這個(gè)命令,默認(rèn)是/bin/date的
于是上服務(wù)器一看:
[root@rootetc]# which date alias date +"%Y%M%D" /usr/bin/date一問同事他給系統(tǒng)默認(rèn) date 起了別名
[root@root etc]# date -R 實(shí)際上 [root@root etc]# date +"%Y%M%D" -R所以錯(cuò)了:
查看alias 的所有條目
使用 unalias 命令將 date 的別名取消
[root@root etc]# unalias date [root@root etc]# date -R Thu, 02 Apr 2015 13:02:00 +0800現(xiàn)在 date 命令已經(jīng)可以正常使用
總結(jié)
以上是生活随笔為你收集整理的Linux 下面的时区与时间错误修复过程记载(血泪篇)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NoSuchMethodError和No
- 下一篇: Hadoop计算中的Shuffle过程