Wednesday, March 5, 2008

Setting the Time in Linux

Setting the Time in Linux

A consistently accurate server clock is vital to the sanity of many applications that are supported by an Apache webserver environment. Fortunately, there are a number of different ways to set the date/time in any version of linux. The main thing to keep in mind is that linux keeps two separate times - software (system) time, and the hardware clock. Let's take a look at a great way to get everything synced and correct... shall we?
Yes, let's.

Configuration
Copy the file that represents the desired timezone from /usr/share/zoneinfo/ to /etc/localtime.
For example:

cp /usr/share/zoneinfo/GMT /etc/localtime

Modify the settings in /etc/sysconfig/clock using your favourite text editor.
Example:

ZONE="GMT"
UTC=true
ARC=false

Note: You can find all possible ZONEs by listing the contents of /usr/share/zoneinfo/.

Feel free to sync the time against a time server, and add an entry to the crontab to do the same.
Here's an example using cPanel's time server:

/usr/bin/rdate -s httpupdate.cpanel.net
crontab -e

0 0 * * 6 /usr/bin/rdate -s httpupdate.cpanel.net

Now that the system time is correct, sync the hardware clock against the system time:
(Notice the similarities between "systohc" and "system to hardware clock")

/sbin/hwclock --systohc

Testing
You can test the results in a few different ways:

root@server [~]# date
Fri Jul 28 17:05:08 CDT 2006
root@server [~]# clock
Fri 28 Jul 2006 05:05:10 PM CDT -0.033127 seconds
root@server [~]#

You can also test by writing a php script that prints the time:

root@server [~]# cat test.php
print( date("D M j G:i:s Y") . "\n" );
?>
root@server [~]# php test.php
Fri Jul 28 17:08:12 2006
root@server [~]#

No comments: