Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of the Linux basic command hwclock

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the usage of Linux basic command hwclock". In daily operation, I believe many people have doubts about the usage of Linux basic command hwclock. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the usage of Linux basic command hwclock"! Next, please follow the editor to study!

Hwclock

Hwclock is a tool for accessing the hardware clock that displays the current time, sets the hardware clock to a specified time, sets the hardware clock to the system time, and sets the system time from the hardware clock. You can also periodically run hwlock to insert or delete time from the hardware clock to compensate for system drift (if you continue to run, the clock always gains or loses time at a certain rate).

The scope of application of this command: RedHat, RHEL, Ubuntu, CentOS, SUSE, openSUSE, Fedora.

1. Grammar

Hwclock [functions] [options]

2. List of options

The following options tell hwclock which function to execute

Option

Description

-r |-- show

Read the hardware clock and print the time on standard output. The time displayed is always local time, even if you keep your hardware clock at coordinated world time. See-UTC option.

-- set

Set the hardware clock to the time specified by the-date option

-s |-- hctosys

Set the system time from the hardware clock.

The kernel's time zone value is also set to the local time zone indicated by the TZ environment variable and / or / usr/share/zoneinfo, as explained by tzset (3). The outdated tz_dsttime field of the kernel time zone value is set to DST_NONE. (for more information about the past meaning of this field, see settimeofday (2) for the date.

This is a good option to use in system startup scripts

-w |-- systohc

Set the hardware clock to the current system time

-- systz

Sets the kernel time zone based on the current time zone and resets the system time, which is reset only on the first call after startup.

