手动校正 Linux 系统时间并把系统时间写入硬件,系统启动时自动校正时间并把时间写入硬件。
下面以 CentOS 6.5 (32位)系统为例,设置系统时间:手动校正 Linux 系统时间并把时间写入硬件,系统启动时自动校正时间并把时间写入硬件。
先手动校正系统时间,如下。
# ntpdate 0.cn.pool.ntp.org
13 Nov 18:27:41 ntpdate[7167]: step time server 85.199.214.101 offset 340324836.877469 sec
# date
Mon Nov 13 18:27:47 CST 2017
查看硬件时钟:
# hwclock --show
Wed 31 Jan 2007 08:19:51 PM CST -0.103558 seconds
然后把系统时间写入到硬件:
# hwclock -w
# hwclock --show
Mon 13 Nov 2017 07:04:03 PM CST -0.478211 seconds
下面加上系统启动时自动校正时间并写时间到硬件的功能,只要在 /etc/rc.local 加入 ntpdate 和 hwclock 命令即可,但要写命令的全路径。
1. 查看 ntpdate 和 hwclock 命令的全路径
# whereis ntpdate
ntpdate: /usr/sbin/ntpdate /usr/share/man/man8/ntpdate.8.gz
# whereis hwclock
hwclock: /sbin/hwclock /usr/sbin/hwclock /usr/share/man/man8/hwclock.8.gz
2. 在 /etc/rc.local 文件末尾加入命令
在 /etc/rc.local 文件末尾加入 ntpdate 和 hwclock 命令(全路径):
/usr/sbin/ntpdate 0.cn.pool.ntp.org
/sbin/hwclock -w
文件全内容如下:
# more /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/sbin/ntpdate 0.cn.pool.ntp.org
/sbin/hwclock -w
3. 验证重启系统自动校正时间的功能
重启系统前先设置错误的系统时间,注意,把时间往后设置,不要往前设置,否则系统重启失败。
重启系统后查看时间:
自动校正时间成功。
验证自动校正时间成功了,但再重启系统时可能会出现如下启动失败错误:
解决办法:
(1) 按提示输入 root 密码进入系统
(2) 如下执行fsck命令
# fsck -A -y
然后再重启系统即可。
完毕!
————————————————
版权声明:本文为CSDN博主「iw1210」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/iw1210/article/details/78523568/
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。