Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to use ntpdate behind a HTTP proxy with authentication? In case it is not possible, are there any good alternatives?

share|improve this question
    
What OS please? –  KCotreau Jul 6 '11 at 17:09
    
Linux in my case (don't think it matters much though). –  Ton van den Heuvel Jul 6 '11 at 19:52
    
It only mattered because it was harder to find anything remotely for Windows. The key search I used was "NTP over HTTP", in case you want to search further. –  KCotreau Jul 6 '11 at 19:58
    
Thanks for the tip :) –  Ton van den Heuvel Jul 6 '11 at 19:59

7 Answers 7

up vote 3 down vote accepted

Here is a much shorter solution based on the comments we exchanged:

sudo date -s "$(curl -sD - google.com | grep ^Date: | cut -d' ' -f3-6)Z"

But as @dfc has pointed out, we'd better do it in 2 steps to ensure we get the expected output from curl:

DATE_STR="$(curl -sD - google.com | grep ^Date: | cut -d' ' -f3-6)Z"

then update the system date only when $DATE_STR is well formed:

sudo date -s "$DATE_STR"

Just in case, certain options might be needed for curl:

  • curl -x $proxy (manual)

    to explicitly set the proxy server to use, default to protocol http and port 1080

  • curl -H 'Cache-Control: no-cache'

    to explicitly disable caching.

Note:

  • google.com is preferred over www.google.com, because the former results in a 301 response, which is much smaller (569 vs 20k+ characters) but still good to use.
share|improve this answer
    
sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z" –  ryenus Sep 29 at 6:32
    
^ above is the wget version, though I personally prefer using curl. –  ryenus Sep 29 at 6:35
    
Passing unsanitized data from internet as a variable to a sudo invocation? Is it 1999? –  dfc Sep 29 at 8:16
    
@dfc, I guess it would be definitely a concern if Google is changing its http response, or it's hacked, both are less likely to happen. But you're right, the user can always run the curl/wget first to check the output, then run sudo date -s, maybe always should :-) –  ryenus Sep 29 at 8:21
    
Or just use tlsdate and not rely on nasty kludges like this. –  dfc Sep 29 at 8:23

This seems like a clear case for tlsdate.

 tlsdate: secure parasitic rdate replacement

  tlsdate sets the local clock by securely connecting with TLS to remote
  servers and extracting the remote time out of the secure handshake. Unlike
  ntpdate, tlsdate uses TCP, for instance connecting to a remote HTTPS or TLS
  enabled service, and provides some protection against adversaries that try
  to feed you malicious time information.

I do not think i have ever seen so many recommendations to use unsanitized data from internet as an argument to a sudo invocation.

Github: https://github.com/ioerror/tlsdate

share|improve this answer

Expanding on http://superuser.com/a/509620/362156

Let's assume you're in Berlin (Germany).

Then use this:

sudo TZ=Europe/Berlin date -s "$(TZ=Europe/Berlin date --date='TZ="UTC" '"$(wget -S  "http://www.google.com/" 2>&1 | grep -E '^[[:space:]]*[dD]ate:' | sed 's/^[[:space:]]*[dD]ate:[[:space:]]*//' | head -1l | awk '{print $1, $3, $2,  $5 , $6, $4 }' | sed 's/,//')")"
share|improve this answer
    
You should explain what is different in your solution in comparison to the answer by fiford_g. –  pabouk Aug 27 at 13:43

A quick and dirty solution for people behind a http proxy server:

My location is GMT+4, I can check out the current time from timeapi server with url http://www.timeapi.org/utc/in+four+hours, for more info pls checkout the website for your location.

To setup date & time I do:

time sudo date $(wget -O - "http://www.timeapi.org/utc/in+four+hours" 2>/dev/null | sed s/[-T:+]/\ /g | awk '{print $2,$3,$4,$5,".",$6}' | tr -d " " )

You can repeat the command if the initial 'time' command reports a high value...

share|improve this answer
    
Thanks for the tip, I got it even easier: sudo date -s "$(curl -s http://www.timeapi.org/utc/now)" You don't need to pay attention to the timezone if your OS is set correctly. Linux recognizes the timezone provided in the string and sets the system time appropriately. –  Melebius Apr 15 at 6:12

Expanding on the answer by carveone:

sudo date -s "$(wget -S  "http://www.google.com/" 2>&1 | grep -E '^[[:space:]]*[dD]ate:' | sed 's/^[[:space:]]*[dD]ate:[[:space:]]*//' | head -1l | awk '{print $1, $3, $2,  $5 ,"GMT", $4 }' | sed 's/,//')"
share|improve this answer
    
Caveat, this would create file 'index.html*' in the current directory. –  ryenus Feb 25 at 2:27
3  
A shorter version without file creation: sudo date -s "$(curl -sD - google.com | grep '^Date:' | tr -d '\r,' | awk '{print $2, $4, $3, $6, $7, $5}')" –  ryenus Feb 25 at 2:29
    
Note that the short version should use www.google.com since google.com is redirecting to it via 301 now with the date "stuck" –  Hansi Mar 28 at 14:19
2  
@Hansi, google.com is good to use because the response from curl -sD google.com still includes the Date: ... line, and IMHO it's even better because the redirection yields a much smaller response size (569 vs 20,058). –  ryenus Jul 3 at 6:44
    
When I made the comment the response for that command returned a day four days out of date. –  Hansi Jul 3 at 9:55

Assuming the http_proxy environment variable is set:

wget -S --spider "http://www.google.com/" 2>&1 | grep -E '^[[:space:]]*[dD]ate:' | sed 's/^[[:space:]]*[dD]ate:[[:space:]]*//'

Or use curl -I --proxy="..." "http://www.google.com/"

After all, if Google's site doesn't have its time set there's no hope.

share|improve this answer

If it is purely an HTTP proxy, it is using port 80, so the basic answer is no to that specifically. NTP uses UDP port 123. If it is a more generic proxy server, serving all ports, then maybe.

There are some programs out there that do NTP over HTTP. I do not use Linux, but this one might do it:

http://www.rkeene.org/oss/htp/ (still not sure if this will do authentication either).

I could not find one for Windows, but I will post back if I do.

share|improve this answer
    
Again for Linux, so I cannot add much other than a link: mina86.com/2010/01/16/ntp-over-http There might also be something that one of these publishes: nist.gov/pml/div688/grp40/softwarelist.cfm –  KCotreau Jul 6 '11 at 17:12
    
The NTP over HTTP link is inspiring, thanks for that! –  Ton van den Heuvel Jul 6 '11 at 19:50
    
@Ton van den Heuvel Thank you for your great accpetance rate. –  KCotreau Jul 6 '11 at 19:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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