linux 8小时时差 bios hwclock 解决 utc localtime

http://blog.csdn.net/misiter/article/details/7767146

比如“上海”

运行命令:tzselect
然后cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
同步时间
ntpdate cn.pool.ntp.org
cn.pool.ntp.org是位于中国的公共NTP服务器,用来同步你的时间。
禁止系统时间设为UTC
编辑 /etc/default/rcS
# Default settings for the scripts in /etc/rcS.d/
#
# For information about these variables see the rcS(5) manual page.
#
# This file belongs to the "initscripts" package.

TMPTIME=0
SULOGIN=no
DELAYLOGIN=no
UTC=yes //把yes改为no
VERBOSE=no
FSCKFIX=no

什么是UTC?

Local vs. UTC
首先重要的问题是你使用utc还是local time.
UTC(Universal Time Coordinated)=GMT(Greenwich Mean Time)
Local time 是你手表上的时间

传统的POSIX计算机(Solaris,bsd,unix)使用UTC格式
linux可以处理UTC时间和蹩脚的Windows所使用的local time

到底是使用UTC还是local time可以这样来确定:
如果机器上同时安装有Linux和Windows,建议使用local time
如果机器上只安装有Linux,建议使用utc

设置时间:

一、使用命令与互联网时间同步,然后再写入硬件(也就是CMOS)
ntp服务器同步时间
代码:
sudo ntpdate cn.pool.ntp.org
将调整后的时间写入cmos
代码:
sudo hwclock --systohc
二、这一种方法与第一种有异曲同工的效果,只是不用与ntp服务器同步
设置系统当前时间
#date -s '2008-6-8 09:19:00'
#date -s '09:19:00'
#date -s '09:19'
写入硬件

#hwclock -w --localtime














方法2依然可行

今天我测试网页的发邮件功能,RHEL6.1系统已经安装的Postfix,而且也随系统启动了,但就是发不出来邮件,于是就开始查找原因,虽然目前还没有找到邮件不能发送的原因。(现在已经写出POSTFIX的问题解决方法了关于postfix发邮件No route to host和Connection refused错误的解决方法)但却发现了系统的时间不正确。按网上的资料也没有解决我的问题,不知道大家是如何解决的,那今天就把我解决的方法给大家共享出来,供大家参考学习。

先来看看问题是什么样的:

1、系统的BISO时间是完全正确的

2、用hwclock -r查看的系统时间

[plain]  view plain copy
  1. [root@yufei ~]# hwclock -r  
  2. Wed 13 Jul 2011 05:50:55 AM CST  -0.868339 seconds  

而现在的北京时间是7月12号21:50。正好相差8小时。

3、用hwclock -D来调试的结果是

[plain]  view plain copy
  1. [root@yufei ~]# hwclock -D  
  2. hwclock from util-linux-ng 2.17.2  
  3. Using /dev interface to clock.  
  4. Last drift adjustment done at 1310506688 seconds after 1969  
  5. Last calibration done at 1310506688 seconds after 1969  
  6. Hardware clock is on UTC time  
  7. Assuming hardware clock is kept in UTC time.  
  8. Waiting for clock tick...  
  9. ...got clock tick  
  10. Time read from Hardware Clock: 2011/07/12 21:44:52  
  11. Hw clock time : 2011/07/12 21:44:52 = 1310507092 seconds since 1969  
  12. Wed 13 Jul 2011 05:44:52 AM CST  -0.315375 seconds  

发现能正确读取到BIOS的时间,但用date命令还是不正确,依然相差8小时,还是Wed 13 Jul 2011 05:44:52

4、折腾了半天也没有设置正确,希望大家别和我一样啊!!哈哈……

下面是我的解决方法

1、我已经忘记了我的系统在安装的时候有没有开启UTC,这一点,我们先忽略。(因为按网上的关掉UTC设置方法,不能解决问题)

2、因为网上资料都基本上雷同,所以我就看hwclock的帮助,想从中找到解决的办法,结果真让我给找到了。

3、使用 hwclock –help查看帮助内容,这个命令大家一定要注意他的使用方法。这样才能找到原因。下面我就把几个重要的内容摘录出来,给大家做个解释。

[plain]  view plain copy
  1. [root@yufei ~]# hwclock --help  
  2. hwclock - query and set the hardware clock (RTC)  
  3.   
  4. Usage: hwclock [function] [options...]  
  5. 这里需要大家特别注意:先是函数后是参数,只用其中一个是没有用的,这是解决问题的关键  
  6.   
  7. Functions:  
  8.   -r | --show         read hardware clock and print result #读取硬件(BIOS)时间,并显示出来  
  9.   -s | --hctosys      set the system time from the hardware clock #把目前系统时间调整为硬件时间  
  10.   -w | --systohc      set the hardware clock to the current system time #把硬件时间调整为目前系统的时间  
  11.        --systz        set the system time based on the current timezone #把系统时间设置为当时时区的时间  
  12.   
  13. Options:  
  14.   -u | --utc          the hardware clock is kept in UTC #设置硬件为的UTC时间  
  15.        --localtime    the hardware clock is kept in local time #设置硬件为本地时间  

hwclock的使用方法很关键,有了上面的解释后,综合一下,就得到了解决问题的方法了。

[plain]  view plain copy
  1. [root@yufei ~]# hwclock -s --localtime  

这一句命令解决所有问题。

注意:

1、确保你的时区要设置正确/etc/sysconfig/clock

2、确保你的系统中的BIOS时间正确

关于如何关闭UTC啊,如何设置时区啊,大家可以参考http://www.opsers.org/linux-home/base/8-hour-time-difference-on-the-linux-incorrect-solution.html


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值