Skip to content

Instantly share code, notes, and snippets.

@kernelsmith
Last active January 14, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kernelsmith/b8d4eb3b1cb2cf0db687 to your computer and use it in GitHub Desktop.
Save kernelsmith/b8d4eb3b1cb2cf0db687 to your computer and use it in GitHub Desktop.
easily set ubuntu system date/time and timezone
#!/bin/sh
# change this value to suit you, see below for further guidance
DESIRED_TIME_ZONE="US/Central"
# Note, if you were to get this value from an argument etc, keep
# in mind it would be vulnerable to command injection
# get timezone values from `ls /usr/share/zoneinfo` if you need them
# for example, you can see there's a /usr/share/zoneinfo/US/Central and
# /usr/share/zoneinfo/America/Chicago etc
# you shouldn't have to change anything below here
# actually set the timezone (overwrite the symbolic link)
echo "Setting timezone ($DESIRED_TIME_ZONE)"
sudo ln -sf /usr/share/zoneinfo/$DESIRED_TIME_ZONE /etc/localtime
sudo sh -c "echo $DESIRED_TIME_ZONE > /etc/timezone"
# get the current time from the Internet so we can automate this
# ignore "too far in the future" err if you get it
echo "Getting current time from timeapi.org" 1>&2
new_time=`wget -q -O - http://www.timeapi.org/utc/now.json | cut -d '"' -f 4`
echo "Got ($new_time)" 1>&2
echo "Setting date and time" 1>&2
sudo date -s "$new_time"
@kernelsmith
Copy link
Author

should add some error checking around the wget especially. Some day.

@kernelsmith
Copy link
Author

updated to write /etc/timezone as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment