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

 

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

Linux時鐘分爲系統時鐘 (System Clock)和硬件(Real Time Clock,簡稱RTC)時鐘。系統時鐘是指當前Linux Kernel中的時鐘,而硬件時鐘則是主板上由電池供電的時鐘,這個硬件時鐘能夠在BIOS中進行設置。當Linux啓動時,硬件時鐘會去讀取系統時鐘的設置,而後系統時鐘就會獨立於硬件運做。
Linux中的全部命令(包括函數)都是採用的系統時鐘設置。在Linux中,用於時鐘查看和設置的命令主要有date、hwclock和 clock。其中,clock和hwclock用法相近,只用一個就行,只不過clock命令除了支持x86硬件體系外,還支持Alpha硬件體系。shell

------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------app

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

(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 CSTthis

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

設置時區
方式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/Shanghaispa

設置硬件時間
(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

相關文章
相關標籤/搜索