The linux method for configuring the ntp server

  • 2020-05-15 03:19:11
  • OfStack

1. Install ntp

1. Check if ntp related packages are installed.
rpm -qa | grep ntp

2. Install ntp.
yum -y install ntp

2. Explanation of parameters

ignore: close all NTP online services

nomodify: the client cannot change the time parameter of the server, but the client can adjust the network time through the server.

notrust: unless the client is authenticated, the client source will be considered a distrusted subnet

noquery: no client time query: the client cannot query the ntp server using ntpq, ntpc and other commands

notrap: does not provide trap remote login: refuses to provide pattern 6 control message trap services for matching hosts. The trap service is a subsystem of the ntpdq control message protocol for remote event loggers.

nopeer: used to prevent hosts from attempting to peer with the server and to allow fraudulent servers to control the clock

kod: send the KoD packet in case of an access violation.

3. Modify the configuration file

1. View the unmodified profile


# grep ^[^#] /etc/ntp.conf  
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

1. Settings allow any IP client to synchronize time (modified profile)


# grep ^[^#] /etc/ntp.conf  
driftfile /var/lib/ntp/drift
restrict default nomodify notrap
restrict 127.0.0.1
restrict ::1
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

2. Time synchronization is only allowed for clients in the 192.168.1.0 network segment (modified configuration file)


# grep ^[^#] /etc/ntp.conf 
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.ntp.org iburst
server 3.centos.pool.ntp.org iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

4. Start NTP service and firewall

systemctl start ntpd
systemctl enable ntpd
iptables -A INPUT -p UDP -i eno16777736 -s 192.168.1.0/24 --dport 123 -j ACCEPT
setsebool-P ntp_disable_trans 1#SELinux setting
vi /etc/sysconfig/ntpd# allows BIOS to synchronize with system time by adding the following line.
SYNC_HWCLOCK=yes

5. Testing NTP

1. Check if the NTP service is running


# netstat -tlunp | grep ntp
udp    0   0 192.168.1.101:123    0.0.0.0:*              2563/ntpd     
udp    0   0 127.0.0.1:123      0.0.0.0:*              2563/ntpd     
udp    0   0 0.0.0.0:123       0.0.0.0:*              2563/ntpd     
udp6    0   0 fe80::20c:29ff:fe7b:123 :::*                2563/ntpd     
udp6    0   0 ::1:123         :::*                2563/ntpd     
udp6    0   0 :::123         :::*                2563/ntpd 

2. Check whether the ntp server is connected to the upper ntp


# ntpstat
synchronised to NTP server (120.25.108.11) at stratum 3
  time correct to within 99 ms
  polling server every 64 s

3. Check the status of ntp server and upper ntp


# ntpq -p
   remote      refid   st t when poll reach  delay  offset jitter
==============================================================================
 news.neu.edu.cn .INIT.     16 u  -  64  0  0.000  0.000  0.000
x202.118.1.130  202.118.1.47   2 u  7  64 377 153.659  9.605 19.941
*time4.aliyun.co 10.137.38.86   2 u  10  64 377  39.666 -47.661 15.944
remote  -  Unit and top ntp the ip Or host name," + "Means first," * "Is a secondary priority 
refid  -  On the reference 1 layer ntp The host address 
st    - stratum class 
when   -  How many seconds ago the time was synchronized 
poll   -  How many seconds after the next update 
reach  -  Already upward ntp The number of times the server requested an update 
delay  -  Network latency 
offset  -  Time compensation 
jitter  -  System time and bios Time difference 

6. Configure the time synchronization client

1. Execute ntpdate:

ntpdate 192.168.1.101 #192.168.1.101 is the ntp server IP address

2. Write BIOS

hclock -w

3. crond service

vi /etc/crontab
30 8 * * * root /usr/sbin/ntpdate 192.168.1.101; /sbin/hwclock -w

4. Restart the crond service

service crond restart


Related articles: