Linux时钟介绍和修改系统时间

简介: 关于UTC/GMT等技术概念,参考:http://blog.csdn.net/cymm_liu/article/details/29234919  Linux时钟简介:参考文章:http://www.cnblogs.com/cy163/archive/2008/05/15/1198683.htmlLinux将时钟分为系统时钟(System Clock) 和 硬件(Real Time Clock,简称RTC)时钟两种。

关于UTC/GMT等技术概念,参考:http://blog.csdn.net/cymm_liu/article/details/29234919

  Linux时钟简介:
参考文章:http://www.cnblogs.com/cy163/archive/2008/05/15/1198683.html
Linux将时钟分为系统时钟(System Clock) 和 硬件(Real Time Clock,简称RTC)时钟两种。系统时间是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的那个主板硬件时钟,这个时钟可以在BIOS的“Standard BIOS Feture”项中进行设置。 既然Linux有两个时钟系统,那么大家所使用的Linux默认使用哪种时钟系统呢?会不回出现两种系统时钟冲突的情况呢?这些疑问和担心不无道理。首 先,Linux并没有默认哪个时钟系统。Linux启动时,系统时钟会去读取硬件时钟的设置,然后系统时钟就会独立于硬件运作。
从Linux启动过程来看,系统时钟和硬件时钟不会发生冲突,但Linux中的所有命令(包括函数)都是采用的系统时钟设置。不仅如此,系统时钟和硬件时 钟还可以采用异步方式,即系统时间和硬件时间可以不同。这样做的好处对于普通用户意义不大,但对于Linux网络管理员却有很大的用处。例如,要将一个很大的网络中(跨越若干时区)的服务器同步,假如位于美国纽约的Linux服务器和北京的Linux服务器,其中一台服务器无须改变硬件时钟 而只需临时设置一个系统时间,如要将北京服务器上的时间设置为纽约时间,两台服务器完成文件的同步后,再与原来的时钟同步一下即可。这样系统和硬件时钟就 提供了更为灵活的操作。
在Linux中,用于时钟查看和设置的命令主要有date、hwclock和clock。其中,clock和hwclock用法相近,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。由于目前绝大多数用户使用x86硬件体系,所以可以视这两个命令为一个命令来学习。
 
在虚拟终端中使用date命令来查看和设置系统时间
查看系统时钟的操作:
# date
设置系统时钟的操作:
# date 091713272003.30
通用的设置格式:
# date 月日时分年.秒
使用hwclockclock命令查看和设置硬件时钟
查看硬件时钟的操作:
# hwclock --show 或# clock –show
2003年09月17日 星期三 13时24分11秒 -0.482735 seconds
设置硬件时钟的操作:
# hwclock --set --date="09/17/2003 13:26:00"
或者# clock --set --date="09/17/2003 13:26:00"
通用的设置格式:hwclock/clock --set --date=“月/日/年 时:分:秒”。
同步系统时钟和硬件时钟
硬件时钟与系统时钟同步:
# hwclock --hctosys
或者
# clock --hctosys
上面命令中,--hctosys表示Hardware Clock to SYStem clock。
系统时钟和硬件时钟同步:
 hwclock --systohc 或者
 clock –systohc

Linux Time Zone Configuration guide:
Background - The Earth is divided into time zones that are 15 degrees of longitude each, for this corresponds to the amount of angular distance the Sun appears to travel in 1 hour. 0 degrees longitude runs through the Royal Observatory in Greenwich, England. This is the origin of Greenwich Mean Time, or GMT. For all practical purposes, GMT and UTC are the same. To complicate matters, some countries observe Daylight Savings Time (DST), while others do not. Even within some countries, some states or districts do not observe DST while the rest of the country does! DST can also begin and end on different days in different countries! What a mess...
There are several files and directories that are used for time zones, and several tools:
l         /etc/sysconfig/clock- this is a short text file that defines the timezone, whether or not the hardware clock is using UTC, and an ARC option that is only relevant to DEC systems.
      eg/ect/sysconfig/clock file content on jstest3:
       ZONE="Asia/Shanghai"
       UTC=true
       ARC=false
    