The local time zone is considered to be indicated by the TZ environment variable and / or / usr/share/zoneinfo, and tzset (3) will explain them. The outdated tz_dsttime field of the kernel time zone value is set to DST_NONE. (for more information about the past meaning of this field, see settimeofday (2) for the date.

This is another option for-hctosys, which does not read the hardware clock and can be used in the most recent 2.6 kernel system startup scripts, because you know that system time contains hardware clock time. If the hardware clock is already in coordination, it will not be reset.

-- adjust

Increase or subtract time from the hardware clock to illustrate the system drift since the last clock setting or adjustment. Please see the discussion below

-- getepoch

Print the hardware clock era of the kernel to standard output. This is the number of years in which the zero value in the hardware clock is referenced in AD. The convention being used is that the year counter in the hardware clock contains the full number of years since 1952, and then the kernel's hardware counter age value must be 1952.

This epoch value is used whenever hwlock reads or sets the hardware clock. Epoch is available only on Alpha machines.

-- setepoch

Set the hardware clock era value of the kernel to the value specified by the-- epoch option.

-v |-- version

Display the version of hwclock on standard output

-- date=date_string

This option is required if you specify the-set option. Otherwise, this option is ignored. This specifies the time to set the hardware clock. Option is an argument to the date directive. For example: hwclock-set-date= "16:45:05 on 9-22-96"

Parameter at local time, even if you keep the hardware clock at coordinated world time. See-UTC option.

-- epoch=year

Specifies the year in which the hardware clock era begins, that is, in AD, the number of years entering the AD indicated by the zero value in the year counter of the hardware clock. It is used with the-setepoch option to set the hardware clock age concept of the kernel or to specify the age for direct ISA access. For example:

Hwclock-setepoch-epoch=1952

The following options are used with the function

Option

Description

-u |-- utc |-- localtime

Indicates that the hardware clock remains at coordinated world time or local time, respectively. You can choose whether to keep the clock at coordinated Universal time or local time, but no information is displayed in the clock. This is your choice, so this option is how you provide the information to the clock.

If you specify one of these options wrong (or not specified, and choose the wrong default value), the hardware clock setting and query will be messed up.

If you do not specify-utc or-localtime, the default value is the value specified the last time you set the clock using hwlock (that is, hwlock successfully runs with the-set,-system ohc, or-regulator option), as recorded in the adjtime file. If the adjtime file does not exist, the default value is local time.

-- noadjfile

Disable the tools provided by / etc/adjtime.hwlock and do not use this option to read or write to the file. -UTC and-- localtime must be specified when using this option

-- adjfile=ilename

Overwrite the default / etc/adjtime file

-f |-- rtc=filename

Override the default / dev filename, which is / dev/rtc on many platforms, but it could be / dev/rtc0, / dev/rtc1, and so on.

-- directisa

It only makes sense on ISA machines or Alpha (roughly speaking, it implements enough ISA machines to achieve the purpose of hwlock). For other machines, it has no effect. This option tells hwlock to access the hardware clock using the explicit Ithumb O instruction. If this option is not available, hwlock will try. To use a / dev/rtc device (assuming it is driven by a RTC device driver), if it cannot open the device (for reading), it will use the explicit Imando O instruction.

-- badyear

Indicates that the hardware clock cannot store years beyond the range of 1994-1999. There is a problem with some BIOSes (almost all BIOSes manufactured between 4-26-94 and 5-31-95), in which they cannot handle years after 1999. If you try to set the century value to a value less than 94 (or 95 in). In some cases, the actual value is 94 (or 95), so if you have one of these machines, hwlock cannot be set after 1999, nor can you use the value of the clock as the real time in a normal way.

To make up for this (it's certainly better without BIOS updates), always use-if you have one of these machines-a bad year. The date is assumed to be the date of the past year in the adjtime file. For it to work, you'd better run hwclock-set or hwlock-systohc at least once a year.

Although hwlock ignores the year value when reading the hardware clock, when it sets the clock, it sets the year value to 1995, 1996, 1997, or 1998, regardless of which year is in the same position in the leap year cycle as the real year. In this way, the hardware clock will be inserted. If you let the hardware clock run without a setting for more than a year, this scheme may fail and you may end up losing a day.

-- srm

This option is equivalent to-- epoch=1900 and is used to specify the most common epoch on the alphad using the srm console.

-- arc

This option is equivalent to-- epoch=1980 and is used to specify the most common history on ALPHS using the ARC console (but Ruffans has 1900).

-- jensen |-- funky-toy

These two options specify the type of Alpha machine you own. If you don't have Alpha, they are invalid, and if so, they are usually unnecessary, because hwlock should be able to determine what it is running on, at least when installing / proc. (if you find that you need one of these options to make hwlock work, contact maintenance to see if the program can be improved to automatically detect your system.' Hwlock-- the output of debug' and 'cat/proc/cpuinfo' may be of interest.)

-- jensen represents running in Jensen mode.

-- funky-toy means that on your machine, you must use the UF bit instead of the UIP bit in the hardware clock to detect the time conversion. The "toy" in the option name refers to the time of year for the machine

-- test

Test the program without changing any settings

-- debug

Showing a lot of information about what's going on inside hwlock, some of which are complex, this output can help you understand how the program works.

3. Description

Generally speaking, there are two clocks in the operating system, the hardware clock is the timer clock on the motherboard, and the system clock is the kernel clock of the system, and they do not affect each other.

1) hardware clock

This clock runs independently of any control program running in CPU, even when the machine is turned off. In ISA systems, this clock is specified as part of the ISA standard. The control program can read or set the clock to the whole second, but the control program can also detect the edge of the 1-second clock, so the clock has virtually unlimited precision.

Such clocks are often called hardware clocks, real-time clocks, RTC, BIOS clocks, and CMOS clocks. The hardware clock was invented by hwlock in uppercase because all other names are not suitable for misleading. For example, some non-ISA systems have several real-time clocks. A very low-power I2C or SPI clock chip can be used as a hardware clock with a backup battery to initialize a better integrated real-time clock for most other uses.

2) system clock

