Linux系统时间和硬件时间设置

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

4,同步网络时间到系统时间

[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将系统时间同步到硬件时间。

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

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

修改系统时间配置文件

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

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

重启系统

[root@localhost ~]# reboot

文章来源

猜你喜欢

转载自blog.csdn.net/network_dream/article/details/88671805