l         /etc/localtime - this is a symbolic link to the appropriate time zone file in /usr/share/zoneinfo
l         /usr/share/zoneinfo - this directory contains the time zone files that were compiled by zic(The time zone compiler. Zic creates the time conversion information files.)These are binary files and cannot be viewed with a text viewer. The files contain information such as rules about DST. They allow the kernel to convert UTC UNIX time into appropriate local dates and times.
l         /etc/rc.d/rc.sysinit- This script runs once, at boot time. A section of this script sets the system time from the hardware clock and applies the local time zone information.
l         /etc/init.d/halt- This script runs during system shutdown. A section of this script synchronizes the hardware clock from the system clock.
l         /etc/adjtime - This file is used by the adjtimex function, which can smoothly adjust system time while the system runs. settimeofday is a related function.
l         redhat-config-date or dateconfig - These commands start the Red Hat date/time/time zone configuration GUI.
l         zdump - This utility prints the current time and date in the specified time zone. Example:
zdump Japan
Japan Sat Mar 29 00:47:57 2003 JST
zdump Iceland
Iceland Fri Mar 28 15:48:02 2003 GMT

 硬件时间 /sbin/hwclock


   直接调用 /sbin/hwclock 显示的时间就是 BIOS 中的时间吗?未必!这要看 /etc/sysconfig/clock 中是否启用了UTC,如果启用了UTC(UTC=true),显示的其实是经过时区换算的时间而不是BIOS中真正的时间,如果加上 --localtime 选项,则得到的总是 BIOS 中实际的时间.

[12-01 19:07> ~]# hwclock
2009年12月07日 星期一 14时28分43秒 -0.611463 seconds
[12-01 19:07> ~]# hwclock --utc
2009年12月07日 星期一 14时28分46秒 -0.594189 seconds
[12-01 19:07> ~]# hwclock --localtime
2009年12月07日 星期一 06时28分50秒 -0.063875 seconds

/etc/localtime


   这个文件用来设置系统的时区,将 /usr/share/zoneinfo/ 中相应文件拷贝到/etc下并重命名为 localtime 即可修改时区设置,而且这种修改对 date 命令是及时生效的。不论是 date 还是 hwclock 都会用到这个文件,会根据这个文件的时区设置来进行UTC和本地之间之间的换算。

 /etc/sysconfig/clock

   这个文件只对 hwclock 有效,而且似乎是只在系统启动和关闭的时候才有用,比如修改了其中的 UTC=true 到 UTC=false 的前后,执行 hwclock (--utc, 或 --localtime) 都没有变化,要重启系统后才生效。注:如果设置 UTC=false 并重启系统后,执行一些命令结果如下:

date 2009年 12月 07日 星期一 19:26:29 CST
date -u 2009年 12月 07日 星期一 11:26:29 UTC
hwclock 2009年12月07日 星期一 19时26分30秒 -0.442668 seconds
hwclock --utc 2009年12月08日 星期二 03时26分31秒 -0.999091 seconds
hwclock --localtime 2009年12月07日 星期一 19时26分32秒 -0.999217 seconds

可见,如果不使用UTC,BIOS时间(红色部分)就是系统本地时间,而且注意这时执行 hwclock --utc 得到的结果没有任何意义,因为这里我们已经禁用了UTC,而且也明显不符合“本地时间=UTC+时区”的关系。





