我使用过的Linux命令之hwclock - 查询和设置硬件时钟

hwclock命令,与clock命令是同一个命令,主要用来查询和设置硬件时钟(query and set the hardware clock (RTC))。RTC=Real Time Clock,也就是硬件时钟。在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。系统时钟则是指kernel中 的时钟。所有Linux相关指令与函数都是读取系统时钟的设定。因为存在两种不同的时钟,那么它们之间就会存在差异。根据不同参数设置,hwclock命令既可以将硬件时钟同步到系统时钟,也可以将系统时钟同步到硬件时钟。

关于系统时钟与硬件时钟的关系的说法,网上分成了两种:

第一种:当Linux启动时,硬件时钟会去读取系统时钟的设置,然后系统时钟就会独立于硬件运作。

第二种:当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。

到底那一种才是正确的呢?执行一下man hwclock可以看到里面有一段:

man hwclock 写道

The System Time is the time that matters. The Hardware Clock’s basic purpose in a Linux system is to keep time
when Linux is not running. You initialize the System Time to the time from the Hardware Clock when Linux
starts up, and then never use the Hardware Clock again. Note that in DOS, for which ISA was designed, the
Hardware Clock is the only real time clock.

这段文字说明,第二种说法是正确的,第一种说法是错误的。但网上大部分文章都是第一种说法,所以说,网络虽好,要注意甄别。

man hwclock 写道

It is important that the System Time not have any discontinuities such as would happen if you used the date(1L)
program to set it while the system is running. You can, however, do whatever you want to the Hardware Clock
while the system is running, and the next time Linux starts up, it will do so with the adjusted time from the
Hardware Clock.

如果使用date命令修改了系统时间,并不会自动去修改硬件时钟,因此,当系统下次重启时,系统时钟还会从硬件时钟去取,date设置的时间就无效了。这大概就是为什么需要hwclock命令的原因吧。

常用参数

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

使用示例

示例一 hwclock命令与clock命令是一个东西

[[email protected] ~]# type -a hwclock 
hwclock is /sbin/hwclock
hwclock is /usr/sbin/hwclock
[[email protected] ~]# ls -l /sbin/hwclock /usr/sbin/hwclock 
-rwxr-xr-x 1 root root 34096 2010-01-07 /sbin/hwclock
lrwxrwxrwx 1 root root    18 08-13 00:14 /usr/sbin/hwclock -> ../../sbin/hwclock

[[email protected] ~]# type -a clock 
clock is /sbin/clock
[[email protected] ~]# ls -l /sbin/clock 
lrwxrwxrwx 1 root root 7 08-13 00:14 /sbin/clock -> hwclock
[[email protected] ~]#

示例二 显示硬件时钟

[[email protected] ~]# hwclock 
2010年11月06日 星期六 21时09分28秒  -0.134840 seconds
[[email protected] ~]# hwclock -r 
2010年11月06日 星期六 21时09分33秒  -0.469123 seconds
[[email protected] ~]# hwclock --show 
2010年11月06日 星期六 21时09分45秒  -0.127548 seconds
[[email protected] ~]#

示例三 修改日期并同步到硬件时钟

[[email protected] ~]# date 
2010年 11月 06日 星期六 21:11:57 CST
[[email protected] ~]# date 11062112     <== 格式 mmddHHMM,即 月日时分 
2010年 11月 06日 星期六 21:12:00 CST
[[email protected] ~]# hwclock -w 
[[email protected] ~]# hwclock 
2010年11月06日 星期六 21时12分13秒  -0.648818 seconds
[[email protected] ~]#

注:要使系统时间准确,最好还是使用ntp方式。

示例四 使用ntpdate命令从网络同步时间,再同步到硬件时钟

ntpdate命令是使用NTP协议来从网络同步时间的命令。NTP=Network Time Protocol 网络时间协议。

[[email protected] ~]# ntpdate 0.rhel.pool.ntp.org 
 6 Nov 21:17:55 ntpdate[4829]: step time server 123.146.124.28 offset -1.025258 sec
[[email protected] ~]# date 
2010年 11月 06日 星期六 21:17:59 CST
[[email protected] ~]# hwclock -w 
[[email protected] ~]#

