Home  >  Article  >  Operation and Maintenance  >  What should I do if the time is wrong after centos restarts?

What should I do if the time is wrong after centos restarts?

藏色散人
藏色散人Original
2022-01-28 11:23:537241browse

Solution to the incorrect time after centos restart: 1. Execute "ln -sf /usr/share/zoneinfor/Asia/Shanghai /etc/localtime" to change the time zone; 2. Just restart.

What should I do if the time is wrong after centos restarts?

The operating environment of this article: centOS6.3 system, DELL G3 computer

What should I do if the time is wrong after centos restarts?

Solve the problem that the modified time of the Linux centos version is invalid after restarting

After installing centos, I found that the time did not match the local time, so I searched online There are many methods, but none of them work. After restarting, it returns to the original time. Very puzzled.

Finally, I added this command with the mentality of giving it a try: ln -sf /usr/share/zoneinfor/Asia/Shanghai /etc/localtime After restarting, it was modified and I felt relieved. tone.

The detailed instructions are listed below, I hope it will be helpful to you.

Linux time is divided into two types, hardware time and system time:

1: Display system time: #date
Modify system time: date -s '2012-08-02 12:00' #Set the system time to local time

2: Display hardware time: #hwclock --show
Set hardware time: #hwclock --set -- date '08/02/2012 12:00:00'

3: This is the hardware time synchronization system time: hwclock --hctosys

4: This command forces the system time to be written to CMOS :clock -w

If you only need the above 4 steps, and the modifications will take effect after restarting, then congratulations, you don’t need to look at the next step. If it still doesn’t take effect, then you must use it. This is the critical step 5.

5: ln -sf /usr/share/zoneinfor/Asia/Shanghai /etc/localtime #Change time zone

In Linux, there are two types of clocks: hardware clock and system clock. The hardware clock refers to the clock device on the motherboard, which is the clock that can usually be set in the BIOS screen. The system clock refers to the clock in the kernel. All Linux related commands and functions read the system clock settings. Because there are two different clocks, there will be differences between them. When Linux starts, the system clock will read the hardware clock settings, and then the system clock will operate independently.

After using the date command to set the system time, the hardware clock will not be modified. Therefore, after the system is restarted, the system time will still read the hardware time. This is why the date setting fails.

Therefore, after setting the system time, you need to synchronize the system time to the hardware clock.

clock/hwclock:

Display and set the hardware clock (query and set the hardware clock (RTC)), the two commands are the same. RTC=Real Time Clock, which is the hardware clock.

Command parameters:

-r, --show        读取并打印硬件时钟(read hardware clock and print result)
-s, --hctosys     将硬件时钟同步到系统时钟(set the system time from the hardware clock)
-w, --systohc     将系统时钟同步到硬件时钟(set the hardware clock to the current system time)

Command example:

1. View the hardware clock

2014年03月27日 星期四 11时03分50秒  -0.328520 seconds
[root@localhost ~]# hwclock -r
2014年03月27日 星期四 11时03分53秒  -0.797264 seconds
[root@localhost ~]# hwclock --show
2014年03月27日 星期四 11时04分01秒  -0.672267 seconds

2. clock is the same as hwclock

[root@localhost ~]# type -a hwclock
hwclock is /sbin/hwclock
hwclock is /usr/sbin/hwclock
[root@localhost ~]# ll /sbin/hwclock /usr/sbin/hwclock 
-rwxr-xr-x. 1 root root 46940 11月 22 23:27 /sbin/hwclock
lrwxrwxrwx. 1 root root    18 3月  26 19:27 /usr/sbin/hwclock -> ../../sbin/hwclock
[root@localhost ~]# type -a clock
clock is /sbin/clock
[root@localhost ~]# ll /sbin/clock 
lrwxrwxrwx. 1 root root 7 3月  26 19:27 /sbin/clock -> hwclock

3. Synchronize the system time to the hardware clock

[root@localhost ~]# hwclock
2014年03月27日 星期四 18时50分51秒  -0.312960 seconds
[root@localhost ~]# date
2014年 03月 27日 星期四 10:50:55 CST
[root@localhost ~]# hwclock -w
[root@localhost ~]# hwclock
2014年03月27日 星期四 10时51分15秒  -0.875387 seconds

Synchronize the network time to the system time

[root@localhost ~]# ntpdate 210.72.145.44
27 Mar 11:11:24 ntpdate[4766]: adjust time server 210.72.145.44 offset 0.011401 sec

210.72.145.44 (the official time synchronization server IP domain name of Xi’an Timing Service Center in China)

After synchronizing the network time to the system time, you can use hwclock -w to synchronize the system time to the hardware time.

Modify time zone

Some systems may still fail after restarting after performing the above two steps, so the time zone needs to be modified.

[root@localhost ~]# tzselect #按照提示进行选择时区
[root@localhost ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Modify the system time configuration file

[root@localhost ~]# /etc/sysconfig/clock

#打开clock文件,将相关内容改成以下内容
UTC=false 
ARC=false
ZONE="Asia/Shanghai"

Restart the system

[root@localhost ~]# reboot

I am using centOS6.3. When restarting the virtual machine, the error message is displayed as follows

/dev/mapper/root: Superblock last mount time (Mon Mar  5 17:40:11 2012,  
         now = Sat Mar  5 18:46:53 2011) is in the future.  
/dev/mapper/root: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.  
         (i.e., without -a or -p options)  
                                                                                                 [FAILED]  
*** An error occurred during the file system check.  
*** Dropping you to a shell; the system will reboot  
*** when you leave the shell.  
Given root password for maintenance  
(or type Control-D to continue):

Later I found a solution:

Enter the password directly at "(or type Control-D to continue):", enter the repair mode, and run the repair in the form of fsck dbf945c5710b2c57c41dd707d30b7551, For yours, just run fsck /dev/mapper/root, and then reboot. After restarting, you may be prompted with the same error on other disks. Run it again in the form of fsck dbf945c5710b2c57c41dd707d30b7551, then reboot, and test it on the virtual machine to start.

Recommended: "centos usage tutorial"

The above is the detailed content of What should I do if the time is wrong after centos restarts?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn