How to check ntp status?

i have only ntpd, but want to know how to check if time has been synchronized?

You can add an hotplug script to react when there is an ntpd event.

For my own system I have set a logging line for the initial synchronising and the server stratum events, but not for every periodic update.

Below is example from my build, where I use hotplug to log NTP events to system log:

root@router1:~# cat /etc/hotplug.d/ntp/20-ntpd-logger
[ $ACTION = "step" ]    && logger -t ntpd Time set, stratum=$stratum interval=$poll_interval offset=$offset
[ $ACTION = "stratum" ] && logger -t ntpd Stratum change, stratum=$stratum interval=$poll_interval offset=$offset

The output in system log looks like:

root@router1:~# logread | grep ntp
Tue Mar 24 08:24:51 2020 user.notice ntpd: Time set, stratum=16 interval=32 offset=41280.463320
Tue Mar 24 08:24:52 2020 user.notice ntpd: Stratum change, stratum=3 interval=32 offset=-0.000191
Tue Mar 24 10:17:57 2020 user.notice ntpd: Stratum change, stratum=4 interval=512 offset=0.007282
Tue Mar 24 10:26:39 2020 user.notice ntpd: Stratum change, stratum=3 interval=512 offset=0.002756
Tue Mar 24 11:46:07 2020 user.notice ntpd: Stratum change, stratum=4 interval=1024 offset=0.008404
Tue Mar 24 12:01:00 2020 user.notice ntpd: Stratum change, stratum=3 interval=1024 offset=0.002724




thanks a lot, but i need to check it with C language

To my knowledge the busybox ntpd (that we use) does not offer any query interface.

You might need to create that query interface with C for busybox, author the patch for OpenWrt, and hopefully also upstream the patch to busybox...

Alternatively, use chrony ntpd, which offers a query interface if I remember right.

This alternative might be some crude "fetch time with NTP or http protocol from network and compare that to local time" type of crude check, if your need is merely to find out if the time is roughly ok. Use a C NTP library for that?

1 Like

Fourth alternative:
use the NTP hotplug to write an indicator file to /tmp at the initial NTP time setting ("step" action).
Then you can check the existence of that file from your C program.

Really like this! Exactly what I'm after, as I need to delay WireGuard (uplink) until time sync is done (or the WireGuard handshake fails).

So I created /etc/hotplug.d/ntp/20-ntpd-logger like you note, but not seeing anything in the log. Anything "special" that needs to be done ... file permissions, or registering the file somehow? I do know that time sync has been achieved (based on the date / time :laughing:).


Or do a manual sync, but watch out UDP is blocked. If you want to truly automate this, you need to include htpdate as a last resort

if ntpd -nqN -p -p >/dev/null 2>&1 || ntpd -nqN -p -p >/dev/null 2>&1; then
       #### restart wireguard

Sorry, not quite following. When WireGuard is not connected, but it's set up for ... no traffic gets through, right?


Have you got a killswitch activity on the VPN? The above command will run a NTP sync and returns true/false

Right, but if WG is up, with no handshake => the traffic (NTP sync request) just goes in the bit "trash", no? At least I have been seeing this, similar to this post.


Sort of yes. NTP Daemon can be delay to sync on reboot

You can force an manual sync and if successful restart wireguard

1 Like

Got it, thanks!