How to change the timezone in Linux

From ArticleWorld


Linux, like other Unix-like operating systems, keeps track of your current timezone. It uses it in order to display the current time correctly and, in some cases, in order to synchronize with some remote computers. There are times when you will want to change the timezone, either because you have not chosen the correct one during installation, or even while you are installing a Linux system from scratch or troubleshooting one.

Steps

Linux keeps track of the current timezone by using the /etc/localtime file. This is the file we will use to change the timezone.

1 First, log in as root and check the current timezone of your machine by running the 'date' command. You can also try to run 'clock' if 'date' is not found. This will display the current hour and the current timezone, too.

2 Enter the /usr/share/zoneinfo directory. This contains a number of files, each of which represents a timezone. Find the one that matches yours. You can use 'ls | less' or 'ls | more' to see all the files in a scrollable list.

3 You now have two options:

  • You can simply copy the appropriate file in /usr/share/zoneinfo to /etc/localtime, by using the cp command. For example, you can run 'cp Bucharest /etc/localtime' .
  • Or, you may create a symbolic link using ln. This option is probably better: a normal user can simply check his timezone by running 'file /etc/localtime'. Delete /etc/localtime first, and then create a soft link using 'ln -s /etc/localtime ./<timezone file>'

4 You can now update your system's date using rdate (if it is available, by running '/usr/bin/rdate -s time.nist.gov'). You should also set the hardware clock: '/sbin/hwclock --systohc'

Notes

You should note that, although most applications use /etc/localtime to detect the current timezone, others, like PHP, maintain their own timezone-related settings. If you notice an application that does not seem to follow your system's selected timezone, you should consult its documentation.

You should also note that this is the "hard way" of setting the timezone. This will work on any Linux system (and most UNIX systems, in fact). Many distributions come with their own set of utilities, allowing you to select the timezone easily. On Debian, you may use tzsetup. On Red Hat, you can use the Setup utility to change the timezone, if the redhat-config-date package is installed.