1. 程式人生 > >Linux時間設定

Linux時間設定

一、關於時間

有時候我們會發現我們調整了Linux的時間,但是重啟之後,發現時間又對不上了,瞬間有種很蛋疼的感覺有沒有?因為在Linux系統中有兩個時間,一個是硬體時間,還有一個是系統時間。我們通常所表達的都是指系統時間,一般情況下我們修改的也是系統時間。而Linux系統在啟動時會去讀取硬體時間,所以你會發現你重啟後,你係統的時間又不對的“感覺”。

 

二、設定時間

檢視系統時間:

[[email protected] ~]# date
2018年 10月 21日 星期日 17:18:26 CST 

 

格式化系統時間

[[email protected]
etc]# date "+%Y-%m-%d %H:%M:%S" 2018-10-21 18:30:50

 

檢視硬體時間

[[email protected] ~]# hwclock 
2018年10月21日 星期日 17時21分42秒  -0.405552

 

設定當前系統時間

[[email protected] ~]# date -s "2018-10-21 17:25"
2018年 10月 21日 星期日 17:25:00 CST
[[email protected] ~]# date
2018年 10月 21日 星期日 17
:25:02 CST

 

將系統時間同步到硬體時鐘

[[email protected] ~]# hwclock --systohc
[[email protected] ~]# date && hwclock 
2018年 10月 21日 星期日 17:30:50 CST
2018年10月21日 星期日 17時30分50秒  -0.037117

 

三、使用網路時間

使用網路時間同步系統時間

[[email protected] ~]# rpm -qa  | grep ntp
# 檢視是否安裝ntp服務

[
[email protected]
~]# yum install ntp # 安裝ntp服務 [[email protected] ~]# ntpdate time.windows.com 21 Oct 17:47:07 ntpdate[3120]: step time server 51.140.65.84 offset -25.155433 sec # 將系統時間設定為當前時間伺服器的時間 [[email protected] ~]# hwclock --systohc # 將當前系統時間同步硬體時間

 

使用ntpd服務自動同步系統時間

[[email protected] etc]# systemctl start ntpd
[[email protected] etc]# ps -ef | grep ntpd
ntp        3274      1  0 18:13 ?        00:00:00 /usr/sbin/ntpd -u ntp:ntp -g
root       3276   2613  0 18:13 pts/0    00:00:00 grep --color=auto ntpd

 

這/etc/ntp.conf這個配置檔案中21行左右設定了3個預設的伺服器,當我們啟動這個這個服務的時候,它就會幫我麼自動同步系統時間了。 

 

四、時間問題排查

主要的排查思路就是檢視系統的時區,如果時區和當前地區吻合。那麼就是硬體時間和系統時間不一致導致的使用上面的方式設定就好了。

檢視當前系統時區

[[email protected] etc]# date -R
Sun, 21 Oct 2018 18:15:53 +0800

 

設定當前系統時區

[[email protected] etc]# 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 Oct 21 18:23:59 CST 2018.
Universal Time is now:    Sun Oct 21 10:23:59 UTC 2018.
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