Ubuntu時鐘恢復

1 背景

昨天將Ubuntu主板的電源關了,導致重啓後,進行編譯時,提示時鐘錯誤:

make: Warning: File 'main.cpp' has modification time 8221891 s in the future
g++ -MD -MP -MT "./Bin/Intermediate/Arm64-Release/3D/main.d Bin/Intermediate/Arm64-Release/3D/main.o" -c -I/usr/local/include/opencv -I/usr/local/include -O2 -DNDEBUG -I../../include -I/home/work/OpenNI-Linux-Arm64-2.3.0.65/include -DXN_NEON -fPIC -fvisibility=hidden -Werror -o Bin/Intermediate/Arm64-Release/3D/main.o main.cpp
cp -R /home/work/OpenNI-Linux-Arm64-2.3.0.65/Redist/* Bin/Arm64-Release
g++ -o Bin/Arm64-Release/3D ./Bin/Intermediate/Arm64-Release/3D/main.o -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core  -LBin/Arm64-Release -lOpenNI2 -Wl,-rpath ./
make: 警告:檢測到時鐘錯誤。您的創建可能是不完整的。

當時系統中的時間變成2020年了,但是 協調世界時間UTC 和 當地時間中國標準時間CST 的時區差別是正確的,即相差8個小時。

Local time is now:	Fri Nov 20 23:41:25 CST 2020.
Universal Time is now:	Fri Nov 20 15:41:25 UTC 2020.

 

2 時鐘相關知識

Linux時鐘分爲 系統時鐘(System Clock) 和 硬件時鐘 (RTC:Real Time Clock) 。系統時鐘是指當前Linux Kernel中的時鐘,硬件時鐘是主板上由電池供電的時鐘,這個硬件時鐘可以在BIOS中進行設置。當Linux啓動時,硬件時鐘會去讀取系統時鐘的設置,然後系統時鐘就會獨立於硬件運作。

Linux中的所有命令(包括函數)都是採用的系統時鐘設置。在Linux中,用於時鐘查看和設置的命令主要有date,clock 和 hwclock。其中 clock和hwclock用法相近,只用一個就行,只不過clock命令除了支持x86硬件體系外,還支持Alpha硬件體系。

 

3 修改時間

3.1 設置時區

我自己的時區設置是OK的,這裏只是做演示用。

3.1.1 查看時區

timedatectl status

輸出

Local time: Wed 2021-02-24 11:05:12 CST
  Universal time: Wed 2021-02-24 03:05:12 UTC
        RTC time: Wed 2021-02-24 03:05:11
       Time zone: Asia/Shanghai (CST, +0800)
 Network time on: yes
NTP synchronized: no
 RTC in local TZ: no

 或者

date -R

Wed, 24 Feb 2021 14:21:30 +0800

3.1.2 設置時區

使用 tzselect 命令

tzselect

彈出選擇器

Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
 1) Africa
 2) Americas
 3) Antarctica
 4) Asia
 5) Atlantic Ocean
 6) Australia
 7) Europe
 8) Indian Ocean
 9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#?

輸入4,按Enter鍵,提示:

Please select a country whose clocks agree with yours.
 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,按Enter鍵,提示:

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:	Wed Feb 24 11:42:59 CST 2021.
Universal Time is now:	Wed Feb 24 03:42:59 UTC 2021.
Is the above information OK?
1) Yes
2) No
#?

輸入1,按Enter鍵,提示:

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

這裏提示可以在家目錄下的.profile文件中,手動添加 TZ 這個環境變量爲 'Asia/Shanghai',然後登出登錄就會生效。

實際上,上面的交互式操作,執行的就是 /usr/bin/tzselect 這個腳本。

3.1.3 設置時區文件

執行命令,把時區文件和本地時間設置鏈接起來。

sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

 

3.2 修改系統時間

使用 ntpdate 工具來同步網絡時間。

3.2.1 安裝 ntpdate

sudo apt-get install ntpdate

3.2.2 執行命令

同步時間,注意後面傳的中國的ntp機構地址

sudo ntpdate cn.pool.ntp.org

輸出

24 Feb 11:37:03 ntpdate[3272]: step time server 172.18.1.1 offset 8250774.353808 sec

可以看到,本地時間與ntp服務器時間相差 8250774 秒,接近 95.5 天。這也和本地時間對得上。

此時查看系統時間

date

Wed Feb 24 11:37:15 CST 2021

已經正確。

3.2.3 把系統時間同步給硬件

sudo hwclock --systohc // systohc 表示把系統時間同步給硬件,如果是 hctosys 表示把硬件時間同步給系統時間

此時查看硬件時間

sudo hwclock

Wed Feb 24 11:38:21 2021  .201714 seconds

已經同步了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章