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

command linelinuxntp

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.

Best Answer

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.

Related Question