The hwclock__linux of SHELL commands under Linux

Source: Internet
Author: User
Tags cst time time zones local time set time

Reprint: http://blog.gesha.net/archives/221/ Linux system time and hardware clock issues (date and Hwclock) summarize Hwclock, this is easy to faint:

1/etc/sysconfig/clock file, only valid for the Hwclock command, and only useful when the system starts and shuts down (modifies utc=true to Utc=false before and after, and executes Hwclock (–UTC, or –localtime ) have not changed and will not take effect until the system is restarted;
2/etc/rc.d/rc.sysinit file, run once at boot time, which has the operation from the hardware clock synchronization to the system time;
3 Hwclock–localtime output is the real time of hardware clock. If the output takes a time zone (such as a CST), it also depends on the UTC parameter in the/etc/sysconfig/clock, and if utc=false, then the area is meaningful; if utc=true, then the area is meaningless, and it's actually UTC time.
4 when the Utc=false in/etc/sysconfig/clock, date, Hwclock, hwclcok–localtime output time should be consistent, and at this time HWCLOCK–UTC is meaningless;
5 when utc=ture in/etc/sysconfig/clock, the output of date and Hwclock is consistent and the output of hwclock–localtime is UTC time;
6 If you do not want to take time zone in the output, then export lang=c, and then run Hwclock there is no CST, lest time zone misleading you;
7 HWCLOCK–UTC very noisy, or don't look, you will faint ...
8 system shutdown will synchronize the system time to the hardware clock, the system starts from the hardware clock read time update to the system, these 2 steps are based on the/etc/sysconfig/clock file in UTC parameters to set time zone conversion.
2, the actual case analysis: modified/etc/sysconfig/clock UTC parameter But the system is not properly shut down
After modifying the/etc/sysconfig/clock file, if the system kernel suddenly crashes and then presses the power cycle directly, the system does not synchronize the system time to the hardware clock, but when the system starts, and according to the parameters of UTC in/etc/sysconfig/clock, To sync the hardware clock to the system, there will be a time problem:
0) Suppose the time zone of the system is CST (UTC+8);
1) Assume the original/etc/sysconfig/clock utc=true, modified into utc=false;
2 If the system does not shut down normally, the system time is not according to the parameter Utc=false synchronization time to the hardware clock (no + 8 hours);
3) But after the system is restarted, the system reads to Utc=false, and considers the hardware clock as CST time, which is directly used in the system time.
4 So at this point, the system time will be less than 8 hours.first, we need to understand a few concepts:

"System Time" and "Hardware Time"

System time: Typically the time we see the date command, all the time calls (except for direct access to the hardware time) in the Linux system are used this time.

Hardware time: The time in the BIOS on the motherboard, by the motherboard battery power to maintain operation, the system on the boot to read this time, and according to it to set the system time (note: System startup According to the hardware time to set the system time of the process may have time zone conversion, depending on the specific system and related settings).

"UTC Time" and "local time"

