Linux: 同步系统时间和硬件时间

Linux 系统有两个时间,一个是操作系统时间,还有个硬件时间,并且这两个时间不会自动同步,其中系统时间可以通过ntpdate 命令来同步,硬件时钟可以通过 hwclock 命令来同步,下面是简单实验过程:

同步 Linux 系统时间

1
2
3
4
5
6
7
[root@pgb init.d]# date -s " 2012-02-12 12:00:00"  
Sun Feb 12 12:00:00 CST 2012

[root@pgb init.d]# ntpdate asia.pool.ntp.org
12 Feb 14:15:21 ntpdate[3453]: step time server 116.193.83.174 offset 8115.182172 sec
[root@pgb init.d]# date
Sun Feb 12 14:15:22 CST 2012

更改硬件时钟

1
2
3
4
[root@pgb init.d]# hwclock --show  
Fri 20 Feb 2015 06:54:27 PM CST -0.930090 seconds

[root@pgb init.d]# /sbin/hwclock --systohc

显示当前硬件时钟

1
2
[root@pgb init.d]# hwclock --show  
Sun 12 Feb 2012 02:16:25 PM CST -0.953096 seconds

备注: –systohc set the hardware clock to the current system time 将硬件时间和当前系统时间保持同步。

加入 Crontab

每小时同步系统时间和硬件时间,如下:

1
2
###ntpdate  
5 * * * * /usr/sbin/ntpdate asia.pool.ntp.org ;/sbin/hwclock --systohc >> /root/sync_date.log 2>&1

附: 网络时间服务器

微软公司授时主机(美国)
time.windows.com
台警大授时中心(台湾)
asia.pool.ntp.org
中科院授时中心(西安)
210.72.145.44
网通授时中心(北京)
219.158.14.130

Hwclock 命令说明

NAME
hwclock – query and set the hardware clock (RTC)
SYNOPSIS
hwclock -r or hwclock –show
hwclock -w or hwclock –systohc
hwclock -s or hwclock –hctosys
hwclock -a or hwclock –adjust
hwclock -v or hwclock –version
hwclock –set –date=newdate
hwclock –getepoch
hwclock –setepoch –epoch=year
DESCRIPTION
hwclock is a tool for accessing the Hardware Clock. You can display the current time, set the Hardware Clock
to a specified time, set the Hardware Clock to the System Time, and set the System Time from the Hardware
Clock.
You can also run hwclock periodically to insert or remove time from the Hardware Clock to compensate for sys-
tematic drift (where the clock consistently gains or loses time at a certain rate if left to run).

Ntpdate 命令说明

NAME
ntpdate – set the date and time via NTP
Disclaimer: The functionality of this program is now available in the ntpd program. See the -q command line
option in the ntpd – Network Time Protocol (NTP) daemon page. After a suitable period of mourning, the ntpdate
program is to be retired from this distribution

ntpdate sets the local date and time by polling the Network Time Protocol (NTP) server(s) given as the server
arguments to determine the correct time. It must be run as root on the local host. A number of samples are
obtained from each of the servers specified and a subset of the NTP clock filter and selection algorithms are
applied to select the best of these. Note that the accuracy and reliability of ntpdate depends on the number of
servers, the number of polls each time it is run and the interval between runs.

参考资料

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/237822.html

(0)
上一篇 2022年1月29日
下一篇 2022年1月29日

相关推荐

发表回复

登录后才能评论