Wednesday, July 07, 2010

Set timezone using /etc/localtime configuration file in Linux

Often /etc/localtime is a symlink to the file localtime or to the correct time zone file in the system time zone directory.


1 Log in as root, check which timezone your machine is currently using by executing `date`.
You'll see something like Thu 10 Jun 2010 12:15:08 PM IST, IST in this case is the current timezone.

2 Change to the directory /usr/share/zoneinfo here you will find a list of time zone regions. Choose the most appropriate region, if you live in Canada or the US this directory is the "America" directory.

3 If you wish, backup the previous timezone configuration by copying it to a different location. Such as
#mv /etc/localtime /etc/localtime-old

4 Create a symbolic link to the appropriate timezone from /etc/localtime. Example:
#ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime

5 Some distro use /usr/share/zoneinfo/dirname/zonefile format (Red hat and friends)
# ln -sf /usr/share/zoneinfo/EST localtime

6 Set the ZONE entry in the file /etc/sysconfig/clock file (e.g. "America/New_York")

7 Set the hardware clock by executing:
#/sbin/hwclock --systohc

Using NTP (Network Time Protocol)

NTP will connect to a server to get the atomic time. It can be downloaded from www.ntp.org/downloads.html To get started with NTP simply download it, install it, use the ntpdate command followed by a public time server, and update your hardware clock.
$ ntpdate "0.pool.ntp.org (0, 1, or 2)"
4 Nov 22:31:28 ntpdate[26157]: step time server 209.81.9.7 offset 22317290.440932 sec
$ hwclock --systohc

To keep your time accurate you can create a cron job that executes:
(the -w option is the same as --systohc)
#ntpdate "server name" && hwclock -w

No comments: