Location>code7788 >text

Chrony: An artifact that makes your server time accurate to microseconds!

Popularity:169 ℃/2025-01-16 16:29:29

In modern computer systems, time synchronization is crucial. Whether it's a distributed system, a database cluster, or logging, time inconsistencies can cause serious problems. andChrony, as a high-performance time synchronization tool, is becoming the first choice of more and more system administrators. It is not only better than traditionalntpdFaster, more accurate, and maintains excellent performance even when the network is unstable. Today, we’ll take a deep dive into the power of Chrony and how to use it to make your server time accurate to microseconds!


Why do you need Chrony?

In distributed systems, the importance of time synchronization is self-evident. If the time between servers is inconsistent, it may cause the following problems:

  • The log time is messed up, making it difficult to troubleshoot the problem.
  • Database transaction conflicts and data consistency is destroyed.
  • The distributed lock fails and unpredictable errors occur in the system.

And Chrony was born to solve these problems. It stands out through the following features:

  • Quick sync: Chrony can complete time synchronization in a few seconds under good network conditions.
  • High precision: Supports microsecond-level time synchronization to meet high-precision requirements.
  • Adaptable: Chrony maintains stable synchronization even in environments with network fluctuations or high latency.
  • Low resource usage: Suitable for resource-constrained devices such as embedded systems or virtual machines.

Chrony’s core strengths

1. Faster and more accurate than ntpd

One of the design goals of Chrony is to be more efficient than traditionalntpdComplete time synchronization faster. It uses intelligent algorithms andiburstoption to send multiple requests during the initial sync, significantly reducing sync time.

2. Adapt to network fluctuations

If your server is located in an environment with unstable network (such as cloud server or mobile device), Chrony will perform even better. It can dynamically adjust the synchronization strategy and reduce the impact of network fluctuations on time synchronization.

3. Support offline mode

Chrony relies on the drift rate of the local clock to maintain time accuracy even when there is no network connection. This is very useful for systems that need to run offline.

4. Low resource usage

Chrony has a very low resource footprint and is suitable for running on embedded devices or virtual machines. It has no noticeable impact on system performance.


How to configure Chrony?

Chrony's configuration files are usually located in/etc/, the following is a simple configuration example:

# Use Alibaba Cloud's NTP server
 server iburst
 server iburst

 # Allow local network access
 allow 192.168.1.0/24

 # When the time deviation exceeds 1 second, adjust immediately
 makestep 1.0 3

 # Use local clock as backup source
 local stratum 10

After the configuration is complete, start the Chrony service:

sudo systemctl start chronyd
sudo systemctl enable chronyd

Common commands in Chrony

Chrony provides a powerful command line toolchronyc, used to monitor and manage time synchronization. Here are some commonly used commands:

1. Check the time server status

chronyc sources -v
  • Displays the currently configured time servers and their status.
  • ^*Indicates the server currently in use.
  • ^+Indicates available backup servers.

Example output:

MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^*                 2   6   377    46    +12us[-123us] +/-   23ms
^+                2   6   377    45    -10us[-145us] +/-   25ms

2. Check synchronization status

chronyc tracking
  • Displays the clock synchronization status of the current system, including time deviation, frequency deviation, etc.

Example output:

Reference ID    : 123.456.789.101 ()
Stratum         : 3
Ref time (UTC)  : Tue Jan 16 12:34:56 2025
System time     : 0.000123 seconds slow of NTP time
Last offset     : +0.000045 seconds
RMS offset      : 0.000012 seconds
Frequency       : 1.234 ppm slow
Residual freq   : +0.001 ppm
Skew            : 0.012 ppm
Root delay      : 0.023456 seconds
Root dispersion : 0.001234 seconds
Update interval : 64.2 seconds
Leap status     : Normal

3. Manual time synchronization

chronyc makestep
  • Force immediate synchronization of system time, suitable for situations with large time deviations.

4. Check client access

chronyc clients
  • Displays information about clients currently connected to Chrony.

Example output:

Hostname                      NTP   Drop Int IntL Last
===============================================================================
192.168.1.100                 2      0   6   -    45
192.168.1.101                 2      0   6   -    50

5. View time server details

chronyc sourcestats -v
  • Displays time server statistics, including latency, deviation, etc.

Example output:

Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
===============================================================================
             12   7   100     +0.001      0.012    +45us    12us
            10   6   100     -0.002      0.015    -30us    15us

6. Add or remove time servers

  • Add time server:
    chronyc add server 
    
  • Delete time server:
    chronyc delete 
    

7. Check Chrony activity status

chronyc activity
  • Displays the current Chrony activity status, including the number of servers in use.

Example output:

200 OK
4 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

8. Manually adjust the time

chronyc settime "2025-1-16 12:34:56"
  • Set the system time manually (use with caution).

9. Check Chrony version

chronyc -v
  • Displays Chrony version information.

10. Restart the Chrony service

sudo systemctl restart chronyd
  • Restart the Chrony service to apply the configuration changes.

Chrony vs. ntpd: Which one is better for you?

characteristic Chrony ntpd
Sync speed faster slower
network adaptability Adapt to network fluctuations High requirements for network stability
Resource occupation Low higher
Configuration complexity Simple more complex
Offline mode support support Not supported

If your system requires fast and accurate time synchronization and may face network instability, then Chrony is undoubtedly a better choice.


Summarize

Chrony is a powerful and flexible time synchronization tool that can provide high-precision time synchronization services for your server. Whether it's a data center, cloud server, or embedded device, Chrony can handle it with ease. Through simple configuration and management, you can make the system time accurate to the microsecond level, and say goodbye to the troubles caused by time inconsistency!

If you haven’t tried Chrony yet, get started and install it now! I believe it will become another powerful tool in your system management toolbox.