BLOG // May 4, 2021
Set the date on Linux without NTP
I've recently had a situation where I need a perfect timesync on a server I was logged into, but it had very restricted outbound access. Specifically, it couldn't use NTP to sync time.
This awesome hack gets the date from a webserver (e.g. google) and sets the system time based on that.
date -s "$(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g')"
Naturally, I don't recommend this as a primary method for fixing time drift, but in a pinch it's great!
Explaining how it works
Lets break it up into it's component pieces:
date -s
sets your local time on Linux.curl -s --head http://google.com
gets the HTTP headers from Google.comgrep ^Date:
find the line in the headers with Date in itsed 's/Date: //g
removes the "Date: " part, leaving only the actual date
So what you are really doing is getting the Date from Google.com, altering the text a bit to just have the real date left, and then setting your local time based on that.
Comments
Subscribe to new articles
If you enjoy my content, consider subscribing. You will only receive new blog stories, no other email.