Location>code7788 >text

Server time drift, how to enable Linux NTP automatic synchronization

Popularity:181 ℃/2025-04-27 15:33:01

Preface

In daily server operation and maintenance, we often default to the server time to be accurate. But the last accidentdateThe query made me realize that the server time actually drifted quietly...

This article records the entire investigation and resolution process, hoping to help friends who encounter similar problems and leave a systematic growth note for themselves.

Discover problems

I've been developing the access analysis function of StarBlog recently, but I found that the logs seem to be incorrect

So I logged in to the server and entereddateDuring the command, it was found that there was a certain error between the current time and the actual Beijing time.

[deali@server ~]# date
Sun Apr 27 14:40:53 CST 2025

Although the time difference is not large, for servers that require accurate time recording, even a few minutes of error may lead to a series of problems such as log time disorder, abnormal scheduled tasks, and failure of SSL verification.

Preliminary investigation

To further confirm the problem, under the guidance of Grandpa, I used ittimedatectl statusCommand to troubleshoot:

[deali@server ~]# timedatectl status
      Local time: Sun 2025-04-27 14:40:53 CST
  Universal time: Sun 2025-04-27 06:40:53 UTC
        RTC time: Sun 2025-04-27 06:50:29
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

You can see that although the time zone is set correctly (Asia/Shanghai),NTP not enabled (NTP enabled: no),alsoNot synchronized (NTP synchronized: no)

This means that the server time depends entirely on the system itself, and drift will inevitably occur over a long period of time.

🔎 Tips: NTP (Network Time Protocol) is a protocol used to synchronize time between network devices. It is crucial to keep the system time synchronized with world standard time.

Try enabling NTP synchronization

So, I tried to passtimedatectlTurn on NTP synchronization:

sudo timedatectl set-ntp true

Check the status again:

[deali@server ~]# timedatectl status
     NTP enabled: yes
NTP synchronized: no

Although NTP is successfully enabled (enabled: yes), the synchronization status is stillno

For a moment, I seemed to see the dawn of hope, but I was almost wiped out by the rain of reality. 🌧️

This shows that although the server has turned on the NTP synchronization switch,Not successfully synchronized. may be:

  • The server lacks the corresponding NTP client service;
  • The network cannot access the default time server;
  • It takes longer to wait for the first sync (but this is not reliable).

Using Chrony

In order to complete time synchronization quickly and stably, I chose to install a lightweight and efficient NTP client—chrony

Install

sudo yum install chrony -y
sudo systemctl enable chronyd
sudo systemctl start chronyd

Synchronize time now

sudo chronyc makestep

After execution, the system time is accurately aligned instantly without waiting!

Configure domestic NTP source

edit/etc/, replace the default server with domestic source

server  iburst
server  iburst
server  iburst

After saving, restart chronyd:

sudo systemctl restart chronyd

Verify synchronization status

chronyc tracking

You can see detailed information such as the reference time source (Reference ID), synchronization status (stratum) in the output.

at the same time,timedatectl statusShown:

NTP enabled: yes
NTP synchronized: yes

At this point, the system time synchronization problem has been basically solved.

Synchronous Hardware Clock (RTC)

Although the system time has been synchronized, after inspection, it was found that the hardware clock (RTC) is still inconsistent with the system time.

If the synchronization is not done in time, the time deviation may occur when the server restarts in the future.

You can use commands to synchronize system time to RTC:

sudo hwclock --systohc

After synchronization is completed, verify:

hwclock --show

Confirm that the hardware clock is consistent with the system time and truly achieve accurate start-up.

summary

Through this investigation and solution, I have gained the following experience:

  • The importance of NTP synchronization
    System time drift will bring about a series of chain reactions, and it is necessary to ensure accurate synchronization of server time.
  • The importance of investigation ideas
    Starting from surface problems, use appropriate tools (e.g.timedatectl) Sequential investigation without blind operation.
  • Make good use of efficient tools (Chrony)
    Compared to traditionalntpdchronyMore lightweight, flexible and fast, it is perfect for modern server environments.
  • Systemic growth behind small problems
    Every investigation seems trivial, but in essence it is polishing your problem analysis ability and system operation and maintenance skills.

🚀 If operation and maintenance are a long-term race, then every problem investigation is a small step towards professionalism.

I hope this experience can also help you who are on the road to growth.

References

  • Chrony official documentation
  • Detailed explanation of Linux timedatectl command