linux系統時間有兩個,一個是硬件時間,即BIOS時間,就是我們進行CMOS設置時看到的時間,另一個是系統時間,是linux系統Kernel時間。當Linux啓動時,系統Kernel會去讀取硬件時鐘的設置,然後系統時鐘就會獨立於硬件運作。有時我們會發現系統時鐘和硬件時鐘不一致,因此需要執行時間同步。
時間設置
一、date 查看/設置系統時間
日期時間設置:
date -s “2016-4-18 15:14:23”
date -s 2012-08-02
date -s 10:08:00
查看當前時區:
# date +"%Z %z"
CST +0800
二、hwclock/clock 查看/設置硬件時間
命令參數:
-r, --show 讀取並打印硬件時鐘(read hardware clock and print result )
-s, --hctosys 將硬件時鐘同步到系統時鐘(set the system time from the hardware clock )
-w, --systohc 將系統時鐘同步到硬件時鐘(set the hardware clock to the current system time )
--adjust hwclock每次更改硬件時鐘時,都會記錄在/etc/adjtime文件中。使用--adjust參數,可使hwclock根據先前的記錄來估算硬件時鐘的偏差,並用來校正目前的硬件時鐘。
--debug 顯示hwclock執行時詳細的信息。
--directisa hwclock預設從/dev/rtc設備來存取硬件時鐘。若無法存取時,可用此參數直接以I/O指令來存取硬件時鐘。
--hctosys 將系統時鐘調整爲與目前的硬件時鐘一致。
--set --date=<日期與時間> 設定硬件時鐘。
--show 顯示硬件時鐘的時間與日期。
--systohc 將硬件時鐘調整爲與目前的系統時鐘一致。
--test 僅測試程序,而不會實際更改硬件時鐘。
--utc 若要使用格林威治時間,請加入此參數,hwclock會執行轉換的工作。
--version 顯示版本信息。
1、查看系統硬件時鐘
[root@linux-node ~]# hwclock --show 或者
[root@linux-node ~]# clock --show
2、設置硬件時間
[root@linux-node ~]# hwclock --set --date="11/03/17 14:55" (月/日/年時:分:秒) 或者
[root@linux-node ~]# clock --set --date="11/03/17 14:55" (月/日/年時:分:秒)
三、同步系統及硬件時鐘
[root@linux-node ~]# hwclock --hctosys 或者
[root@linux-node ~]# clock --hctosys
備註:hc代表硬件時間,sys代表系統時間,以硬件時間爲基準,系統時間找硬件時間同步
[root@linux-node ~]# hwclock --systohc或者
[root@linux-node ~]# clock --systohc
備註:以系統時間爲基準,硬件時間找系統時間同步
時區設置
時區設置用tzselect 命令來實現。但是通過tzselect命令設置TZ這個環境變量來選擇的時區,需要將變量添加到.profile文件中。
一、tzselect命令執行
執行tzselect命令 --> 選擇Asia(5) --> 選擇China(9) --> 選擇Beijing Time(1)-->然後輸入Yes(1)。
[root@test01 ~]# 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: Thu Mar 14 15:43:25 CST 2019.
Universal Time is now: Thu Mar 14 07:43:25 UTC 2019.
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
執行完tzselect命令選擇時區後,時區並沒有更改,只是在命令最後提示你可以執行 TZ='Asia/Shanghai'; export TZ 並將這行命令添加到.profile中,然後退出並重新登錄。
[root@test01 ~]# TZ='Asia/Shanghai'; export TZ
[root@test01 ~]# date
Thu Mar 14 15:45:06 CST 2019
二、修改配置文件來修改時區
[root@test01 ~]# echo "ZONE=Asia/Shanghai" >> /etc/sysconfig/clock
[root@test01 ~]# cat /etc/sysconfig/clock
ZONE=Asia/Shanghai
[root@test01 ~]# rm -f /etc/localtime
#鏈接到上海時區文件
[root@test01 ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
執行完上述過程後,重啓機器,即可看到時區已經更改。
備註:
在centos7中設置時區的命令可以通過 timedatectl 命令來實現
[root@linux-node ~]# timedatectl set-timezone Asia/Shanghai