Advanced Linux _ Time Synchronization (NTP and chrony)

introduction

  • Ancient timing mode

● In ancient times, humans used to determine the time of some of nature's way is "relative" everlasting cycle. As the Earth's revolution is for one year, the moon revolution is in January, the Earth's rotation is a day like the earliest timing can be traced back about 2000 BC, ancient Egyptians used the shadow left by the use of light timing tool. Shadow pull the longer, the more precise timing. The ancient Egyptians built a large obelisk towering to track the movement of the sun, then people took advantage of the hourglass, sundial, pendulum and other tools, clever use of relatively fixed number of cycles to the timing and accurate
● Shang people to develop and use a type of drain water clock - clepsydra. Subsequently, with candles and incense timing
, astronomer Su Song will armillary sphere, celestial globe and combine timekeeping device ● North Songyuan the first year (1086), the construction of a landmark timing tool - "clock tower"
● 14 when century, Western countries are widely used mechanical clock. In the sixteenth century, the Ottoman Empire scientists Founders Ai - Ding (Taqi al-Din) invented a mechanical alarm clock
● 1583, Galileo made famous isochronous theory that regardless of the size of the swings, to complete a swing the time is the same. In 1656, Dutch scientist克里斯蒂安Huygens (Christiaan Huygens) applied his theory, designed the world's first pendulum
● 1868 years, the Patek Philippe (Patek Philippe) invented the wristwatch

  • Modern timing mode

● When the quartz crystal cell subjected to the influence of power, vibration is generated regularly. The number of vibrations per second is 32768 times the circuit may be designed to calculate a vibration frequency when the count to 32768 times, i.e., 1 second timer. In 1967, the Swiss released the first quartz watch in the world
● when from a relatively high atomic "energy state" moved to low "energy state", will release electromagnetic waves, resonance frequency. According to this principle, the rabbis devised a new timing equipment - atomic (Atomic Clock)
● Because the resonance frequency of the atom is fixed. Such as: natural frequency of the cesium atom (Caesium133) is 9,192,631,770 Hz, about 9.2 billion Hz, cesium clock count of 9,192,631,770 times, to measure a second. Many countries (including the USA and China NIST) of the Bureau of Standards, is to use cesium atomic clocks as precision time standard. GPS systems are also used cesium atomic clock timing
● 2008 birth of strontium (Strontium87) atomic clock, the natural frequency of 429,228,004,229,873, or about 430 trillion hertz, will improve the accuracy of 17 to 10 to the power
● 2013 years ytterbium element (ytterbium) made the advent of atomic clocks, the natural frequency of ytterbium atomic clock about 518 trillion hertz, accuracy of up to 10 ^ 18. Age of the universe is 13.8 billion years. If this ytterbium atomic clock starts counting from the beginning of the birth of the universe, until today not one second of error occurred

Time synchronization,

Time Synchronization: multi-host collaborative work time each host synchronization is very important, time inconsistency will cause a lot of failures of important applications, such as: encryption protocol, logging, clustering, etc., using the NTP (Network Time Protocol) protocol so that each network computer time to achieve synchronization. NTP is one of the basic services currently belong to the operation and maintenance of the necessary infrastructure

  • ntp: UTC will synchronize the system clock and the world coordination, accuracy up to 0.1ms in the LAN, the vast majority of places on the Internet accuracy can be achieved 1-50ms, the project's official website: http://www.ntp.org
  • chrony: Free software implementation of the NTP protocol. The system allows the NTP server clock, the reference clock (e.g. GPS receiver) and the use of manual input and keyboard synchronize watches. Can also serve as NTPv4 (RFC 5905) server and peer operation, the computer provides the network service time. Designed to run well under a variety of conditions, including intermittent and highly congested network connection, a temperature change (temperature sensitive computer clock), and can not run or continuous system running on the virtual machine. Typical accuracy through the Internet between two machines synchronized within a few milliseconds, in over LAN, you typically tens of microseconds accuracy. Using hardware or hardware reference clock time stamp, sub-microsecond accuracy can be achieved

ntp

Experiment: ntp time synchronization

Experiment is relatively simple, not drawing a.

  1. HostA used to make time server, hostA with Ali cloud when the time source.
  2. HostB to synchronize with hostA

host A

When the time server you want to modify ntp.confntp configuration file, the restrict default nomodify notrap nopeer noqueryline commented on it. Without comment, we can not act as a time server.

[root]$ yum -y install ntp
[root]$ systemctl start ntpd
[root]$ systemctl enable ntpd
[root]$ vim /etc/ntp.conf   
    #restrict default nomodify notrap nopeer noquery
    server ntp.aliyun.com iburst
[root]$ systemctl restart ntpd
[root]$ ntpq -p

Here Insert Picture Description
In front view showing ip with "*" I have been synchronized. Then we can use hostB to sync with it

host B

To see the effect of sync, we have time to manually Correction

[root]$ date -s "2 day"

Then install ntp, modify the configuration file. Then the way to server 0.centos.pool.ntp.org iburstthese lines removed, so talk to other people synchronized. Remember to change the configuration to be done to restart the service

[root]$ yum -y install ntp
[root]$ systemctl start ntpd
[root]$ systemctl enable ntpd
[root]$ vim /etc/ntp.conf   
    server 192.168.88.7 iburst
[root]$ systemctl restart ntpd
[root]$ ntpq -p

Here Insert Picture Description

chrony

chrony advantages:

  1. Faster sync, thereby minimizing the time and frequency error, very useful for the virtual machine is not running 24 hours a day
  2. Better able to respond to rapidly changing clock frequency, the clock is very useful for virtual machines have unstable or cause the clock frequency is changed in terms of energy-saving technologies
  3. After the initial synchronization, it will not stop the clock in case of monotonic applications that require the system time affect
  4. In responding to temporary asymmetrical delay (for example, when large-scale download link cause saturation) provides better stability
  5. No need to poll the server periodically, so the system comprising an intermittent network connection can still be quickly synchronized clock

chrony official website: https://chrony.tuxfamily.org
chrony official document: https://chrony.tuxfamily.org/documentation.html

Installation chrony

  1. yum install chrony
  2. The two main programs: chronyd and chronyc
  3. chronyd: Daemon running in the background, the system clock and the clock running in the kernel server is used to adjust the synchronization. It determines the ratio of increase or decrease in computer time, and to compensate
  4. chronyc: Command-line user tool for monitoring the performance and configuration diversity. It may work on a computer-controlled chronyd instance, can work on a different remote computer
  5. Service unit files: /usr/lib/systemd/system/chronyd.service
  6. Listen Port: 323/udp,123/udp
  7. Profiles: /etc/chrony.conf

Profiles

/etc/chrony.conf

  1. server- it can be used for clock server, when the server reachable iBurst option, transmitting a packet instead of the usual eight of a data packet. Packet interval is usually 2 seconds to accelerate the speed of initial synchronization
  2. driftfile - calculating the actual time an increase or decrease in the ratio of time the computer will record it to a file, the system will compensate for the clock after reboot
  3. rtcsync - enable the kernel mode, the system copies the time to 11 minutes each real-time clock (RTC)
  4. allow/deny - Specify a host, a subnet or a network to allow or deny access to the server
  5. cmdallow / cmddeny - You can specify which hosts can be controlled using the command chronyd
  6. bindcmdaddress - monitor which allows chronyd interface to receive commands executed by the chronyc
  7. makestep- according to requirements by usually chronyd slow down or speed clock, so that the system gradually correct all the time offset. Under certain circumstances, the system clock may drift too fast, resulting in the adjustment process consumes very long time to correct the system clock. The instruction chronyd force when adjusting the adjustment of the system clock is greater than a certain threshold
  8. local stratum 10 - even if the server is unavailable instruction time server, also allows the time when the local standard time feedback as to the other clients

chronyc command

chronyc [OPTION]... [COMMAND]...
Interactive command

[COMMAND] significance
help Command to view more chronyc interactive command
accheck Check whether the current server can access to specific hosts
activity It shows how many NTP source online / offline
sources [-v] It displays the current time synchronization information source
sourcestats [-v] Current statistics show synchronized time source
add server Add a new NTP server manually
clients Reports have access to the server's client list
delete NTP server or manually remove the server, etc.
settime Daemon manually set time
sracking Time information display system

Examples chrony

  1. Installed and enabled chrony
yum install chrony
systemctl start  chronyd
systemctl enable chronyd
systemctl status chronyd
  1. Setting time source
[root]$ vim /etc/chrony.conf
server IP iburst
#iburst表示加急
  1. chrony as a time server
[root]$ vim /etc/chrony.conf
25 # Allow NTP client access from local network.
26 allow 192.168.0.0/16
#allow 0.0.0.0/0可以表示允许所有的网络来同步时间
  1. Local time server
    even if the time server is not time synchronization source, it may be used as a time server
[root]$ vim /etc/chrony.conf
28  # Serve time even if not synchronized to a time source.
29  local stratum 10
  1. Show time information sources currently being accessed chronyd
chronyc sources -v

Here Insert Picture Description

Time tool

  1. timedatectl
    view the date and time, time zone and NTP status: timedatectl
    When viewing zone list: timedatectl list-timezones
    when modifying zone: timedatectl set-timezone Asia / Shanghai
    change date and time: timedatectl set-time "2017-01-23 10:30:00 "
    open nTP: timedatectl set-ntp true / flase
  2. system-config-date: graphical configuration tool chrony services

NTP Public Services

  1. pool.ntp.org :
    the project is to provide a reliable and easy to use NTP service of virtual cluster ncn.pool.ntp.org , 0-3.cn.pool.ntp.org
  2. Ali cloud public NTP server
  3. University ntp service
    s1a.time.edu.cn Beijing University of Posts and
    s1b.time.edu.cn Tsinghua
    s1c.time.edu.cn Peking University
  4. National Time Service Center server
    210.72.145.44

Guess you like

Origin blog.csdn.net/weixin_42758707/article/details/94738761