How can we automatically set the system default timezone in Linux using the Internet? As I see it, NTP servers can update only time, but not timezone. Is there any server that can change the timezone?
-
1Are you saying like the time zone change from day light savings to standard time? – slm Sep 12 '13 at 02:26
-
Here, there are two things. 1.if there is change in country. 2. second if there is change in day light savings. – Embedded Programmer Sep 12 '13 at 02:54
-
ntp servers handle changes in daylight savings time automatically. I don't know of any servers that detect your location and update your timezone automatically (like a cell phone does). – drs Sep 12 '13 at 03:32
-
2@drs More accurately, NTP doesn't care about the time zone at all. NTP time is UTC, plain and simple, and time zone conversion is for the client to perform. A specific NTP client *might* do timezone conversion, or it might not, but since most *nix systems have the system clock set to UTC anyway, it shouldn't need to. – user Sep 12 '13 at 09:59
3 Answers
I wrote a program a while ago that does this: tzupdate.
You can see what it would set your timezone to (without actually setting it) by running tzupdate -p:
$ tzupdate -p
Europe/Malta
You can set it for real by running tzupdate as root.
$ sudo tzupdate
Europe/Malta
$ date
Thu 12 Sep 05:52:22 CEST 2013
This works by:
- Geolocating your current IP
- Getting the time zone for that location
- Updating the symlink at
/etc/localtimeto point to the zoneinfo file for that timezone
- 122,090
- 24
- 265
- 262
This is an old thread, but still relevant for kubuntu 1710 which doesn't change timezone automatically. I am using Chris Down's solution for tzupdate, but ensuring also that it gets triggered when the system tries to connect to a network (wifi or other). This assumes that you are using Network Manager.
Open a terminal first.
Install tzupdate
sudo apt install python-pip pip install -U tzupdateThen ensure that it is accessible by all users
sudo cp -R ~/.local/lib/python2.7/site-packages/. /usr/lib/python2.7/. sudo cp /home/marta_riba/.local/bin/tzupdate /usr/local/bin/tzupdateTest that it works
sudo tzupdateWe then need to do is ensure that this is accessible by all user
sudo su - tzupdatewhich drops you in as root and then checks that the command changes your timezone. Then ensure you exit to return as normal user
exitMake sure that tzupdate can be run with sudo without a password. This is needed for a later step when we trigger this from networkmanager which runs this in the background.Type this in a terminal to edit the sudoers file
sudo visudoAdd this line at the end of the file
ALL ALL=(root) NOPASSWD: /usr/local/bin/tzupdateHit CTRL-X and then press Y and then ENTER to save changes
We then ensure that NetworkManager always calls this after triggering a network connection change (e.g. when connecting to a wifi). For this we create a file called tzupdate in the dispatcher.d folder which simply calls sudo tzupdate. The file needs executable permissions to run
echo 'sudo tzupdate'| sudo tee /etc/NetworkManager/dispatcher.d/99-tzupdate sudo chmod +x /etc/NetworkManager/dispatcher.d/99-tzupdateNote that NetworkManager will call execute sudo (i.e. sudo tzupdate) which will typically prompt for a password.
There might be a better way to do this i.e. get NetworkManager to call this only when it brings a connection up and not also for down (as the above will do) - I have tried putting the script in /etc/network/if-up.d but this hasn't worked for me
To test this in Kubuntu - Change your local timezone to something else (Go to the Date plasmoid in your taskbar and right click and Select 'Adjust Date/Time' and then 'TimeZone' from the tab)
Turn off your wifi and back on - you will see that once it connects to the wifi, it seems to wait for around 30 seconds and then should change your timezone successfully.
Note that this hasn't been tested with wifi where one needs to enter a password on a webpage. Hopefully the fact that this runs at the very end of a network connection should provide enough time for a user to enter credentials - otherwise this may need further tweaking (check that a connection exists, check that you are able to connect to the internet, wait for X seconds if not able to connect to the internet and then retry Y times
- 3
- 3
- 121
- 1
- 3
-
2It's too bad that in 2018 this is still the correct answer. I believe Gnome has a package (gnome-clocks) that will handle this but in KDE there is no other good solution currently. – cardonator Mar 21 '18 at 15:19
-
You can do "if [ "$2" = "up" ]" to check if the network connection is up before running the command. The second parms is what state is being called by NM. – flamusdiu Dec 16 '18 at 14:57
It's not clear what you mean by "timezone update".
Unix and Linux keep time as the number of seconds since January 1, 1970, 00:00 UTC. The Unix system clock is therefore timezone independent. Timezones do not exist in the Linux kernel; it is only when a userspace application displays the time that the number of seconds since 1970 is interpreted using a timezone. Usually, this interpretation is done through C library functions. The timezone is selected by the TZ environment variable. The timezone definitions (which consist of a timezone name, offset from UTC, dates when daylight saving time is in effect, and the offsets during DST) are part of GNU libc, and are usually included in Linux distributions as a "tzdata" package. Therefore, updating the timezone definitions is a matter of running apt-get upgrade, yum update, or some similar operation.
Note that some software, such as Java, have their own timezone definition files, which have to be updated separately.
NTP keeps track of the number of seconds since January 1, 1900, UTC. Therefore, NTP, like the Unix kernel, has no concept of timezones.
- 5,496
- 1
- 26
- 34
-
2Timezone update, we can see "date" command will print time and date according to /etc/localtime timezone, we can change timezone manually by creating symbolic link of /usr/share/zoneinfo/America/Los_Angeles. but this is manual way of doing. I want, it should automatically update time zone of country where i am, rather than doing manually. – Embedded Programmer Sep 12 '13 at 08:47
-
In simple terms, it's for a localized time, automated timezone updates are only needed for people who travel a lot though. – Cestarian Mar 22 '19 at 16:25