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

已经同步了。

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