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 | 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 gets the HTTP headers from
  • grep ^Date: find the line in the headers with Date in it
  • sed 's/Date: //g removes the "Date: " part, leaving only the actual date

So what you are really doing is getting the Date from, altering the text a bit to just have the real date left, and then setting your local time based on that.


Subscribe to new articles

If you enjoy my content, consider subscribing. You will only receive new blog stories, no other email.



© 2022 Dave Blakey. All rights reserved.

Development, startup and tech tips from Gatsby to Laravel, Strapi to PHP.