Use Ntpdate synchronization time on Linux systems __linux

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

The difference between NTPD and ntpdate

Before you use it, you need to figure out what the difference is between NTPD and ntpdate in the update time. NTPD is not only a time synchronization server, he can also do the client and Standard Time server synchronization time, but also is a smooth synchronization, not ntpdate immediate synchronization, in the production environment to use the ntpdate, also is so both can not run simultaneously.

The jump of a clock can cause serious problems for some programs. Many applications rely on sequential clocks--after all, this is a common assumption that the time that is obtained is linear, and that some operations, such as database transactions, often rely on the fact that time does not jump backwards. Unfortunately, the way ntpdate adjusts time is what we call a "jump": After a time, Ntpdate uses Settimeofday (2) to set the system time, which has several obvious problems:

First, it is not safe to do so. The Ntpdate setting relies on the security of the NTP server, which allows an attacker to take advantage of some software design flaws to capture the NTP server and perform some expendable tasks with the server with which it synchronizes. Because the ntpdate is in the same way that the server following it does not know if an exception has occurred (time is different, the only way is to use the server).

Second, this is not accurate. Once the NTP server is down, the server following it will not be able to synchronize time. Unlike this, ntpd not only can calibrate the computer's time, but also can calibrate the computer's clock.

Third, this is not elegant enough. Because it is a jump, rather than a time that is faster or slower, a sequence-dependent program can fail (for example, if Ntpdate finds your time is fast, you may experience two of the same moment, which is fatal for some applications).

Thus, the only point that can make the time jump is when the computer just started, but hasn't started a lot of services yet. For the rest of the time, it is ideal to use NTPD to calibrate the clock instead of adjusting the time on the computer clock.

NTPD, during synchronization with the time server, records the oscillation frequency deviation of the BIOS timer--or the natural drift of the local Clock (drift). So even if there is a problem with the network, this machine can still maintain a fairly accurate walking.

The migration of NTP

1)./configure--prefix=your_install_directory--host=arm-linux

2) Make

3) make install

Use of NTP

1 add two lines to vi/etc/services on the client:

NTP 123/tcp

NTP 123/udp

2 If there is no localtime file in the client/etc directory, copy localtime files from/etc directory on the server to the/ETC directory of the client

3 to open the NTP process on the time server (assuming its IP address is 192.168.1.11)

/ETC/INIT.D/NTP start

4 The client starts to update the time

Ntpdate 192.168.1.11

Attention:

L If the NTP daemon is restarted, the Development Board may not be able to update the time immediately, prompting the error "No server suitable for synchronization found", This is because it takes approximately 3-5 minutes for the client to establish a normal communication connection with the server after each reboot of the NTP server, and wait for a moment to update it.

L It is possible that the firewall will block the update time, and then the "-u" parameter can be added.

Configuration of NTP

This aspect of the article on the internet there are a lot of talk is also very detailed, the main is to modify the/etc/init.d/ntp.conf file, I slightly modified, add two lines:

Server 210.72.145.44

Server cn.pool.ntp.org

And then I can sync time, and somebody added a line.

Restrict 192.168.1.1mask 255.255.255.0 nomodify

I didn't add, but still succeeded, looked down the config file has this first line

Restrict:: 1

Maybe this line contains the meaning of that line. Just a guess. A sample annotation is given below for reference only:

# 1. About the Permissions settings section
# permissions are set mainly by restrict this parameter, the main syntax is:
# Restrict IP Mask netmask_ip parameter
# where IP can be the software address, can also be default, default is similar to 0.0.0.0
# As for the paramter, there are:
# Ignore: Turn off all NTP Online Services
# Nomodify: Indicates that the client side cannot change the time parameter on the Server side, however,

# The client side can still be used for network school on the server side.
# Notrust: This client source will be treated as an untrusted domain unless authenticated
# Noquery: Do not provide client-side time queries

# Notrap: Do not provide trap this remote event login

# If the paramter is not set at all, that means the IP (or domain) has no limits.

Restrict default nomodify notrap noquery # Close all NTP requirements Packet
Restrict 127.0.0.1 #这是允许本级查询
Restrict 192.168.0.1 mask 255.255.255.0 nomodify
#在192. Servers within the 168.0.1/24 network segment can be synchronized through this NTP server
# 2. The setting of the upper host
# to set the top-level host to the main server this parameter to set, the syntax is:
# Server [ip| HOST Name] [prefer]
# The server is followed by our top time server. And if the Server parameter
# After adding perfer, that means our NTP host is mainly used as the host of the
# The corresponding of the time correction. In addition, in order to solve the transmission delay action of the update Time packet,
# so you can use Driftfile to specify our host
# The amount of time it takes to communicate with times Server can be recorded in the Driftfile
# Back in the file, for example, in the following example, our NTP server and
# The time spent cn.pool.ntp.org online is recorded in the/etc/ntp/drift file
Server 0.pool.ntp.org

Server 1.pool.ntp.org

Server 2.pool.ntp.org

