Get remotetime via commandline, set it to local clock with TZ
I was experimenting with the clock. Changed it on local box. After that, Google and other SSL websites stopped working so I was not able to get the exact local time (I don't wear a watch). Luckily, the phone was in another room so I fetched it and then set the time.
But what about automation, and times when I am in a data center where they don't allow phones? Is there a command-line way to query a non-SSL remote location to get UTC time?
Cherry would be for a tool that takes your local time zone and sets the local time, but that I can do too, only will be off a few seconds... but can adjust that by repeated
date -s "xxx"
calls till it's accurate.
I looked at How to get date&time using curl command?, but when I use ntpq -c 'rv 0 clock' 139.59.15.185
, I
get the following output:
139.59.15.185: timed out, nothing received
***Request timed out
and the command:
ntpq -c 'rv 0 clock' ntp.ubuntu.com
outputs:
ntp.ubuntu.com: timed out, nothing received
***Request timed out
Here are the contents of sudo nano /etc/ntp.conf
:
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
driftfile /var/lib/ntp/ntp.drift
# Leap seconds definition provided by tzdata leapfile /usr/share/zoneinfo/leap-seconds.list
# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/
statistics loopstats peerstats clockstats filegen loopstats file
loopstats type day enable filegen peerstats file peerstats type day
enable filegen clockstats file clockstats type day enable
# Specify one or more NTP servers.
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information. pool 0.ubuntu.pool.ntp.org iburst pool 1.ubuntu.pool.ntp.org iburst pool 2.ubuntu.pool.ntp.org iburst pool 3.ubuntu.pool.ntp.org iburst
# Use Ubuntu's ntp s...
ntpq
command is for querying the NTP server's status and configuration, not for querying time. The server probably doesn't allow to query its status from random clients, so you get timeouts. To query time from remote server, use ntpdate
command:
ntpdate -q ntp.ubuntu.com
will show the time difference between your local system and the ntp.ubuntu.com
server, for example:
15 Jul 18:50:09 ntpdate[25028]: adjust time server 91.189.89.198 offset -0.139723 sec
To set local time to time announced by ntp.ubuntu.com
server, use:
sudo ntpdate ntp.ubuntu.com
("sudo" is needed because setting the system time requires root privileges)