Last active
January 14, 2016 19:25
-
-
Save kernelsmith/b8d4eb3b1cb2cf0db687 to your computer and use it in GitHub Desktop.
easily set ubuntu system date/time and timezone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
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
should add some error checking around the wget especially. Some day.