tikkat
tikkat

Reputation: 45

How to find the date using internet (ie ntp) from bash?

How can I learn date and time from the internet using bash without installing anything extra.

I am basically looking for an equivalent of bash $ date, but using an NTP (or any other way) to get the correct date and time from the internet. All the methods I find (such as ntpd) are meant to correct the system time, which is not my purpose.

Upvotes: 2

Views: 7194

Answers (2)

user3788685
user3788685

Reputation: 3113

If you have ntpd installed & configured then you can use the NTP Query command ntpq -crv which will return;

associd=0 status=04ff leap_none, sync_uhf_radio, 15 events, stale_leapsecond_values,
version="ntpd [email protected] Mon Feb  6 07:22:46 UTC 2017 (1)",
processor="x86_64", system="Linux/4.10.13-1.el6.elrepo.x86_64", leap=00,
stratum=1, precision=-23, rootdelay=0.000, rootdisp=1.000, refid=PPS,
reftime=dd2c9f10.f25911ee  Wed, Aug  2 2017 19:57:20.946,
clock=dd2c9f11.f4251b0a  Wed, Aug  2 2017 19:57:21.953, peer=6516, tc=4,
mintc=3, offset=-0.005, frequency=-17.045, sys_jitter=0.110,
clk_jitter=0.007, clk_wander=0.003, tai=37, leapsec=201701010000,
expire=201706010000

You want the line starting clock which gives the time, date etc - you would be best parsing this out with awk or something if you just want the date stamp rather then everything else.

You do not need to be a root user to run the command. It won't set anything, but will query your local server (presuming your running ntp) and present the details.

Upvotes: 1

Ljm Dullaart
Ljm Dullaart

Reputation: 4979

date has a lot of options for formatting, but I'm assuming that you just want the date and time:

ntpdate -q time.google.com | sed -n 's/ ntpdate.*//p'

(or any other time server)

Upvotes: 3

Related Questions