Solve the problem of inconsistency between CentOS system time and UTC time---practice

1. Problem description

Recently, I wrote a timer using @Scheduled and corn expressions on the paddling fish blog project. There is no problem running the project on the local windows, but I found that when it was published to the linux service, the running time of the timer was wrong . There is a difference of 8 hours from the timing time I set.

After checking the relevant information, it is found that: the CentOS system time of linux is inconsistent with the UTC time

2. Reason

​ After we installed the Centos Linux operating system, we clicked on the time of the system and found that it was inconsistent with the time we are using now. There is a difference of 8 hours. When we installed the system, the time zone we chose was Shanghai, but the default bios time of CentOS Linux It is utc time (UTC is the English abbreviation of Universal Time Coordinated), which is a second-based time scale stipulated and recommended by the International Radio Consultative Committee and maintained by the International Time Bureau (BIH).

​ UTC is equivalent to the mean solar time on the prime meridian (that is, longitude 0 degrees), which was expressed in Greenwich mean time (GMT) in the past. For example, UTC time is 0:00, and Beijing time is 8:00 am on January 1, 1999. ), so we are separated by 8 hours in time. At this time, the bios time and the system time are of course inconsistent, one indicates utc time, and the other indicates cst (+8 time zone), that is, the time in Shanghai.

View time command:

1.查看系统时间:
date
2.查看utc时间
date -u

image-20211013095940378

Obviously, the difference between the two times is 8 hours, so the above problems arise.

3. Solutions

Execute the following command in the terminal command in CentOS Linux:

3.1 Edit time configuration file
vi /etc/sysconfig/clock 
------------------------------
 ZONE="Asia/Shanghai"操作系统
 UTC=false                          #设置为false,硬件时钟不于utc时间一致同步
 ARC=falsei
3.2 The time zone of linux is set to Shanghai time zone
ln -sf /usr/share/zoneinfo/Asia/Shanghai    /etc/localtime
3.3 Alignment time
ntpdate 47.99.xxx.xxx(你的ip)

If the ntp server is not installed, you just need to execute the following command to install the npt server:

yum install ntp

If you report this question:

image-20211013095248340

solve kill process

使用lsof –i:123
没有lsof命令的话:yum install -y lsof

[root@izbp12w1juq9po2x7vs7nnz ~]# lsof -i:123
COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
ntpd    31393  ntp   16u  IPv4 48258263      0t0  UDP *:ntp 
ntpd    31393  ntp   17u  IPv6 48258264      0t0  UDP *:ntp 
ntpd    31393  ntp   18u  IPv4 48258269      0t0  UDP localhost:ntp 
ntpd    31393  ntp   19u  IPv4 48258270      0t0  UDP izbp12w1juq9po2x7vs7nnz:ntp 
ntpd    31393  ntp   20u  IPv4 48258271      0t0  UDP izbp12w1juq9po2x7vs7nnz:ntp 
[root@izbp12w1juq9po2x7vs7nnz ~]# kill -9 31393

然后再执行命令:ntpdate 47.99.xxx.xxx(你的ip)

If an error is reported:

image-20211013101651247

解决办法:
①在本机上装ntpd服务
②启动ntpd服务
③检查ntp server完成了与自身的同步
④使用ntpdate让其它节点与ntp server进行同步
3.4 Set the hardware time to be consistent with the system time and calibrate
/sbin/hwclock --systohc

Well, our CentOS linux system time and computer hardware time are finally cst time, and they are both in Shanghai time zone, so they are finally normal.

The time is synchronized, but when searching for methods on the Internet, the CST time is replaced by GMT, so don't care if it doesn't affect the use.

GMT time is Greenwich Mean Time. CST time refers to including China. U.S. Brazil, Australia time in four time zones.

image-20211013110801332

Another simple method is to change the time directly, command:

date -s 2021/10/13
date -s 11:04

image-20211013111445519

Done!

Guess you like

Origin blog.csdn.net/SHUKAI618/article/details/120740335