Two detailed methods for synchronizing network time in Linux and CentOS system

  • 2020-11-30 08:46:27
  • OfStack

Due to hardware reasons, the machine is more or less inconsistent with the standard time, the error of a month in a few seconds to a few minutes. Time is not right for the server, which can cause a lot of trouble. For example, when paying, can't place an order, the game can't log in and so on.

Method 1: Update the time from the time server with ntpdate

If the system does not have the ntpdate command, it can be installed online:


yum -y install ntp

After the installation, you don't want to do any configuration, you don't need to test 1 directly


[root@snsgou-pc src]# date
2015 years  05 month  20 day   week 3 22:42:19 CST
[root@snsgou-pc src]# ntpdate time.nist.gov 
20 May 22:42:38 ntpdate[26759]: step time server 131.107.13.100 offset 2.117558 sec
[root@snsgou-pc src]# date
2015 years  05 month  20 day   week 3 22:43:17 CST

The above situation indicates successful synchronization with network time.

We can use timed tasks to synchronize our time regularly

Enter the crontab edit state with the ES17en-ES18en command and append the following timed task text


*/10 * * * * ntpdate time.nist.gov  # Domain name or IP

Represents synchronization every 10 minutes. Several time servers are recommended:


time.nist.gov
time.nuri.net
0.asia.pool.ntp.org
1.asia.pool.ntp.org
2.asia.pool.ntp.org
3.asia.pool.ntp.org

Method 2: Set up your own time server with ntp

When we build our own time server, we don't need crontab to run regularly.

1. Installation time: ntp


yum install ntp

2. Configure ntp


[root@localhost ~]# cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'
restrict default ignore  // Modifications or queries are not allowed by default ntp, And no special packets are accepted 
restrict 127.0.0.1    // Give all permissions to the machine 
restrict 192.168.1.0 mask 255.255.255.0 notrap nomodify // Give synchronization time permissions to machines on LAN machines 
server time.nist.gov prefer   // Set up the time server, plus prefer Said the priority 
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 127.127.1.0   # local clock
fudge  127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys

3. Start ntp


[root@localhost ~]# /etc/init.d/ntpd start

4. Review and test


[root@localhost ~]# netstat -upnl |grep ntpd  // Look at the schedule 
 
[root@localhost ~]# ntpq -pn  // View the synchronized server IP
 remote      refid   st t when poll reach  delay  offset jitter
==============================================================================
 50.77.217.185  .INIT.     16 u  -  64  0  0.000  0.000  0.000
 202.90.158.4  .INIT.     16 u  -  64  0  0.000  0.000  0.000
 202.71.100.89  .INIT.     16 u  -  64  0  0.000  0.000  0.000
 202.134.1.10  .INIT.     16 u  -  64  0  0.000  0.000  0.000
*127.127.1.0   .LOCL.     10 l  18  64 377  0.000  0.000  0.001
 
[root@localhost ~]# ntpstat  // Synchronous result 
synchronised to local net at stratum 11
 time correct to within 12 ms
 polling server every 512 s

remote: The IP or host name of the NTP host. Note the symbol on the far left, where a "+" represents the upper NTP of the clock currently in action, and a "*" indicates that there is also a link online, but as a secondary online NTP host.

refid: Refer to the address of the NTP host on the upper level

st: the stratum class

when: A few seconds ago I did a time sync update

poll: The next update is in a few seconds

reach: Number of times updates have been requested from the upper NTP server

delay: Time of clock delay during network transmission

offset: The result of time compensation

jitter: The difference between Linux system time and BIOS hardware time

Above is the Linux system synchronization network time in this article to talk about the two methods, I hope to help you.


Related articles: