34

I have ntpd running on a box. I want to see how the time on the box compares to the time retrieved from ntp.ubuntu.com. Is there an easy way to do this?

John Bachir
  • 2,364
  • 7
  • 29
  • 37

4 Answers4

38

You can use ntpdate to query a time server

ntpdate -q ntp.ubuntu.com

user9517
  • 115,471
  • 20
  • 215
  • 297
20

ntpq -p ntp.ubuntu.com

From man ntpq:

...
-p     Print  a list of the peers known to the server as well as a summary of their state. This is equivalent to the peers interactive command.
...

Edit: The host is timing out right now. ntpq -p pool.ntp.org will return a valid result.

jscott
  • 24,484
  • 8
  • 79
  • 100
  • 3
    ntpdate is deprecated so it's best to get in habit of using ntpq instead. However I still use ntpdate all the time myself, old habits die hard. – Phil Hollenback Jan 10 '11 at 20:43
  • 1
    @Phil Hollenback : ntpdate may be "deprecated", but it works. ntpq -p often does not. Possibly because ISPs are blocking it? Or because of the server configuration? – mivk Jan 02 '16 at 23:54
  • 2
    @mivk *"Possibly because ISPs are blocking it"* This makes no sense. It's all the same NTP protocol. – Jonathon Reinhart Jan 13 '16 at 15:06
  • 12
    @mivk @Jonathon actually, `ntpq -p` is not asking for time, it's asking for a list of peers and other vars. If the server has a `noquery` restriction it will timeout. – GnP Apr 21 '16 at 19:10
  • 5
    `ntpq -p pool.ntp.org` times out from two of my machines, while there is no such problem with `ntpdate -q pool.ntp.org`. As said by GnP, `ntpq -p` does not do what the user expects. This incorrect answer should probably be removed. Moreover [`ntpq` cannot be installed under Debian without the associated ntp daemon](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=422347)! – vinc17 Nov 16 '16 at 13:05
  • 1
    `ntpq` is not an equivalent replacement for `ntpdate -q` - `sntp` is. While ntpq can return the same information as ntpdate (and much more) it uses a completely different protocol, and thus won't always be accessible (especially if the server software isn't ntpd). `sntp` (like `ntpdate` before it) use the ntp protocol, and will work with any ntp server. – pavon Oct 12 '21 at 22:26
13

Actually, the command ntpq -c "rv 0 clock" ntp.ubuntu.com will return the time of the server in standard format : this is the result from ca.pool.ntp.org :

clock=d1b5aa9c.b8f9697a  Wed, Jun 29 2011  9:43:56.722
Chris S
  • 77,945
  • 11
  • 124
  • 216
Ion
  • 131
  • 1
  • 2
1

There are a few different options for querying an ntp server now that ntpdate is deprecated. Which is preferred is often simply a matter of which is already installed (or easy to install) on your OS:

sntp

sntp is a tool from the ntp project, and is their recommended tool for querying remote servers:

$ sntp -K/dev/null ntp.ubuntu.com
2021-10-12 16:01:48.425034 (+0700) -0.175372 +/- 0.128998 secs

The -K/dev/null is optional, and disables persisting information about the requests to a file. Normal users typically won't have access to this file, so without that option it will print some errors but will otherwise work.

chronyd

chronyd is an alternative ntp implementation and the default time server in recent RHEL and Ubuntu distributions.

$ chronyd -Q "server ntp.ubunutu.com iburst"
2021-10-27T21:55:49Z chronyd version 3.5 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
2021-10-27T21:55:49Z Disabled control of system clock
2021-10-27T21:55:53Z System clock wrong by -0.233053 seconds (ignored)
2021-10-27T21:55:53Z chronyd exiting

The -Q tells it to query the server and print results without setting the time (capital is important - lowercase will set the time!). By default it will query the servers configured in it's config file, but you can also give a config line as done above. server is used to specify a single NTP server to query, but pool and refclock could also be used. iburst configures it get an initial result more quickly.

pavon
  • 363
  • 3
  • 9