Wednesday, May 29, 2013

Setting Time Zone in CentOS 5

Check the current timezone
# cat /etc/sysconfig/clock
ZONE=”America/Toronto”
UTC=true
ARC=false
There are two ways to change the timezone on CentOS.

1. Set the timezone by running system-config-date command
Before running the system-config-date tool, I created a file named “timestamp” as timestamp.
# date > timestamp
Type the system-config-date command at the command line to start the time and date properties tool.
# system-config-date
I selected “America/Vancouver” and press “OK” to exit.
Use the following command to find which configuration files had been changed.
# find /etc –newer timestamp
/etc/sysconfig/clock
/etc/localtime

From the above output, I know /etc/sysconfig/clock and /etc/localtime had been changed. Open the file /etc/sysconfig/clock, I found the “ZONE” had been changed. And I found the /etc/localtime had the same size with the file “/usr/share/zoneinfo/America/Vancouver”.

2. Set the timezone using /etc/sysconfig/clock and /etc/localtime configuration files
Since I know which files affect the timezone, I can modify manually. Use “vi” to modify the file /etc/sysconfig/clock, and change the “Vancouver” back to “Toronto”. Then copy “/usr/share/zoneinfo/America/Toronto” to /etc/localtime or use symbolic link command as below.
# ln -sf /usr/share/zoneinfo/America/Toronto /etc/localtime

Write the current System Time to the Hardware Clock
After changed the timezone, don’t forget to check the date and time using the date command, and then write the current System Time to the Hardware Clock.
# hwclock —systohc

Display date and time according to timezone
You can use TZ environment variable to display date and time according to your timezone. For example, I can display the local time of Los Angeles as below.

$ export TZ=America/Los_Angeles
$ date

There are two clocks on Linux system, which one is the Hardware Clock and the other one is the System Time. The Hardware Clock keeps running after the system shutdowns. When a Linux system boots, the script /etc/rc.d/rc.sysinit will run the /sbin/hwclock program to set the System Time from the Hardware Clock. When you want to set the Hardware Clock, you should use the date command to set the System Time first, and then set the Hardware Clock to the current System Time with the command /sbin/hwclock –systohc.

No comments:

Post a Comment