talk is cheap, show me the code.
Linux的時間分爲System Clock(系統時間)和 Real Time Clock (硬件時間,簡稱RTC)。
-
系統時間
指當前Linux Kernel中的時間
-
硬件時間
主板上有電池供電的時間
查看系統時間 date
[tanpeng@itplh ~]$ date
Fri Mar 27 17:53:52 CST 2020
設置系統時間的命令 date -s "月/日/年 時:分:秒"
[tanpeng@itplh ~]$ sudo date -s "02/27/2020 19:00:00"
Thu Feb 27 19:00:00 CST 2020
[tanpeng@itplh ~]$ date
Thu Feb 27 19:00:08 CST 2020
查看硬件時間的命令 hwclock
[tanpeng@itplh ~]$ sudo hwclock
Fri 27 Mar 2020 05:56:39 PM CST -0.453631 seconds
設置硬件時間的命令 hwclock –set –date = (月/日/年 時:分:秒)
[tanpeng@itplh ~]$ sudo hsclock -s "02/27/2020 19:00:00"
Thu Feb 27 19:00:00 CST 2020
[tanpeng@itplh ~]$ date
Thu Feb 27 19:00:08 CST 2020
上述提到的是手動設置時間到一個時間點,可能與當前網絡的時間有誤差
與時間服務器上的時間同步的方法
-
安裝
ntpdate
工具yum -y install ntp ntpdate
-
設置系統時間與網絡時間同步
ntpdate cn.pool.ntp.org
-
將系統時間寫入硬件時間
hwclock --systohc
example
[tanpeng@cloud02 ~]$ sudo ntpdate cn.pool.ntp.org
27 Mar 18:20:22 ntpdate[17937]: step time server 111.230.189.174 offset 2502162.541489 sec
[tanpeng@cloud02 ~]$ sudo hwclock --systohc
[tanpeng@cloud02 ~]$ date
Fri Mar 27 18:20:47 CST 2020
[tanpeng@cloud02 ~]$ sudo hwclock
Fri 27 Mar 2020 06:20:56 PM CST -0.609821 seconds