26

How to know current time from internet from command line in Linux?

For example, with ntpq program?

Note: not from computer or operating system clock, but from internet?

Note: I don't want to CHANGE or to SYNC time. Just KNOW it.

30

If you use bash the following line will do the job

$ cat </dev/tcp/time.nist.gov/13

56525 13-08-21 23:07:09 50 0 0  55.6 UTC(NIST) *

It leverages the built-in network capabilites¹ of the bash shell. If you use a POSIX shell or any other shell you can use e.g. netcat.

$ nc time.nist.gov 13

56525 13-08-21 23:07:09 50 0 0  55.6 UTC(NIST) *

Both commands query the timer server on TCP port 13 of the National Institute of Standards and Technology and output the received data on stdout.

¹EDIT: From the Bash man page: Bash handles several filenames specially when they are used in redirections, as described in the following table:

/dev/tcp/host/port
If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a TCP connection to the corresponding socket.

/dev/udp/host/port
If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a UDP connection to the corresponding socket.

  • Nice, +1. Could you expand a bit on how this works? Or, since I seem to be missing a large chunk of bash knowledge here, give me a name to look this behavior up with? What is the </dev/tcp/ format? – terdon Aug 22 '13 at 0:49
  • The two minor drawbacks I see with this approach is that different time zones are not easily supported and no format can be specified in a straight-forward way. To remedy that situation, one could use the time api. For example, to query the actual time for central Europe in a more human legible way, one simply invokes: curl -G --data-urlencode "format=%D-%T\n" http://www.timeapi.org/cet/now? – Moreaki Jun 14 '14 at 21:58
  • Not sure if time.nist.gov has any rate limiting or is overloaded but I get varying results with this host. – Snowcrash May 26 '17 at 11:24
22

There are several NTP services available per distro.

If you would like to do a one time clocksync:

date -s "$(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g')"

Set system time to hardware Real-Time-Clock

hwclock -r --utc
hwclock -w --utc
hwclock -r --utc

(Note: this happens to work well with Google because they have severs all over)


If you would like to just see what time Google's nearest server sends:

date -d "$(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g')"
17

The ntpdate command can do this with the -q flag:

$ ntpdate -q 1.debian.pool.ntp.org
server 88.191.120.99, stratum 3, offset -0.015076, delay 0.06604
server 88.191.235.218, stratum 2, offset -0.000676, delay 0.06592
server 188.165.240.21, stratum 3, offset 0.001191, delay 0.07005
server 91.121.34.166, stratum 2, offset 0.000565, delay 0.06998
22 Aug 00:56:21 ntpdate[31373]: adjust time server 88.191.235.218 offset -0.000676 sec

From man ntpdate:

   -q     Query only - don't set the clock.
0

You can also obtain the time over the Internet using either HTTP using HTP which is available on many systems as htpdate which usually only has accuracy to the nearest second, or you can obtain it using TLS using tlsdate which can provide better accuracy:

tlsdate -nV google.com

Also since ntpdate is being deprecated you can use sntp (it's installed by default on MacOS and comes as part of the ntp distributions) to obtain the date much more accurately:

sntp pool.ntp.org 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.