This is the time held by the clock in the Linux kernel and driven by timer interrupts. On ISA machines, timer interrupts are part of the ISA standard. It only makes sense if linux is running on the machine. The system time is the number of seconds since 00:00 Universal coordinated time (UTC) on January 1st, 1970 (or, more succinctly, since 1969). However, the system time is not an integer, it is actually infinite. System time is an important time. The basic purpose of a hardware clock in a Linux system is to maintain time when Linux is not running. When Linux starts, initialize the system time from the hardware clock, and then no longer use the hardware clock. Note that in the DOS that designed the ISA, the hardware clock is the only real-time clock.

Importantly, there is no discontinuity in system time when the system is running, such as using the date command to set it. However, while the system is running, you can do whatever you want with the hardware clock, and the next time Linux starts, it will be set with the adjustment time of the hardware clock.

The Linux kernel maintains the concept of the system's local time zone. But don't be misled-few people care what time zone the kernel thinks it is in. In contrast, programs that care about the time zone (perhaps because they want to show you the local time) almost always use a more traditional approach. Determine the time zone: they use the "tz" environment variable or the "/ usr/share/zoneinfo" directory, as explained on the man page of tzset (3). The time zone value is incorrect and the vFAT file system reports and sets the wrong timestamp on the file.

When you set the system time with the "--hctosys" option, hwlock sets the kernel time zone to the value indicated by "tz" or "/ usr/share/zoneinfo".

The time zone value actually consists of two parts: 1) the field "tz_minutesWest" indicates that the local time (not adjusted according to DST) lags behind the UTC;2) field "tz_dsttime", indicating the type of daylight saving time (DST) convention that is currently in force locally. The second field is not used under Linux and is always zero.

3) how to access the hardware clock by hwclock

Hwlock uses a number of different methods to get and set hardware clock values, the most common of which is to perform an I dev/rtc O operation on the device special file "/ clock", assuming that the file is driven by a rtc device driver. However, this approach is not always available. First of all, the rtc driver is a relatively new kind of linux. In addition, although there are some versions of rtc drivers that work on decalpha, it seems that a large number of alpha do not work (a common symptom is that the clock hangs). In addition, recent linux systems have more general support for rtc, even for more than one system, so you may need to override the default values by specifying / dev/rtc 0 or / dev/rtc 1.

In the old system, the method of accessing the hardware clock depends on the system hardware.

In the ISA system, hwlock can directly access the "CMOS memory" register that makes up the clock by performing the I / O operation on the ports 0x70 and 0x71. It uses the actual Istroke O instruction, so this can only be done if the superuser is a valid user, ID. (for jensen Alpha,hwlock, these Iamp O instructions cannot be executed, so it uses the device file "/ dev/port", which provides an interface almost as low as the Iamp O subsystem.) This is a very bad way to access the clock, because user-space programs should not normally perform direct I _ stop O and disable interrupts. But in ISA and Alpha systems, this is the only way.

On m68k systems, hwlock can access the clock through the console driver and the clock through the device file "/ dev/tty1".

Hwlock tried to use the file "/ dev/rtc". If the kernel does not compile "/ dev/rtc", or if it cannot open "/ dev/rtc", hwlock will return to another method (if available). On ISA or Alpha computers, you can force hwclock to use direct manipulation of CMOS registers without specifying the "--directisa" option.

4) Calibration function adjust

The hardware clock is usually not very accurate, but many of its inaccuracies are completely predictable, and it gains or loses the same time every day. This is called system drift. The adjust function of hwlock allows you to make system corrections to correct system drift. It works as follows: hwlock saves a file "/ etc/adjtime" that holds some historical information.

