Ubuntu clock recovery

1 background

Yesterday, the power of Ubuntu motherboard was turned off, resulting in a clock error when compiling after restart:

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- -DXN_NEON -fPIC -fvisibility=hidden -Werror -o Bin/Intermediate/Arm64-Release/3D/main.o main.cpp
cp -R /home/work/OpenNI-Linux-Arm64-* 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: Warning: clock error detected. Your creation may be incomplete.

At that time, the time in the system became 2020, but the time zone difference between UTC and CST was correct, that is, the difference was 8 hours.

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 clock related knowledge

Linux clock is divided into system clock and hardware clock. The system clock refers to the clock in the current Linux Kernel. The hardware clock is the clock powered by battery on the motherboard. This hardware clock can be set in BIOS. When Linux starts, the hardware clock will read the settings of the system clock, and then the system clock will operate independently of the hardware.

All commands (including functions) in Linux are set by the system clock. In Linux, the commands for clock viewing and setting mainly include date, clock and hwlock. The usage of clock and hwlock is similar, and only one is needed, but the clock command supports not only x86 hardware system, but also Alpha hardware system.


3 modification time

3.1 setting the time zone

My own time zone setting is OK. This is just for demonstration.

3.1.1 viewing time zone

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 setting the time zone

Using the tzselect command


Pop up selector

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.

Enter 4 and press enter to prompt:

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

Similarly, Enter 9 and press Enter to prompt:

Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time

Press 1 to prompt:

The following information has been given:

	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

Enter 1 and press enter to prompt:

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:

Tips here can be found in the home directory In the profile file, manually add TZ, the environment variable is' Asia/Shanghai ', and then log out and login will take effect.

In fact, the above interactive operation executes the script / usr/bin/tzselect.

3.1.3 set time zone file

Execute the command to link the time zone file with the local time setting.

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



3.2. Modify system time

Use the ntpdate tool to synchronize network time.

3.1.1 install ntpdate

sudo apt-get install ntpdate

3.1.2 execute command

Synchronize the time. Pay attention to the address of ntp organization in China

sudo ntpdate cn.pool.ntp.org


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

It can be seen that the difference between the local time and the ntp server time is 8250774 seconds, nearly 95.5 days. This is also in line with local time.

View the system time at this time


Wed Feb 24 11:37:15 CST 2021

Already correct.

3.1.3 synchronize system time to hardware

sudo hwclock --systohc // systohc means to synchronize the system time to the hardware. If hctosys means to synchronize the hardware time to the system time

View the hardware time at this time

sudo hwclock

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

It's synchronized.

Tags: Linux Ubuntu NTP

Posted by cityguru on Sat, 16 Apr 2022 09:30:02 +0930