Time-zone Setting in Linux and BSDs from Shell

Often the default time-zone in a linux and bsd system does not match our preferences. On a system which we have installed ourself, we may have selected the appropriate time-zone during installation, but as systems administrators we often get our hands on a system which is pre-installed and after taking over we want to change the time-zone to something which we are comfortable understanding and co-relating various system events in the time of our comfort.

The time-zone of  the system is determined by a file called “/etc/localtime”, which a binary file. In order to change the time-zone, we need to replace this file with an appropriate file of our time-zone. All time-zone files are found in “/usr/share/zoneinfo”.

On some systems “/etc/localtime” is a copy and in some cases a hard link of one of the time-zone found in “/usr/share/zoneinfo” directory. In OpenBSD, “/etc/localtime” is a symlink to one of the files in “/usr/share/zoneinfo”. I prefer the symlink approach, you can pick any of the methods to make appropriate “/etc/localtime” file available.

In order to change the time-zone of my system from UTC to IST, I did the following.

$ sudo ln -sf/usr/share/zoneinfo/Asia/Kolkata /etc/localtime

In case you accidentally delete the “/etc/localtime” file, the timezone of the system reverts to UTC and upon having the correct file present again, it will reflect the correct timezone again. See below (I did this on a RHEL 6 machine) –

$ date
Wed Dec 18 13:56:05 IST 2013
$ sudo rm -f /etc/localtime
$ date
Wed Dec 18 08:36:29 UTC 2013
$ ls -l /etc/localtime
ls: cannot access /etc/localtime: No such file or directory
$ sudo ln -sf /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
$ date
Wed Dec 18 14:07:03 IST 2013

 

This entry was posted in FLOSS, Tips/Code Snippets and tagged , , , , , , . Bookmark the permalink.

Leave a Reply