I want to change the timezone of the system clock from the terminal.
This can be done with
tzselect
"which opens a gui in terminal"
or
sudo cp /usr/share/zoneinfo/Europe/London /etc/localtime
"which will set to gmt"
But is there a way to set time zone with just the offset ie.-1 or +5 etc?
I need to do this as I'm writing an application to adjust timeoffset or report logs and the only info I have is the user IP. I can use a webapp to find the location of the IP, but then I need to set offset which would be easy IF I could just get the offset of the location, but if I need to find zone and city it would be a real pain.
If anyone knows the answer to how to set system clock with +/-hour would be great.
To change time zone from terminal, just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command(s) below:
Once open, just follow the screens to change the time zone.
You can also use the new
timedatectl
to set the time in 14.04.I realize this thread is a bit dated, but I was looking for a better solution because I needed to automatically set the timezone in a VM after a user downloads it from our website and deploys it. Here's what I ended up with:
This will query geoip.ubuntu.com from the server once it is started on the new network (my script checks for connectivity first course) and then set the server's timezone based on the response.
The "wget -q0 -" tells wget to output only the results to stdout which is then piped to the $tz variable.
Hope it helps someone!
You can use the GMT±x files in
/usr/share/zoneinfo/Etc
.TL;DR
/etc/localtime
is a symbolic link that points to a file in/usr/share/zoneinfo/Continent/City
. By altering where it points you can alter your timezone.In order to change timezone, remove the
/etc/localtime
and assign symbolic link to your desired city. For instance,ls -l /etc/localtime
reports that this file points to/usr/share/zoneinfo/America/Denver
. To change it to New York, I dosudo rm /etc/localtime
sudo ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
The screenshot bellow shows the steps and confirmation from
timedatectl
The easy way to update the timezone is to use
sudo dpkg-reconfigure tzdata
. But this isn't so easily scripted.If you want to set the timezone explicitly via a script (e.g. for setting up a VM), here's a recipe:
Set a variable for the timezone you want, e.g. one of
Valid values are the directories and filenames under
/usr/share/zoneinfo
.Then simply
Note that both
/etc/localtime
and/etc/timezone
must be consistent.Probably now reboot to get all your services updated too (e.g. cron).
Here's the method I use, which is US-centric as I don't need to worry about non-US time zones.
If I need to determine the current time zone, I either do:
or
I then do:
where
<_designation_>
is one of the following:US/Eastern
US/Central
US/Mountain
US/Pacific
US/Alaska
US/Aleutian
US/Arizona
US/East-Indiana
US/Hawaii
US/Indiana-Starke
US/Michigan
US/Pacific-New
US/Samoa
E.G.:
Other timezone designations can be found here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
You cannot do this unless you select the time zone file you want to correspond to each time zone offset.
Some answers suggested using the files in /usr/share/zoneinfo/Etc, but the problem with these is that they are DST agnostic.
So unless you pick some time zone configuration to match each offset, or you don't care about DST, there is no way to do it generically.