时间: 2024-08-10 02:09:49

我使用过的Linux命令之hwclock - 查询和设置硬件时钟的相关文章

Linux命令之hostname - 显示或设置主机名

我使用过的Linux命令之hostname - 显示或设置主机名 本文链接:http://codingstandards.iteye.com/blog/804648   (转载请注明出处) 用途说明 hostname命令可以用来显示或者设置主机名(show or set the system's host name).环境变量HOSTNAME也保存了当前的主机名.在使用hostname命令设置主机名后,系统并不会永久保存新的主机名,重新启动机器之后还是原来的主机名.如果需要永久修改主机名,需要同

纯linux命令批量创建用户并设置随机密码且显示用户和密码的脚本

##deleate rm -f /root/user_password.txt /root/user_names.txt /root/user_list.txt ##create stu01-stu10 echo stu{01..10}|xargs -n1 >> /root/user_names.txt ##create users stu01-stu10 and build password cat /root/user_names.txt|sed 's#.*#useradd &;e

十年linux命令总结

十年linux命令总结 本文链接: http://codingstandards.iteye.com/blog/786653 关于命令类型划分 本表中列出了我穷尽了我所有的记忆整理出来的Linux命令,其中关于外部命令.关键字或内建命令的划分,不一定正确.有很多命令,既是内建命令也 可能是外部命令,为啥?Bash为了更高效率的执行命令和脚本,把很多外部命令以内建命令的方式实现了.我们使用type这个命令就可以知道一个命令的类 型了,如下所示: [[email protected] ~]# typ

4.Linux命令与文件系统初步

一.查看命令历史及相关信息        bash查找命令的方式:            外部命令查找:$PATH            [[email protected]_basic ~]# echo $PATH            /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin查找路径是从左往右查找的,            如果说刚要执行的命令是在/root/bin则要一各路径一个路径去查找,

我使用过的Linux命令系列总目录

概述关于命令类型划分本表中列出了我穷尽了我所有的记忆整理出来的Linux命令,其中关于外部命令.关键字或内建命令的划分,不一定正确.有很多命令,既是内建命令也可能是外部命令,为啥?Bash为了更高效率的执行命令和脚本,把很多外部命令以内建命令的方式实现了.我们使用type这个命令就可以知道一个命令的类型了,如下所示:[[email protected] ~]# type -a type type is a shell builtin <== builtin就是指内建命令 [[email prot

Linux命令初识一

学习总结:自行学习如下命令 命令格式,常用选项,示例,说明 date.clock.hwclock.cal ls.cd.pwd.tty.whereis.which stat.echo.shutdown.halt.reboot.poweoff who.w.whoami 1. date 使用者:所有用户 语法:date [参数]... [+格式] date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--

Linux命令初识二

学习总结:自行学习如下命令 命令格式,常用选项,示例,说明 date.clock.hwclock.cal ls.cd.pwd.tty.whereis.which stat.echo.shutdown.halt.reboot.poweoff who.w.whoami 1. date 使用者:所有用户 语法:date [参数]... [+格式] date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--

每天一个Linux命令(64)shutdown命令

    shutdown以一种安全的方式关闭系统.     (1)用法:     用法:  shutdown [参数] [时间]     (2)功能:     功能:  系统关机命令,shutdown指令可以关闭所有程序,并依用户的需要,进行重新开机或关机的动作,所有登陆用户都可以看到关机信息提示. 原理:  shutdown通过通知init进程,要求它改换运行级别来实现.运行级别0用来关闭系统,运行级别6用来重启系统,运行级别1用来使系统进入执行系统管理任务状态,如果没有给出 -h 或 -r

64个命令,每天一个linux命令目录, shutdown,tee,rcp,

每天一个linux命令目录 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每天一个linux命令.学习的主要参考资料为: 1.<鸟哥的linux私房菜> 2.http://codingstandards.iteye.com/blog/786653 3.linux命令五分钟系列 4.其他互联网资料,google,baidu等搜索引擎 11. 每天一个linux命令(11):nl命令 12. 每天一个linux命令(12):more命令 13. 每天一个linux命令(13