ntpstat Command in Linux



The ntpstat command is a diagnostic tool that provides information about the synchronization state of the Network Time Protocol (NTP) daemon running on the local machine. The command provides a clear indication of the synchronization status, which helps in diagnosing potential issues with timekeeping and ensuring system reliability.

The ntpstat command checks whether the system's clock is synchronized with an external time source, typically an NTP server, ensuring the local system's time is accurate and consistent with a standard time reference.

Table of Contents

Here is a comprehensive guide to the options available with the ntpstat command −

Introduction to ntpstat Command

The primary function of ntpstat is to report on whether the system's clock is synchronized to a reference time source. When executed, it checks the NTP daemon (ntpd) and assesses the state of the time synchronization. If synchronization is successful, ntpstat will report that the system is synchronized, along with the approximate accuracy of the time, offering insight into how close the local system's time is to the reference time source.

The ntpstat command returns three possible exit codes to indicate the state of time synchronization.

  • A return value of 0 means that the clock is synchronized, indicating the system's time is aligned with the reference source.
  • A return value of 1 means that the clock is not synchronized, which might suggest issues such as an unreachable NTP server or a failure in the NTP daemon.
  • A return value of 2 indicates an indeterminate state, meaning the ntpd service cannot be contacted, possibly due to network issues or the service being down.

When the system is synchronized, ntpstat provides additional information about the accuracy of the time. It offers a rough estimate of how accurate the system's time is compared to the reference time source. This can be crucial for applications or services that depend on precise timing, such as logging, authentication protocols, or scheduled tasks that require exact timing.

Syntax of ntpstat Command

The syntax for the ntpstat command is quite straightforward, as it doesn't require any options or arguments −

ntpstat

Examples of ntpstat Command in Linux

The following examples illustrate the practical use of the ntpstat command −

Checking Synchronization Status

To check the synchronization status of the local machine, simply run the following command −

sudo ntpstat

This command checks the synchronization status of the local machine. The output shows system clock is synchronized, but the exact NTP server is not specified in the output. Stratum 2 indicates the hierarchical level of the time source.

  • time correct to within 187 ms − This indicates that the system clock is accurate within 187 milliseconds.
  • polling server every 64 s − This shows that the system is polling the NTP server every 64 seconds to maintain synchronization.
ntpstat Command in Linux1

Unsynchronized State

To check if the system is in an unsynchronized state, use the following command −

sudo ntpstat

In this example, the ntpstat command reports that the system is not synchronized. The output indicates that the time server is polling the server every 8 seconds to try and re-establish synchronization.

ntpstat Command in Linux2

Indeterminate State

To determine if the system is in an indeterminate state, you can run the following command −

sudo ntpstat

This example shows the command returning an indeterminate state. This occurs when the ntpd (NTP daemon) is not contactable or there are issues preventing the determination of the synchronization status.

ntpstat Command in Linux3

Conclusion

The ntpstat command is an essential tool for monitoring and troubleshooting time synchronization on Unix and Linux systems. By providing clear and concise information about the synchronization status of the NTP daemon, it helps administrators ensure that system time remains accurate and aligned with a reference time source.

The regular use of ntpstat command can help prevent issues that arise from inaccurate system time, such as logging discrepancies, cron job failures, and potential security risks

Whether the system is synchronized, unsynchronized, or in an indeterminate state, ntpstat provides valuable insights, making it easier to detect and address any problems with timekeeping services.

Advertisements