Skip to content

Instantly share code, notes, and snippets.

@martinandersen3d
Forked from dungsaga/sync-time.sh
Created May 2, 2021 01:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinandersen3d/1276881d1109d6bf6c088c055501cb7f to your computer and use it in GitHub Desktop.
Save martinandersen3d/1276881d1109d6bf6c088c055501cb7f to your computer and use it in GitHub Desktop.
sync local clock in bash without NTP
# I want to sync the local clock but don't have access to any NTP server.
# Websites such as https://time.is give me the correct time and I can manually adjust the local clock.
# But then I recall that a HTTP response often contains a date header with current time in GMT timezone.
# I'll use it to set the system clock with the precision of 1 or 2 seconds.
# in Linux
sudo date -us "$(curl -Is google.com | grep '^Date:' | cut -d' ' -f3-)"
# in Linux with fish shell
sudo date -us (curl -Is google.com | grep '^Date:' | cut -d' ' -f3-)
# in MacOS
sudo date -uf "%d %b %Y %T" "$(curl -Is google.com | grep '^Date:' | cut -d' ' -f3-6)"
# in MacOS with fish shell
sudo date -uf "%d %b %Y %T" (curl -Is google.com | grep '^Date:' | cut -d' ' -f3-6)
# If you don't have `curl`, you can use `wget`
wget -Sq --max-redirect=0 google.com | grep '^Date:' | cut -d' ' -f3-6
# You can use another website instead of google.com (if it returns a date header with UTC time).
# WARNING: be careful, some websites have a date header with time in the past.
# Use "time" to measure which one is faster for you.
time curl -Is t.co
time curl -Is a.co
time curl -Is fb.com
time curl -Is c.cc
time curl -Is t.tt
time curl -Is yt.be
time curl -Is g.gl
time curl -Is goo.gl
time curl -Is gogle.com
time curl -Is gooogle.com
time curl -Is googl.com
time curl -Is googlee.com
time curl -Is ggoogle.com
time curl -Is goolge.com
time curl -Is googel.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment