Linux 下面的时区与时间错误修复过程记载(血泪篇)
1、問題產生
昨晚后臺的圖片服務掛了,折騰之后發現是服務器的時間不對,于是看了一下Linux下面關于時區與時間相關的命令
[root@root:~] $ date -R Tue, 31 Mar 2015 13:58:25 +0400 [root@root:~] $北京時間為 +8000 時區,所以上述的時區設置不對
于是使用 tzselect 命令設置時區
首先選擇洲
[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選擇國家
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
系統給出提示
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我們對當前用戶改變時區,所以需要修改 /ect/profile 文件 ,并用source 命令使他生效
[root@root:~] $ vim /etc/profile 在 profile 文件中追加 TZ='Asia/Shanghai'; export TZ [root@root:~] $ source /etc/profile做了上述操作之后服務器的時間正確了,圖片服務也沒有出現因為時間不對簽名校驗失敗的情況,圖片正常上傳
[root@root~]# date -R Thu, 02 Apr 2015 11:36:44 +08002、第二天問題重現
第二天到公司發現在開發中的APP 接口參數簽名錯誤,結果發現又是時間不對,上服務器一看,時間不正確了,明明昨天已經調回到東八區正確的時間了?
團隊其他人處理這個問題,沒搞定,于是自己接著解決,上服務器一看:
[root@root~]# date -R date: multiple output formats specifieddate -R 以及 date 下面所有帶參數命令失效,無語了
[root@root~]# clock Thu 02 Apr 2015 11:47:33 AM UTC -0.395946 secondsUTC 如果使用UTC,那么也應該倒退8個小時才行(因為北京時間 UTC +8),決定直接調整為 CTS(China Standard Time) 時間
網上搜索需要修改 /ect/localtime 文件
[root@root/]# cd /etc/ [root@root/]# ll lrwxrwxrwx 1 root root 34 Mar 31 15:09 localtime -> /usr/share/zoneinfo/Asia/Shanghai發現該文件軟連接到了 /usr/share/zoneinfo/Asia/Shanghai 現在這個指向實際上是昨天通過 tzselect 命令選擇的
但是時間還是不對,我們查看 /usr/share/zoneinfo/Asia/Shanghai 文件
仔細比對另一臺時間正確的服務器發現上述文件中末尾的 UTC-8 時間上應該是CTS-8
最后詢問后得知是同事修改了該文件,果斷把該文件末尾修改為 CTS-8
最關建的一步來了
我們一般使用“date -s”命令來修改系統時間。
比如將系統時間設定成2011年07月15日的命令如下。#date -s 07/15/2011
將系統時間設定成下午3點12分0秒的命令如下。#date -s 15:12:00
注意,這里說的是系統時間,是linux由操作系統維護的。
至此,時間終于正確了,下一次Linux cmos寫入也不會將時間改錯了,實際上前一天做的工作就差這最后一步。
3、date -R 以及其他帶參數命令錯誤的解決
搞了半天不知道什么原因造成的,上來提問
大神建議:
看看是不是環境變量被污染了,用“which date”看看你啟動的這個date命令是不是built-in的date命令,再用"whereis
date"看看哪些目錄都有"date"這個命令,默認是/bin/date的
于是上服務器一看:
[root@rootetc]# which date alias date +"%Y%M%D" /usr/bin/date一問同事他給系統默認 date 起了別名
[root@root etc]# date -R 實際上 [root@root etc]# date +"%Y%M%D" -R所以錯了:
查看alias 的所有條目
使用 unalias 命令將 date 的別名取消
[root@root etc]# unalias date [root@root etc]# date -R Thu, 02 Apr 2015 13:02:00 +0800現在 date 命令已經可以正常使用
總結
以上是生活随笔為你收集整理的Linux 下面的时区与时间错误修复过程记载(血泪篇)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NoSuchMethodError和No
- 下一篇: Linux运维 第三阶段 (二) DHC