Configuration method of using chrony for system time synchronization on ubuntu

 1. Why time synchronization is needed

Before understanding this issue, you need to understand the time concepts of RTC, CST, and UTC.

(1) RTC

In the linux system, we can also see an RTC time, which is generally called hardware time. That is, it is timed by the tick signal generated by the special clock chip hardware on the computer motherboard.

(2)UTC

Coordinated Universal Time, UTC is the standard used by different countries in the world to unify time. It can be understood as the standard time of our earth, based on the atomic time of the SI standard, which is obtained by the tick signal generated by a very high-precision cesium atomic clock. UTC is the time in the zero time zone.

(3)CST

Regarding the multiple content of CST, but a simple understanding is the specific time in the time zone of each country.

Central Standard Time (USA) UT-6:00 (US cst time: minus 6 hours in zone zero)

Central Standard Time (Australia) UT+9:30 (Australia cst: plus 9 and a half hours)

China Standard Time UT+8:00 (China cst: plus 8 hours)

Cuba Standard Time UT-4:00 (Cuba cst: minus 4 hours).
For example, when UTC time is 0 o'clock, China's Beijing time is in East 8 time zone, so China's CST time is 8 o'clock (UTC time + 8 hours).

Now we can explain why time synchronization is needed, because RTC uses a dedicated clock chip hardware on the motherboard to time. On the one hand, the starting time of RTC on each computer is different, and on the other hand, the error of RTC timing accuracy is relatively large. In order to unify everyone's time, it is necessary to allow RTC to synchronize with UTC within a period of time. It can be understood that when CCTV is reporting the time at 8 o'clock every night, each of us will synchronize the time of our watches. It's just that the synchronization cycle in the computer is much more frequent, such as once every few minutes.

Although the synchronization of RTC and UTC can solve the problem of time unification, it may be inconvenient in life if people in all parts of the world use UTC time. An example of the simplest answer is that the rising time of the sun will be different in each region, which will have a great impact on people's living habits of relying on sunrise and sunset. Therefore, it is necessary to convert UTC to the time CST of the specific time zone according to the UTC time and the time zone of the region, and then synchronize the RTC and CST.

2. Commonly used time synchronization tools

A time synchronization tool is actually a software that performs time synchronization requests between computers. Following a set of protocols, let the local computer initiate a synchronization request to the remote server that provides time synchronization function, and then the remote server that provides time synchronization function returns the synchronization time value to the local computer, and then the local computer uses the time value returned by the server to perform certain calculations to get the local time.

Currently popular time synchronization tools include ntp and chrony, ntp uses port 123/UDP for communication, and chrony uses port 323/UDP for communication. Since ntp is relatively old, chrony is used here for time synchronization. To install chrony on the ubuntu system, just run the following command:

sudo apt install chrony

Chrony is divided into server program chronyd and client program chronyc. We can use chronyd to provide time synchronization services for other computers, and we can also use chronyc to initiate time synchronization requests to other time synchronization servers. The configuration methods of the chrony server and chrony client will be introduced in turn below.

3. Configure the chrony server

If we want our chrony server to provide time synchronization services for all computers on the Internet, we need to run this chrony server on a public network server, usually using a cloud server (I chose an Alibaba Cloud server here). Since chrony uses port 323/UDP for communication, it is necessary to go to our server console to open the permission to enter the port 323/UDP of the server.

Then enter /etc/chrony/chrony.conf of the ubuntu server to configure the chrony server. Here we mainly focus on several parameters:

  • server: It is used to specify the source address of the external time synchronization server, and any number of source addresses can be added.
  • pool: used to specify the external time synchronization server pool.
  • allow: Allow client computers with specific IPs to send time synchronization requests to this chrony server. If set to allow all, it means client computers on all network segments are allowed to send requests.
  • local stratum: When the external time synchronization server is unavailable, the server's local time is used as the return value to return to the requesting client computer.

Here, the server source address can directly use the source provided by Alibaba Cloud by default, as shown below:

server ntp.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp1.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp1.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp10.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp11.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp12.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp2.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp2.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp3.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp3.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp4.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp4.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp5.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp5.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp6.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp6.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp7.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp8.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst
server ntp9.cloud.aliyuncs.com minpoll 4 maxpoll 10 iburst

For the convenience of testing, allow is configured as allow all, which means that clients on all network segments are allowed to send requests:

allow all

Here, local stratum 10 is set to allow the server's local time to be returned to the requesting client computer when the external time synchronization server is unavailable.

local stratum 10

The method of starting, stopping and checking the status of the chrony server is as follows:

#启动chrony服务
service chrony start

#重动chrony服务
service chrony restart

#查看chrony服务状态
service chrony status

#停止chrony服务
service chrony stop

4. Configure the chrony client

Corresponding to the ubuntu system client, enter /etc/chrony/chrony.conf of the ubuntu server to configure the chrony client. The configuration parameters are similar to those of the chrony server above, except that the client does not need to provide external time synchronization services but only requests data, so the allow parameter can be omitted.

Here the server source address is specified as the IP address of our chrony server above (you can check the specific IP address in your own cloud server console), you can replace the following my_chrony_test.aliyuncs.com with your own server address, as shown below :

server my_chrony_test.aliyuncs.com minpoll 4 maxpoll 10 iburst

The method of starting, stopping, and checking the status of the chrony client is the same as that of the server above, as follows:

#启动chrony客户端
service chrony start

#重动chronyd客户端
service chrony restart

#查看chrony客户端
service chrony status

#停止chrony客户端
service chrony stop

Then there are some common commands on the chrony client:

#查看可用的时间同步源
chronyc sources -v

#查看时间同步源的状态
chronyc sourcestats -v

#对客户端系统时间进行强制同步
chronyc -a makestep

Use timedatectl to manage local system time:

#修改本地系统时间
timedatectl set-time "2022-03-01 10:01:01"

#查看时区列表
timedatectl list-timezones
timedatectl list-timezones |grep Asia/S

#修改时区
timedatectl set-timezone Asia/Shanghai

#让RTC去和UTC进行同步
timedatectl set-local-rtc 0
#让RTC去和本地系统时间CST进行同步(不推荐)
timedatectl set-local-rtc 1

#启用向外部时间同步服务器发起同步的功能
timedatectl set-ntp yes
#关闭向外部时间同步服务器发起同步的功能
timedatectl set-ntp no
 

5. Summary

Assuming that the local client computer is called A, our server computer is called B, and the external network time synchronization source computer is called C, the time synchronization process is actually A->B->C->...

A initiates a time synchronization request to B to keep the time of A and B consistent;

In order to maintain its own system time, B initiates a time synchronization request to C in the same way, so that the time of B and C are consistent;

C may need to further synchronize with other external network time synchronization sources. In short, time synchronization is performed layer by layer.

Guess you like

Origin blog.csdn.net/m0_68732180/article/details/130251743