解决linux的centos版本修改时间重启后无效的问题

时间:2020-05-29
本文章向大家介绍解决linux的centos版本修改时间重启后无效的问题,主要包括解决linux的centos版本修改时间重启后无效的问题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

安装完centos后,发现时间与本地时间不匹配,在网上找了好多的办法,但是一直没有奏效,重启之后,又恢复为原来的时间。很是纳闷。最后抱着试一试的心态加上了这句指令: ln -sf /usr/share/zoneinfor/Asia/Shanghai /etc/localtime  重启之后,竟然修改过来了,长舒了一口气。下面将详细的指令列出来,希望能够对你有所帮助。

      linux的时间分为两种,硬件时间和系统时间两种:

     1: 显示系统时间:#date
    修改系统时间: date -s '2012-08-02 12:00'   #设置系统时间为本地时间

    2:显示硬件时间:#hwclock --show
    设置硬件时间:#hwclock --set --date ‘08/02/2012 12:00:00’

    3:这就是硬件时间同步系统时间:hwclock --hctosys 

    4:这个命令强制把系统时间写入CMOS:clock -w

    如果你只需要上面4步,在重启之后,修改就生效的话,那就恭喜你,不用看下面一步了,如果还不生效的话,那你就必须得用这关键的第5步了。

   5: ln -sf /usr/share/zoneinfor/Asia/Shanghai /etc/localtime     #更改时区

在Linux中有硬件时钟与系统时钟两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中的时钟。所有Linux相关指令与函数都是读取系统时钟的设定。因为存在两种不同的时钟,那么它们之间就会存在差异。当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。

用date命令对系统时间进行设置后,并不会去修改硬件时钟,所以系统重启后,系统时间还算会去读取硬件时间,这就是为何date设置失效到原因。

因此,需要在设置系统时间后,将系统时间同步到硬件时钟。

clock/hwclock:

显示与设定硬件时钟(query and set the hardware clock (RTC)),两个命令相同。RTC=Real Time Clock,也就是硬件时钟。

命令参数:

-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)

命令示例:

1. 查看硬件时钟

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与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. 同步系统时间到硬件时钟

[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

同步网络时间到系统时间

[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 (中国西安授时中心的官方时间同步服务器IP域名)

同步网络时间到系统时间后,就可以用hwclock -w将系统时间同步到硬件时间。

修改时区

可能部分系统在执行以上两步后,在重启后仍然失效,所以需要进行时区修改。

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

修改系统时间配置文件

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

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

重启系统

[root@localhost ~]# reboot



  1. 我使用的是centOS6.3,使用虚拟机重启的时候显示错误信息如下  
  2.   
  3. /dev/mapper/root: Superblock last mount time (Mon Mar  5 17:40:11 2012,  
  4.          now = Sat Mar  5 18:46:53 2011) is in the future.  
  5. /dev/mapper/root: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.  
  6.          (i.e., without -a or -p options)  
  7.                                                                                                  [FAILED]  
  8. *** An error occurred during the file system check.  
  9. *** Dropping you to a shell; the system will reboot  
  10. *** when you leave the shell.  
  11. Given root password for maintenance  
  12. (or type Control-D to continue):  
  13.   
  14.   
  15. 后来找到了解决的办法:  
  16.   
  17. 在“(or type Control-D to continue):”处直接输入密码,进入修复模式,按照fsck <partition name>的形式运行修复,你的就运行fsck  /dev/mapper/root,然后reboot,重启后可能还会提示其他盘出现相同的错误,按照fsck <partition name>的形式再次运行,然后reboot,在虚拟机上测试可以启动了  

原文地址:https://www.cnblogs.com/actively/p/12988713.html