RedHat Linux操作系统修改时区的方法
Most modern Linux distributions have user-friendly programs to set the timezone, often accesible through the program menus or right-clicking the clock in a desktop environment such as KDE or GNOME. Failing that it's possible to manually change the system timezone in Linux in a few short steps.
It's possible to change the system timezone in Linux in a few short steps.
Steps
1.     Logged in as root, check which timezone your machine is currently using by executing `date`. You'll see something like "Mon 17 Jan 2005 12:15:08 PM PST -0.461203 seconds", PST in this case is the current timezone.
2.     Change to the directory to /usr/share/zoneinfo, here you will find a list of time zone regions. Choose the most appropriate region, if you live in Canada or the US this directory is the "Americas" directory.
3.     If you wish, backup the previous timezone configuration by copying it to a different location. Such as `mv /etc/localtime /etc/localtime-old`.
4.     Create a symbolic link from the appropiate timezone to /etc/localtime. Example: `ln -s /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime`.
5.     If you have the utility rdate, update the current system time by executing `/usr/bin/rdate -s time.nist.gov`. (This step can be skip!)
6.     Set the ZONE entry in the file /etc/sysconfig/clock file (e.g. "America/Los_Angeles")
7.     Set the hardware clock by executing: ` /sbin/hwclock --systohc`
Tips
·       On some versions of RedHat Linux, Slackware, Gentoo, SuSE, Debian, Ubuntu, and anything else that is "normal", the command to display and change the time is 'date', not 'clock'
·       On RedHat Linux there is a utility called "Setup" that allows one to select the timezone from a list, but you must have installed the 'redhat-config-date' package.
Warnings
·       Some applications (such as PHP) have separate timezone settings from the system timezone.
·       On some systems, /etc/localtime is actually a symlink to the appropriate file under the /usr/share/zoneinfo directory (rather than a copy of that file).
·       On some systems, there is a system utility provided that will prompt for the correct timezone and make the proper changes to the system configuration. For example,Debianprovides the "tzsetup" utility.
Here is an example of changing the timezone: (Logged in as root)
In order to manually change the timezone, you can edit the /etc/sysconfig/clockfile and then make a new soft link to /etc/localtime. Here is an example of changing the timezone manually to "America/Denver":
1. Select the appropriate time zone from the /usr/share/zoneinfo directory. Time zone names are relative to that directory. In this case, we will select "America/Denver"
2. Edit the /etc/sysconfig/clocktext file so that it looks like this:
ZONE="America/Denver"
UTC=true
ARC=false
Of course, this assumes that your hardware clock is running UTC time...
3. Delete the following file: /etc/localtime (backup it when you need it !)
4. Create a new soft link for /etc/localtime. Here is an example of step 3 and step 4:
cd /etc
ls -al localtime
lrwxrwxrwx 1 root root 39 Mar 28 07:00 localtime -> /usr/share/zoneinfo/America/Los_Angeles
rm /etc/localtime
ln -s /usr/share/zoneinfo/America/Denver /etc/localtime
ls -al localtime
lrwxrwxrwx 1 root root 34 Mar 28 08:59 localtime -> /usr/share/zoneinfo/America/Denver
date
Fri Mar 28 09:00:04 MST 2003









三、linux与windows双系统间的时间同步


    系统启动和关闭时,硬件时间与系统时间之间的同步有两种方式(假设在中国,用CST代表本地时间):

方式A: 使用UTC(对linux就是 /etc/sysconfig/clock 中 UTC=true)

       开机: BIOS------->UTC(将BIOS中的时间看成是UTC)------(时区变化)----->CST
       关机: CST -------(时区变化)----->UTC-------存储到------>BIOS

 方式B: 不使用UTC(对linux就是 /etc/sysconfig/clock 中 UTC=false)

       开机: BIOS--------------------->CST(将BIOS中的时间看成是CST)
       关机: CST ---------存储到------>BIOS

 通过设定 /etc/sysconfig/clock,linux可以支持这两种方式,然而windows只支持方式B(至少是默认支持B,而我不知道怎么能让它支 持A),那么在双系统情况下,如果linux设成A方式,那么在linux与windows系统切换时一定会造成时间混乱的,解决办法就是将linux中 的UTC禁用,也设成B方式就可以了。

 注:可以通过 hwclock --hctosys 来利用硬件时间来设置系统时间(注意不是简单的复制BIOS中的时间为系统时间,要看是否使用UTC,如果使用的话则要做时区换算),通过 hwclock --systohc 来根据系统时间设置硬件时间(也要看是否启用UTC来决定是否做时区换算)。

总之,不论使用 --systohc 还是 --hctosys,同步后直接运行不带参数的 hwclock 得到的时间与直接运行 date 得到的时间应该一致,这个时间是否就是BIOS中的时间(hwclock --localtime)那就不一定了,如果启用了UTC就不是,没启用UTC就是。

而且还要注意:在系统中手动使用 hwclock hwclock --set --date='yyyy-mm-dd' 来设置BIOS时间只在系统运行时有效,因为当系统关闭时,还会按设定好的方式根据系统时间来重设BIOS时间的,于是手动的设置便被覆盖掉了。



总结一下hwclock:


 1)/etc/sysconfig/clock 文件,只对 hwclock 命令有效,且只在系统启动和关闭的时候才有用(修改了其中的 UTC=true 到 UTC=false 的前后,执行 hwclock (--utc, 或 --localtime) 都没有变化,要重启系统后才生效);

 2)/etc/rc.d/rc.sysinit 文件,run once at boot time,其中有从硬件时钟同步时间到系统时间的操作;

 3)hwclock --localtime 的输出,才是硬件时钟真正的时间。如果输出结果带时区(比如CST),还要看/etc/sysconfig/clock里的UTC参数,如果 UTC=false,那时区有意义;如果 UTC=true,那时区没意义,实际上是UTC时间。

 4)在 /etc/sysconfig/clock 中 UTC=false 时,date、hwclock、hwclcok --localtime 输出的时间应该都一致,且此时 hwclock --utc是没有意义的;

 5)在 /etc/sysconfig/clock 中 UTC=ture 时,date、hwclock 的输出是一致的,hwclock --localtime 的输出则是UTC时间;

 6)如果不想在输出中带时区,则 export LANG=C ,然后再运行 hwclock 就没有什么CST了,免得时区误导你;

 7)hwclock --utc 很闹腾,还是别看了,你会晕的。。。