Assuming you don't have an adjtime file, issue the hwlock-set command to set the hardware clock to the real current time. Hwlock creates the adjtime file and records the current time in it as the last time to calibrate the clock. Five days later, the clock increases by 10 seconds, so you can issue another "hwlock-- set" command to set it. Return 10 seconds. Hwlock updates the adjtime file, showing the current time as the last time to calibrate the clock, and records 2 seconds per day at the system drift rate. 24 hours have passed, and then issue the "hwlock-adjust" command. Hwlock looked up the adjtime file and saw that the clock left by 2 seconds a day. I stayed alone all day. So it subtracts 2 seconds from the hardware clock. It then records the current time as the last time to adjust the clock. After another 24 hours, you issued another "hwclock-adjust" command. Hwclock does the same thing: subtract 2 seconds and update the adjtime file with the current time, which is the last time to adjust the clock.

Each time you calibrate the clock (using-- set or-systohc), hwlock recalculates the system drift rate based on the time of the last calibration, the time after the last adjustment, the assumed drift rate in any intermediate adjustment, and the current turn-off amount of the clock. Any time hwclock is used, there will be a small error, so it will not be adjusted for less than 1 second. Later, when you request the adjustment again, the cumulative drift will exceed 1 second, while the hwlock will adjust.

It is good to adjust the hwlock before "hwlock-- hctosys" when the system starts, and it can also be adjusted periodically when the system is running through cron.

Although the adjtime file is named only to control the history of time adjustments, it actually contains other information that hwlock uses to remember information from one call to the next. The format of the adjtime file is ASCII:

The three digits in the first line, separated by spaces, represent: a) system drift rate, in seconds per day, floating point decimal point; b) the number of seconds recently adjusted or calibrated since UTC in 1969, a decimal integer; and c) zero (compatible with clock (8)) is a decimal integer.

Line 2 is a number that represents the number of seconds produced by the last calibration since UTC in 1969. If there is no calibration, or if any previous calibration is known to be meaningless, the value is 0 (for example, because after calibration, the hardware clock has been found and does not include valid time). This is a decimal integer.

The third line is "utc" or "local". Indicates whether the hardware clock is set to coordinated world time or local time.

5) how to automatically synchronize the hardware clock in the kernel

In some systems, you should notice another way for hardware clocks to stay synchronized. The Linux kernel has a mode that replicates system time to the hardware clock every 11 minutes. This is a good pattern when you use something complex, such as NTP, to keep the system time synchronized. NTP is a way to keep system time synchronized. It can be synchronized with a time server on the network or with a radio clock connected to your system. See RFC 1305).

This mode (which we call "11-minute mode") is turned off until something opens it. The ntp daemon xntpd can open it. You can turn it off by running anything, including "hwlock-- hctosys," which sets the system time in the old-fashioned way.

If your system is running in 11-minute mode, do not use "hwlock--adjust" or "hwlock-hctosys". Using "hwlock-- hctosys" at startup to get a reasonable system time is acceptable until your system is up and running. Set the system time from an external source and start 11-minute mode

4. Examples

1) do not apply any parameters, check the hardware clock directly

[root@localhost ntop-4.0.1] # hwclock

Thursday, August 23, 2018, 15:01:28-0.577410 seconds

2) set the hardware clock

[root@localhost ntop-4.0.1] # hwclock-- set-- date= "0904" / / set the hardware clock. Date parameter is required to work with it.

[root@localhost ntop-4.0.1] # hwclock

Tuesday, September 04, 2018, 09:04:09-0.479386 seconds

3) set the time format of the cost of the hardware clock

[root@localhost ntop-4.0.1] # hwclock-- localtime

Tuesday, September 04, 2018, 01:05:46-0.462990 seconds

4) set the hardware clock to the system time

[root@localhost ntop-4.0.1] # date / / View the current system time

Tuesday, September 04, 2018, 12:25:15 CST

[root@localhost ntop-4.0.1] # hwclock-w / / set the hardware clock to the system time

[root@localhost ntop-4.0.1] # hwclock / / View hardware clock

Tuesday, September 04, 2018, 12:25:48-0.263687 seconds

At this point, the study of "the usage of the Linux basic command hwclock" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report