Linux 下面的時區與時間錯誤修復過程記載(血淚篇)

一、問題產生

昨晚後臺的圖片服務掛了,折騰以後發現是服務器的時間不對,因而看了一下Linux下面關於時區與時間相關的命令linux

[root@root:~] $ date -R
Tue, 31 Mar 2015 13:58:25 +0400
[root@root:~] $

北京時間爲 +8000 時區,因此上述的時區設置不對
因而使用 tzselect 命令設置時區shell

首先選擇洲vim

[root@root:~] $ 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

選擇國家segmentfault

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

肯定app


The following information has been given: China east 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

系統給出提示ide

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

咱們對當前用戶改變時區,因此須要修改 /ect/profile 文件 ,並用source 命令使他生效ui

[root@root:~] $ vim /etc/profile
在 profile 文件中追加 TZ='Asia/Shanghai'; export TZ
[root@root:~] $ source /etc/profile

作了上述操做以後服務器的時間正確了,圖片服務也沒有出現由於時間不對簽名校驗失敗的狀況,圖片正常上傳this

[root@root~]# date -R
Thu, 02 Apr 2015 11:36:44 +0800

二、次日問題重現

次日到公司發如今開發中的APP 接口參數簽名錯誤,結果發現又是時間不對,上服務器一看,時間不正確了,明明昨天已經調回到東八區正確的時間了?操作系統

團隊其餘人處理這個問題,沒搞定,因而本身接着解決,上服務器一看:

[root@root~]# date -R
date: multiple output formats specified

date -R 以及 date 下面全部帶參數命令失效,無語了

[root@root~]# clock
Thu 02 Apr 2015 11:47:33 AM UTC -0.395946 seconds

UTC 若是使用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 文件

[root@root Asia]# cd /usr/share/zoneinfo/Asia/
[root@root Asia]# vim Shanghai
TZif2^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^@^@^C^@^@^@^@^@^@^@^Q^@^@^@^C^@^@^@^L°þ<9a><9b>È\^A<80>Èú'pÉÕ^N<80>ÊÛZð^^º6^@^_i^?p ~h<80>!Iap"^J<80>#)Cp$Gg^@%^R_ð&'I^@&òAð(^G+^@(Ò#ð^B^A^B^A^B^A^B^A^B^A^B^A^B^A^B^A^B^@^@qå^@^@^@^@~<90>^A^D^@^@p<80>^@^HLMT^@CDT^@CST^@^@^@^@^@^@^@TZif2^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^@^@^C^@^@^@^@^@^@^@^Q^@^@^@^C^@^@^@^Lÿÿÿÿ°þ<9a><9b>ÿÿÿÿÈ\^A<80>ÿÿÿÿÈú'pÿÿÿÿÉÕ^N<80>ÿÿÿÿÊÛZð^@^@^@^@^^º6^@^@^@^@^@^_i^?p^@^@^@^@ ~h<80>^@^@^@^@!Iap^@^@^@^@"^J<80>^@^@^@^@#)Cp^@^@^@^@$Gg^@^@^@^@^@%^R_ð^@^@^@^@&'I^@^@^@^@^@&òAð^@^@^@^@(^G+^@^@^@^@^@(Ò#ð^B^A^B^A^B^A^B^A^B^A^B^A^B^A^B^A^B^@^@qå^@^@^@^@~<90>^A^D^@^@p<80>^@^HLMT^@CDT^@CST^@^@^@^@^@^@^@
UTC-8

仔細比對另外一臺時間正確的服務器發現上述文件中末尾的 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中讀到系統時間變量中,之後修改時間經過修改系統時間實現。爲了保持系統時間與CMOS時間的一致性,Linux每隔一段時間(大約是11分鐘)會將系統時間寫入CMOS。因爲該同步是每隔一段時間進行的,在咱們執行date-s後,若是立刻重起機器,修改時間就有可能沒有被寫入CMOS,這就是問題的緣由。若是要確保修改生效能夠執行以下命令。  
 ---- #clock -w  
 ---- 這個命令強制把系統時間寫入CMOS。
[root@root ]# clock -w
[root@root ]# clock
Thu 02 Apr 2015 11:47:33 AM CTS-0.395946 seconds

至此,時間終於正確了,下一次Linux cmos寫入也不會將時間改錯了,實際上前一天作的工做就差這最後一步。

三、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 的全部條目

[root@rootetc]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
alias date +"%Y%M%D"

使用 unalias 命令將 date 的別名取消

[root@root etc]# unalias date
[root@root etc]# date -R
Thu, 02 Apr 2015 13:02:00 +0800

如今 date 命令已經能夠正常使用

相關文章
相關標籤/搜索