NTP系統時間同步-操作記錄

散盡浮華發表於2016-10-10

 

在初始化一臺linux伺服器後,發現這臺伺服器的時間不對
[root@dev ~]# date
2016年 10月 11日 星期二 07:04:34 CST

Linux時鐘分為系統時鐘 (System Clock)和硬體(Real Time Clock,簡稱RTC)時鐘。系統時鐘是指當前Linux Kernel中的時鐘,而硬體時鐘則是主機板上由電池供電的時鐘,這個硬體時鐘可以在BIOS中進行設定。當Linux啟動時,硬體時鐘會去讀取系統時鐘的設定,然後系統時鐘就會獨立於硬體運作。
Linux中的所有命令(包括函式)都是採用的系統時鐘設定。在Linux中,用於時鐘檢視和設定的命令主要有date、hwclock和 clock。其中,clock和hwclock用法相近,只用一個就行,只不過clock命令除了支援x86硬體體系外,還支援Alpha硬體體系。

------------------------------------------------------------------------------------------------------
linux系統時區由EDT改為CST

EDT:指美國東部夏令時間,波士頓、紐約市、華盛頓哥倫比亞特區,都在這個時區內,跟北京時間有12小時的時差,晚12小時。
CST:可以指下面兩種:
1)美國中部標準時間(西六區,-6:00),中國是東八區(+8:00),北京時間比美國中部標準時間早14個小時。3:45 PM CST 是北京時間凌晨1:45。
2)中澳大利亞標準時間(+10:30),中國是東八區(+8:00),北京時間比中澳大利亞標準時間晚2個半小時。3:45 PM CST 是北京時間下午上午5:45。
[root@db1 ~]# date
Wed Oct 25 04:23:19 EDT 2017

修改操作:
[root@db1 ~]# mv /etc/localtime /etc/localtime.bak
[root@db1 ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@db1 ~]# date
Wed Oct 25 16:25:39 CST 2017
------------------------------------------------------------------------------------------------------

同步系統時間
(1)安裝ntpdate
[root@dev ~]# yum install ntpdate

(2)接著進行線上同步,選擇上海交大的NTP伺服器進行同步;確保網路通暢,DNS正常解析;或者使用ntpdate cn.pool.ntp.org
[root@dev ~]# ntpdate ntp.sjtu.edu.cn
10 Oct 23:02:23 ntpdate[21945]: step time server 202.120.2.100 offset -28975.130708 sec

再次檢視時間,發現時間是正確的了
[root@dev ~]# date   //正常顯示時區應該是CST(即中國標準時間);EST是美國東部標準時間;UTC是協調世界時間/世界標準時間;GMT是格林尼治標準時間
2016年 10月 10日 星期一 23:02:35 CST
[root@dev ~]# date --set "10/10/16 23:45:52"          //--set引數等於-s.  (月/日/年 時:分:秒)
2016年 10月 10日 星期一 23:45:52 CST

(3)結合crontab制定定時同步系統時間(比如每一小時執行一次同步)
[root@dev ~]# crontab -e
10 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn > /dev/null 2>&1

設定時區
方式1:刪除本地時間,並設定時區為上海
[root@dev ~]# rm -rf /etc/localtime
[root@dev ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
修改/etc/sysconfig/clock檔案,修改為:
[root@dev ~]# cat /etc/sysconfig/clock
# The time zone of the system is defined by the contents of /etc/localtime.
# This file is only for evaluation by system-config-date, do not rely on its
# contents elsewhere.
ZONE="Asia/Shanghai"
UTC=false              //這一行可加可不加
ARC=false             //這一行可加可不加

方式2:通過tzselect調整
[root@dev ~]# 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) Beijing Time
2) Xinjiang Time
#? 輸入1,北京時間

The following information has been given:

    China
    Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Local time is now:  Sun Dec 18 21:34:19 CST 2016.
Universal Time is now:  Sun Dec 18 13:34:19 UTC 2016.
Is the above information OK?
1) Yes
2) 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

設定硬體時間
(1)檢視硬體時間
[root@dev ~]# hwclock --show
Sun 18 Dec 2016 09:38:44 PM CST  -0.902813 seconds
[root@dev ~]# clock --show
Sun 18 Dec 2016 09:38:51 PM CST  -0.236699 seconds
(2)修改硬體時間 
[root@dev ~]# hwclock –set –date=”07/07/06 10:19″              (月/日/年 時:分:秒)
[root@dev ~]# clock –set –date=”07/07/06 10:19″                  (月/日/年 時:分:秒)

(3)硬體時間和系統時間的同步
按照前面的說法,重新啟動系統,硬體時間會讀取系統時間,實現同步;
但是在不重新啟動的時候,需要用hwclock或clock命令實現同步:
硬體時鐘與系統時鐘同步
[root@dev ~]# hwclock --hctosys           //hc代表硬體時間,sys代表系統時間
或者
[root@dev ~]# clock --hctosys
系統時鐘和硬體時鐘同步
[root@dev ~]# hwclock --systohc
或者
[root@dev ~]# clock --systohc

同步BIOS時鐘,強制把系統時間寫入CMOS,命令如下:
[root@dev ~]# clock -w

相關文章