How to install and configure NTP server and client on Linux?

Posted by marty on Mon, 14 Feb 2022 16:01:15 +0100

preface

NTP time synchronization is a time synchronization network technology.

There are many kinds of time synchronization technologies, each of which has its own characteristics, and there are great differences in the time synchronization accuracy of different technologies.

1, Chrony

1. Brief description

  • Note that NTP is a network protocol that can be used by clients to synchronize the time of ntpd, which is still faster and more accurate.

  • Two main programs: chronyd and chronyc
    chronyd: a daemon running in the background, which is used to adjust the synchronization between the system clock running in the kernel and the clock server. It determines the rate at which the computer increases or decreases time and compensates for it
    chronyc: command line user tool for monitoring performance and diversified configuration. It can work on a computer controlled by a chronyd instance or on a different remote computer

  • Service unit file: / usr / lib / SYSTEMd / system / chronyd service

  • Listening port: 323/udp, 123/udp

  • Configuration file: / etc / Chrony conf

2. Understanding of time zone concept

  • UTC: coordinated universal time, also known as world unified time, world standard time and international coordinated time. Due to the different abbreviations of English (CUT) and French (TUC), UTC is abbreviated as a compromise
  • GMT: refers to the standard time where Greenwich is located. It is also a form of indicating the rotation rate of the earth. A time measurement system based on the earth's rotation. The angle of the earth's rotation can be measured by the movement of the local meridian relative to the basic reference point on the earth.
  • CST: China standard time
  • DST: daylight saving time means that when the sun rises early in summer, the time is set forward by one hour to advance the use of sunlight (not used in China).

2, Time server

1. Software package

[root@chronyd_host ~]# Yum / / install service package synchronization time

[root@chronyd_host ~]# rpm -qc chrony 		//  Check the configuration files under the package
/etc/chrony.conf
/etc/chrony.keys
/etc/logrotate.d/chrony
/etc/sysconfig/chronyd

2. Modify profile

  • Main configuration file: / etc / Chrony conf
# Modify profile
[root@chronyd_host ~]# vim /etc/chrony.conf
allow 192.168.1.100/24			// Allow that IP or network to access NTP
allow  0.0.0.0/0		// Allow all clients to use

deny 192.168.2.10			// Deny that IP or network access to NTP

local stratum 10		// Set the number of layers of NTP server

3. Time service management and basic command

[root@chronyd_host ~]# systemctl restart chronyd 		//  Restart the service for the configuration to take effect

[root@chronyd_host ~]# systemctl enable chronyd 		//  Set startup and self startup

[root@chronyd_host ~]# timedatectl status 		//  View time synchronization status
[root@chronyd_host ~]# timedatectl set-ntp true 		//  Turn on network time synchronization

3, Client

1. Modify profile

  • Main configuration file: / etc / Chrony conf
# Modify profile
[root@localhost ~]# vim /etc/chrony.conf
server chronyd_host	iburst

allow  0.0.0.0/0		// Allow all clients to use

2. Start service

[root@localhost ~]# systemctl start chronyd 		//  Restart the service for the configuration to take effect

[root@localhost~]# systemctl enable chronyd 		//  Set startup and self startup

[root@localhost ~]# chronyc sources -v 		//  Check whether the time is synchronized
.
--
Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||

===============================================================================
^* 192.168.2.100


* :  Indicates that the current status is normal synchronization
? : Represents unreachable

3. Common commands

[root@localhost ~]# timedatectl  			//  Displays the current date and time of the system

// Set date and time 
[root@localhost ~]# timedatectl set-time "YYYY-MM-DD HH:MM:SS"
[root@localhost ~]# timedatectl set-time "YYYY-MM-DD"
[root@localhost ~]# timedatectl set-time "HH:MM:SS"

[root@localhost ~]# timedatectl set-timezone Asia/Shanghai  	//  Set time zone

Topics: Linux Operation & Maintenance server