Server cn.pool.ntp.org prefer

#其他设置值, you can use the default value of the system

Server 127.127.1.0 # Localclock

Fudge 127.127.1.0 Stratum 10

Driftfile/var/lib/ntp/drift
Broadcastdelay 0.008
Keys/etc/ntp/keys

To sum up, restrict is used to set access permissions that the server uses to set up the upper-level time server, driftfile to set up files for saving drift time.

Time synchronization between different machines

In order to avoid the time deviation caused by the long-term operation of the host time, it is necessary to do the work of Time Synchronization (synchronize). Linux systems typically use an NTP server to synchronize the time of a different machine. A machine that can be both an NTP server and an NTP client. In a network, it is recommended that you synchronize time with a tiered server like a DNS server. Sync time, you can use the Ntpdate command, or you can use the NTPD service. Using Ntpdate is relatively simple. The format is as follows:

[Root@linux ~]# ntpdate [-NV] [NTP Ip/hostname]
[Root@linux ~]# ntpdate 192.168.0.2
[Root@linux ~]# ntpdate time.ntp.org

But such synchronization is only mandatory to set the system time to NTP server time. If there is a problem with the CPU tick, it is not a cure. So, generally with cron command, for regular synchronization settings. For example, add in crontab:

0 * * * * */usr/sbin/ntpdate 192.168.0.1

In this way, the time will be synchronized at 12 o'clock every day. The NTP server is 192.168.0.1.

Note that NTPD has a self-protection setting: If the time difference between this machine and the source is too large, the NTPD does not run. So the new set of time server must first ntpdate from the source to get the initial value, and then start the NTPD service. After the NTPD service runs, it first synchronizes with the source server every 64 seconds, and adjusts its time gradually according to the error value measured at each synchronization, and gradually increases the interval of synchronization as the error decreases. Every beat, repeat the adjustment process.

Settings for the NTPD service

The relevant setup files for the NTPD service are as follows:

1)/etc/ntp.conf: This is the main set of NTP daemon file, is also the only set of NTP files.

2)/usr/share/zoneinfo/: The files in this directory are actually the time setting files for the major time zones, such as the Beijing region's time zone settings file in/usr/share/zoneinfo/asia/beijing. The files in this directory are related to the two files (clock and localtime) that are to be discussed below.

3)/etc/sysconfig/clock: This file is not actually included in the daemon of NTP because this is the main time zone setup file for Linux. After each boot, Linux automatically read the file to set its own system to display the default time.

4/etc/localtime: This file is "local-side time profile." In the clock file, the time set file (ZONE) is/usr/share/zoneinfo/asia/beijing, so this is the local time, at which point the Linux system will save the Beijing file as a copy. Etc/localtime file, so the future of our time display will be Beijing that time set file.

5)/etc/timezone: System time zone file

The start and observation of NTP service

Before you start the NTP service, manually correct the time for this host that provides the service. (Because the port is occupied by the server, it cannot synchronize time manually)

[Root@linux ~] # ntpdate cn.pool.ntp.org

APR 14:33:51 ntpdate[8310]: Step time server80.85.129.2 Offset 6.655976 sec

Then, start the NTPD service:

[Root@linux ~] #/ETC/INIT.D/NTP Start/restart

View Port:

[Root@linux ~] # Netstat-ln|grep 123

UDP 0 0192.168.228.153:123 0.0.0.0:*

UDP 0 0127.0.0.1:123 0.0.0.0:*

UDP 0 00.0.0.0:123 0.0.0.0:*

UDP 0 0:::123:::*

Ntptrace

[Root@linux ~] # ntptrace–n 127.0.0.1

127.0.0.1:stratum, offset 0.000000,synch distance0.950951

222.73.214.125:stratum 2,offset–0.000787,synch distance0.108575

209.81.9.7:stratum 1,offset 0.000028,synch distance0.00436,refid ' GPS '

#这个指令可以列出目前NTP服务器 (first tier) and the top NTP server (second tier) between each other

#关系

Ntpq

[Root@linux ~] # ntpq–p


The directive "Ntpq-p" can list the current state of our NTP with the associated upper NTP, the meanings of the above several fields are as follows:

Remote: The IP or host name of the NTP host. Note that the leftmost symbol, if the "+" represents the current action clock of the upper NTP, if it is "*" means that there are even on-line, but as a minor online NTP host.

refID: Reference to the address of the previous NTP host

ST: that stratum stratum

When: A few seconds ago did time sync Update action

Poll: Next update in a few seconds

Reach: The number of times the top NTP server has been asked to update

Delay: Network transmission process clock delay time

Offset: Results of time compensation

Jitter:linux time difference between system time and BIOS hardware time

System time and hardware time synchronization

     NTP service, which defaults to synchronizing only the system time. If you want NTP to synchronize hardware time at the same time, you can set up the/etc/sysconfig/ntpd file, in the/etc/sysconfig/ntpd file, add Sync_hwclock=yes so that you can synchronize the hardware time with the system time.

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.