8)系统关闭时会同步系统时间到硬件时钟,系统启动时会从硬件时钟读取时间更新到系统,这2个步骤都要根据 /etc/sysconfig/clock 文件中UTC的参数来设置时区转换。

2、实际案例分析:修改了  /etc/sysconfig/clock 中UTC参数但系统未正常关闭的情况


修改 /etc/sysconfig/clock 文件后,如果系统内核突然崩溃,然后直接按电源重启,则系统没有进行 系统时间到硬件时钟的 同步;但是 系统启动时,又根据 /etc/sysconfig/clock 中UTC的参数,来同步硬件时钟到系统,这时就会出现时间问题:

0)假设系统的时区为CST(UTC+8);
1)假设原 /etc/sysconfig/clock 中 UTC=true,修改成 UTC=false;
2)如果此时系统未正常关机,系统时间未按参数 UTC=false 同步时间到硬件时钟(没有+8小时);
3)但系统被按电源重启后,系统读取到 UTC=false,认为硬件时钟为CST时间,直接用于系统时间;
4)那么此时,系统时间将少了8小时。












linux时间修改:

1. 查看当前时区
[root@rac2 ~]# date -R
Mon, 02 Sep 2013 03:51:28 -0700            ——显示为西7区时间,我们接下来要改成东八区。

2. 修改设置时区
  方法(1)
[
 
根据上面的提示,我把  TZ='Asia/Shanghai'; export TZ加到了root用户下的 .bash _profile 最后面,重新logout然后login,再次查询的时候就成功了。。。。
  方法(2) 仅限于RedHat Linux 和 CentOS   timeconfig
 
这个方法是最简单的,一次设置永久有效。。非常的好哈哈。。。
  
方法(3) 适用于Debian
  dpkg-reconfigure tzdata


相关文章
|
8月前
|
Linux C++
通过C/C++代码设置Linux系统时间的方法与实例
Linux系统中的时间设置是关键任务之一,涉及日志记录、数据同步等众多应用场景。本文将详细介绍如何通过C/C++代码设置Linux系统时间,包括调用系统调用和使用第三方库,同时提供实例演示。
998 2
|
9月前
|
Ubuntu Linux 网络安全
Linux:使用ntpdate命令同步更新系统时间
Linux:使用ntpdate命令同步更新系统时间
1656 0
|
8月前
|
Linux 开发工具
百度搜索:蓝易云【Linux系统时间同步方法。】
在Linux系统中,时间同步是非常重要的,它能确保服务器和其他设备之间的时间一致性,从而避免出现时间戳错误的问题。
86 0
|
6月前
|
Linux
百度搜索:蓝易云【Linux 修改系统时间的两种方式?】
这些是在Linux系统中修改系统时间的两种常见方式。根据您的需求和权限,选择适合您的方法进行操作。
825 0
百度搜索:蓝易云【Linux 修改系统时间的两种方式?】
|
1月前
|
算法 Unix Shell
【Shell 命令集合 系统管理 】Linux 显示或设置系统时间和日期 date命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示或设置系统时间和日期 date命令 使用指南
33 0
|
1月前
|
存储 Linux Shell
【Shell 命令集合 系统设置 】Linux 设置和显示硬件时钟 hwclock命令 使用指南
【Shell 命令集合 系统设置 】Linux 设置和显示硬件时钟 hwclock命令 使用指南
25 0
|
1月前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】⭐Linux 显示系统的时钟时间 clock命令 使用指南
【Shell 命令集合 系统设置 】⭐Linux 显示系统的时钟时间 clock命令 使用指南
27 0
【Shell 命令集合 系统设置 】⭐Linux 显示系统的时钟时间 clock命令 使用指南
|
4月前
|
Linux
Linux - Date命令查看并设置系统时间
Linux - Date命令查看并设置系统时间
102 0
|
5月前
|
Java Linux
Linux系统时间和java的时间不一致
Linux系统时间和java的时间不一致
|
6月前
|
Linux 虚拟化
解决VMware 虚拟机中Linux系统时间不准确问题(安装VMware Tools)
解决VMware 虚拟机中Linux系统时间不准确问题(安装VMware Tools)
126 0