Linux time synchronization introduction, linux time synchronization

Source: Internet
Author: User

Linux time synchronization introduction, linux time synchronization

In Linux, synchronize is necessary to avoid the Time deviation caused by long running of the host. In Linux, ntp is generally used to synchronize the time of different machines. NTP is short for Network Time Protocol. Why is it used? Is to synchronize the time between computers through the network protocol.

System Time and hardware time

Before we start Linux system time synchronization, we must first understand some concepts: on a computer, we have two clocks: A hardware time clock (RTC)Real Time Clock, also known as Real-Time Clock), also known as System Clock ).

A hardware clock is a special circuit embedded on the motherboard. It exists because we can calculate the time after shutdown. RTC is short for Real-Time Clock, which is translated as a Real-Time Clock chip. RTC is the generation pulse of the clock circuit consisting of the crystal oscillator and related circuits on the PC motherboard, RTC generates an OS (system) Clock TSC with a lower frequency through a 8254-circuit frequency conversion. The system clock is added to one cycle for each cpu cycle and initialized by RTC at the beginning of the system. 8254 itself, you also need to have your own drive clock (PIT)

The system clock is the time clock used by the kernel of the operating system to calculate the time. It is the sum of seconds from 00:00:00 UTC on January 1, January 1, 1970 to so far. In Linux, the system time will be synchronization with the hardware at startup, and then it will run independently.

By default, the system time and hardware time are not automatically synchronized. During Linux running, the system time and hardware time run in asynchronous mode and do not interfere with each other. The hardware time is maintained by the BIOS battery, while the system time is maintained by the CPU tick. This is also the reason why the system may experience time deviation during a long period of operation. When I was a child, I bought an electronic watch. At that time, the electronic products were quite unreliable, when the electronic watch is walking, it will not be able to match the time in the news. It will always take several minutes or even dozens of minutes. The most worrying thing at that time is to manually adjust the time of the electronic watch, this is also a kind of time synchronization! The system time can be viewed by the date command, and the hardware time can be viewed by the clock or hwclock command. Let's take a look at the system time and hardware time.

[root@db-server ~]# date
 
Wed Jan 14 14:49:16 CST 2015
 
[root@db-server ~]# 
 
[root@db-server ~]# clock
 
Wed 14 Jan 2015 02:49:24 PM CST -0.473465 seconds
 
[root@db-server ~]# 
 
[root@db-server ~]# hwclock --show
 
Wed 14 Jan 2015 02:49:36 PM CST -0.625346 seconds
 
[root@db-server ~]# 
 
[root@db-server ~]# 

As shown in the following figure, we modified the system time to 08:00:00. Then we can see that the system time and hardware time are run asynchronously. Complementary interference.

[root@db-server ~]# date -s "2015-01-14 08:00:00"
 
Wed Jan 14 08:00:00 CST 2015
 
[root@db-server ~]# date
 
Wed Jan 14 08:00:02 CST 2015
 
[root@db-server ~]# clock
 
Wed 14 Jan 2015 02:52:54 PM CST -0.045672 seconds
 
[root@db-server ~]# 

You can use the hwclock or clock command to set the hardware time. Among them, clock and hwclock use similar, just use one line, but clock commands not only support x86 hardware system, but also support Alpha hardware system.

Set hardware time

[root@db-server ~]# clock
 
Wed 14 Jan 2015 03:24:09 PM CST -0.941272 seconds
 
[root@db-server ~]# hwclock --set --date="2015-01-15 15:24:00";
 
[root@db-server ~]# clock
 
Thu 15 Jan 2015 03:24:04 PM CST -0.476775 seconds
 
[root@db-server ~]# 

[root@db-server ~]# clock --help
hwclock - query and set the hardware clock (RTC)
 
Usage: hwclock [function] [options...]
 
Functions:
  --help        show this help
  --show        read hardware clock and print result
  --set         set the rtc to the time given with --date
  --hctosys     set the system time from the hardware clock
  --systohc     set the hardware clock to the current system time
  --adjust      adjust the rtc to account for systematic drift since 
                the clock was last set or adjusted
  --getepoch    print out the kernel's hardware clock epoch value
  --setepoch    set the kernel's hardware clock epoch value to the 
                value given with --epoch
  --version     print out the version of hwclock to stdout
 