UTC time: Coordinated Universal 8 E2 I (H7 t0 ^/^time World coordinated Time (also known as World standard Time, world unification time), with GMT (Greenwich Mean time, Greenwich mean times), under general precision requirements, is a Kind of, in fact, that is to say GMT≈UTC, but UTC is calibrated by atomic clocks, more accurate.

local time: Because in different time zones, local time is generally different from UTC, the conversion method is

local time = UTC + Time zone or UTC = local time-time zone

Time zone East is positive, West is negative, for example, in China, the local time is used in Beijing time, on Linux display is CST (China Standard time, Chinese standard, when attention to the United States Central Standard when Standard time also abbreviated as CST, It's not the same as the CST here. ), the time zone is East eight, which is +8, so cst=utc+ (+ 8 hours) or utc=cst-(+ 8 hours). second, time order

System Time Date

Call date directly and get the local time. If you want to get UTC time, use Date-u.

[12-01 19:07> ~] date2009 Year December 07 Monday 14:22:20cst[12−0119:07>] Date December 07, 2009 Monday 14:22:20 CST [12-01 19:07> ~] Date-u
Monday, December 07, 2009 06:22:22 UTC
2. Hardware Time/sbin/hwclock

A direct call to/sbin/hwclock display time is the time in the BIOS. Not necessarily. This depends on whether UTC is enabled in the/etc/sysconfig/clock, if UTC (Utc=true) is enabled, the time zone conversion is actually displayed instead of the actual time in the BIOS, and if the –localtime option is added, the BIOS is always The actual time in the.

[12-01 19:07> ~]# Hwclock
December 07, 2009 Monday 14:28 43 sec -0.611463 seconds
[12-01 19:07> ~]# HWCLOCK–UTC
December 07, 2009 Monday 14:28 46 sec -0.594189 seconds
[12-01 19:07> ~]# Hwclock–localtime
December 07, 2009 Monday 06:28 50 sec -0.063875 seconds
3./etc/localtime

This file is used to set the system time zone, copy the corresponding file in/usr/share/zoneinfo/to/etc and rename it to LocalTime to modify the time zone settings, and this modification takes effect in time for the date command. This file is used by both date and Hwclock, and the conversion between UTC and the locality is based on the time zone setting of the file.
4./etc/sysconfig/clock

This file is only valid for Hwclock and seems to be useful only when the system starts and shuts down, such as modifying the Utc=true to Utc=false before and after the execution Hwclock (–UTC, or –localtime) has not changed, to reboot the system before it takes effect. Note: If you set Utc=false and restart the system, some commands are executed as follows:

Date December 07, 2009 Monday 19:26:29 CST
date-u December 07, 2009 Monday 11:26:29 UTC
hwclock December 07, 2009 Monday 19:26 30 sec-0.44 2668 seconds
hwclock--utc December 08, 2009 Tuesday 03:26 31 sec -0.999091 seconds
hwclock--localtime December 07, 2009 Monday 19:26 32 seconds, -0.999217 seconds.

Visible, if the Utc,bios time is not used (the red part) is the system local time, and note that the result of the execution of HWCLOCK–UTC does not make any sense, as we have disabled UTC here and obviously do not conform to the "local time =utc+ time zone" relationship. third, the time synchronization between Linux and Windows dual system

When the system starts and shuts down, there are two ways to sync between the hardware time and the system time (assuming that the CST represents local time in China):

Way A: use UTC (For Linux is/etc/sysconfig/clock utc=true)
Power on: BIOS ——->UTC (consider time in BIOS as UTC)-(Time zone change)-–>CST
Shutdown: CST ——-(Time zone change)-–>utc ——-stored to-->bios
Mode B: Do not use UTC (Linux is/etc/sysconfig/clock utc=false)
power-on: BIOS ————— -->CST (Consider the time in the BIOS as CST)
Shutdown: CST ——— store to-->bios
/etc/sysconfig/clock,linux can support both methods by setting the However, Windows only supports mode B (at least the default support B, and I don't know how to get it to support a), then in the case of a dual system, if Linux is set to a, then the Linux and Windows system switch will cause time confusion, the solution is to the Linux UTC is disabled, and it is set to B.
Note: You can use hardware time to set the system time by Hwclock–hctosys (note that it is not simple to copy the time in the BIOS to the system time, to see if you are using UTC, if you use it, do time zone conversion), and pass the HWCLOCK–SYSTOHC To set the hardware time based on the system time (also depends on whether UTC is enabled to decide whether to make a time zone conversion).
in summary, regardless of whether you are using –SYSTOHC or –hctosys, the time to run the Hwclock without parameters directly after synchronization should be consistent with the time that you obtained directly from running date, which is the time in the BIOS (hwclock–localtime) That is not necessarily true, if UTC is enabled, it is not, and UTC is not enabled.
and also note that the manual use of Hwclock hwclock–set–date= ' YYYY-MM-DD ' in the system to set the BIOS time is only valid when the system is running, because when the system shuts down, the BIOS time is reset according to the system time, The manual settings are then overwritten. Four, instance

The Linux clock is divided into system clocks (systems Clock) and hardware (real time Clock, short RTC) clocks. The system clock is the clock in the current Linux kernel, and the hardware clock is the battery-powered clock on the motherboard, which can be set in the BIOS. When Linux is started, the hardware clock will read the system clock settings, and the system clock will be independent of the hardware operation.
All the commands (including functions) in Linux are set by the system clock. In Linux, the commands for clock viewing and setting are mainly date, hwclock, and clock. Among them, clock and hwclock usage is similar, only use one on the line, except that the clock command supports the Alpha hardware system in addition to the x86 hardware system.

1. Date
View system time
# date
Set system time
# date--set "07/07/06 10:19" (Month/day/year: minutes: SEC)
2, Hwclock/clock
View hardware Time
# Hwclock--show
Or
# clock--show
Set Hardware time
# Hwclock--set--date= "07/07/06 10:19" (Month/day/year: minutes: SEC)
Or
# clock--set--date= "07/07/06 10:19" (Month/day/year: minutes: SEC)
3, hardware time and system time synchronization
According to the previous version, reboot the system, the hardware time will read the system time, to achieve synchronization, but without restarting, you need to use the Hwclock or clock command to achieve synchronization.

The hardware clock synchronizes with the system clock:
# Hwclock--hctosys (HC represents hardware time, SYS represents system time)
Or
# clock--hctosys
system clock and Hardware clock sync:
# Hwclock--SYSTOHC
Or
# CLOCK–SYSTOHC '

4, the time zone setting tzselect

# Tzselect Please identify a location so this time zone the rules can be set correctly.
Please select a continent or ocean. 1) Africa 2) Americas 3) Antarctica 4) Arctic Ocean 5) Asia 6) Atlantic Ocean 7) Australia 8) Europe 9) Indian Ocean) P
Acific Ocean None-i Want to specify the time zone using the Posix TZ format. #?
Enter 5, Asia please select a country.  1) Afghanistan Israel) Palestine 2) Armenia) Philippines 3) Azerbaijan Jordan (4) Qatar Kazakhstan) Russia 5 Bangladesh) Korea (north) Saudi Arabia 6) Bhutan) Korea (South)) Singapore 7) B Runei) Kuwait) Sri Lanka 8) Cambodia Kyrgyzstan) Syria 9)-Laos) Taiwan 44 (Cyprus) Tajikistan) (Timor) MACAU) Thailand) Georgia) Malaysia (Turkmenistan) Kong 47) United Arab Emirates) India Myanmar (Burma)) Uzbekistan) Indonesia () Nepal) (Vietnam) Yemen) Iraq) PakistaN #?
Enter 9, China Please select one of the following time zone regions. 1) East China-beijing, Guangdong, Shanghai, etc. 2) Heilongjiang 3), China-gansu, Guizhou, Sichuan, etc
. 4) Tibet & most of Xinjiang Uyghur 5) southwest Xinjiang #?
Input 1, Beijing time the following information has been Given:china East China-beijing, Guangdong, etc.
Therefore tz= ' Asia/shanghai ' would be used.
Local Now:fri June 7 10:32:18 CST 2006.
Universal is Now:fri June 7 02:32:18 UTC 2006.
is the above information OK? 1) Yes 2 No #?
 Enter 1 to confirm

If you do not use the Tzselect command, you can modify the file change time zone.

# Vi/etc/sysconfig/clock
Zone=asia/shanghai (check/usr/share/zoneinfo file below)
utc=false
arc=false
# RM/ Etc/localtime
# Ln-sf/usr/share/zoneinfo/asia/shanghai/etc/localtime

Reboot can be done.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.