NTP Synchronization – How to Check System Time Sync Without Knowing NTP Client

ntpsynchronization

how to check whether the system time is synchronized to NTP server without knowing the NTP client used in the product?
I'm developing an application that is expected to run in both containers or standalone systems. My application has a requirement to ensure that the system time is synchronized before it attempts to perform certain operations. However, the availability of NTP/chrony package could not be guaranteed to be available in the container even though one or the other NTP client is used in the host OS.

So I'm looking for a unified way to know whether the system time is synchronized or not?

Best Answer

Applications on general purpose compute cannot know in all cases how time sync works on the hosts they run on. In a container, you do not see and cannot connect to the chronyd or ntpd running on the host, yet this keeps time just fine. Or a VM guest that relies on host time sync, also not visible. Further making a general answer difficult, there are more NTP implementations than you might think: chrony, ntp, ntpsec, openntpd, w32tm.

Often documentation of the importance of correct time is sufficient.

On some platforms, making a dependency on an ntpd starting is relatively straight forward. On RHEL, to wait for time sync systemctl enable chrony-wait and add to your systemd unit

After=time-sync.target
Requires=time-sync.target

Yet there are applications with strict time requirements. Most demanding I can think of is time stamping authorities, one of which claims standards require less than one second offset or nothing can be issued. This aggressive of a response implies the application does its own time checks.

Perhaps bundling a SNTP client, which checks NTP offsets in your application, against configurable NTP servers. Cannot check for a proper ntpd running, but can sanity check offsets regardless of how time sync works to the host.