Options: 
  --utc         the hardware clock is kept in coordinated universal time
  --localtime   the hardware clock is kept in local time
  --directisa   access the ISA bus directly instead of /dev/rtc
  --badyear     ignore rtc's year because the bios is broken
  --date        specifies the time to which to set the hardware clock
  --epoch=year  specifies the year which is the beginning of the 
                hardware clock's epoch value
  --noadjfile   do not access /etc/adjtime. Requires the use of
                either --utc or --localtime
 
[root@db-server ~]# hwclock --show
Thu 15 Jan 2015 03:40:47 PM CST  -0.544844 seconds
[root@db-server ~]# clock --set --date  "2015-01-14 15:41:00";
[root@db-server ~]# hwclock --show
Wed 14 Jan 2015 03:41:12 PM CST  -0.740512 seconds
[root@db-server ~]# 

Synchronization of system time and hardware time

We can use hwclock -- systohc to set the system time to the hardware time, as shown in the following figure (we first modify the system time, which causes the system time to be inconsistent with the hardware time)

[root@db-server ~]# date -s "2015-01-14 15:53";
Wed Jan 14 15:53:00 CST 2015
[root@db-server ~]# date 
Wed Jan 14 15:53:03 CST 2015
[root@db-server ~]# hwclock --systohc
[root@db-server ~]# date 
Wed Jan 14 15:53:35 CST 2015
[root@db-server ~]# hwclock --show
Wed 14 Jan 2015 03:53:36 PM CST  -0.149835 seconds

You can also set the hardware time to the system time by running the following command:

Hwclock -- hctosys

Time Synchronization settings

Network Time Protocol (NTP) is a standard Internet Protocol used for Time synchronization on the Internet. NTP is used to synchronize computer time to certain time standards. The current Time standard is Universal Time Coordinated ). The primary developer of NTP is Professor David L. Mills from the University of Delaware. For more information about the concept of NTP, I would like to explain it in detail. For the moment, we only focus on how to synchronize time. To synchronize time, you must have a precise NTP Server. For NTP Server, refer to the NTP official website http://www.pool.ntp.org, where we can find the NTP Server closest to our city.

In the synchronization world, we generally use the ntpd or ntpdate commands. What is the difference between ntpd and ntpdate in the Update time? Ntpd is not only a time synchronization server, it can also synchronize time between the client and the standard time server, but also smoothly synchronize not ntpdate immediately. Use ntpdate with caution in the production environment, the two cannot run simultaneously.

One is the calibration time, and the other is the adjustment time. That is to say, ntpd is calibrated a little bit in actual synchronization time, and the time is gradually corrected. However, ntpdate does not take into account the pain points of other programs and directly adjusts the time.

As shown in the following figure, first modify the system time and then use the ntpdate command to synchronize the system time.

[Root @ DB-Server ~] # Date

Tue Jan 27 23:44:54 CST 2015

[Root @ DB-Server ~] # Date-s "23:48:50"

Sun Dec 27 23:48:50 CST 2015

[Root @ DB-Server ~] # Ntpdate 2.cn.pool.ntp.org

27 Jan 23:45:41 ntpdate [6930]: step time server 202.112.31.197 offset-28857832.720974 sec

[Root @ DB-Server ~] # Date

Tue Jan 27 23:45:44 CST 2015

However, if there is a problem with the cpu tick, such adjustments are often difficult to solve the problem at one time. Therefore, you must use the cron command to completely eliminate this problem, for example, the following cron command.

*/30 */usr/sbin/ntpdate 192.168.7.161 1>/dev/null 2> & 1

It is recommended that you have a unique chapter on ntpd to introduce how to configure ntpd. We will not detail it here.

References:

Http://blog.csdn.net/suer0101/article/details/7868813

Http://blog.csdn.net/sjx800688/article/details/7010979

Http://hoya120.blog.163.com/blog/static/520294792010612103319272/

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.