Your computer has two timepieces; a battery-backed one that is always running (a.k.a. the hardware, BIOS, or CMOS clock), and another that is maintained by the operating system currently running, it’s called system clock. This document explains how to set your system clock, your timezone, and other stuff related to how linux does its time-keeping.
The hardware clock is generally only used to set the system clock when your operating system boots, and then from that point until you reboot or turn off your system, the system clock is the one used to keep track of time.
On Linux systems, you have a choice of keeping the hardware clock in UTC/GMT time or local time. The preferred option is to keep it in UTC. The disadvantage with keeping the hardware clock in UTC is that if you dual boot with an operating system (like DOS) that expects the hardware clock to be set to local time, the time might be wrong in that OS.
Set your timezone
The timezone under Linux is set by a symbolic link from /etc/localtime to a file in the /usr/share/zoneinfo directory that corresponds with what timezone you are in. For example, since I’m of same timezone as Hong Kong, /etc/localtime is a symlink to /usr/share/zoneinfo/Asia/Hong_Kong. To set this up, run:
[admon@planet ~]# ls -F /usr/share/zoneinfo/ Africa/ CST6CDT Etc/ Greenwich Kwajalein Navajo SystemV/ iso3166.tab America/ Canada/ Europe/ HST Libya PRC Turkey posix/ Antarctica/ Chile/ Factory Hongkong MET PST8PDT UCT posixrules Arctic/ Cuba GB Iceland MST Pacific/ US/ right/ Asia/ EET GB-Eire Indian/ MST7MDT Poland UTC zone.tab Atlantic/ EST GMT Iran Mexico/ Portugal Universal Australia/ EST5EDT GMT+0 Israel Mideast/ ROC W-SU Brazil/ Egypt GMT-0 Jamaica NZ ROK WET CET Eire GMT0 Japan NZ-CHAT Singapore Zulu [admon@planet ~]# ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime
Replace your timezone with something like US/Pacific or Europe/Paris. Have a look in the directories under /usr/share/zoneinfo to see what timezones meet your needs.
This assumes that you are running Red Hat (or Redhat based Linux) . On older systems, you’ll find that /usr/lib/zoneinfo is used instead of /usr/share/zoneinfo.
Set the system clock
To set the system clock under Linux, use the date command. As an example, to set the current time and date to Sun Dec 13 10:02:00 AM CST 2009, type date 12131002 (note that the time is in 24 hour notation).
[admon@planet ~]# date Sun Dec 13 10:09:29 CST 2009 [admon@planet ~]# date 12131002 Sun Dec 13 10:02:00 CST 2009
You can reset system time without date information like this:
[admon@planet ~]# date -s 10:04 Sun Dec 13 10:04:00 CST 2009
If you wanted to change the year as well, just type date 121310022009. To set the seconds as well, type date 12131002.30 or date 121310022009.30. The following line is an example. When the command runs succesfully, it returns current system time:
[admon@planet ~]# date 121310212009.30 Sun Dec 13 10:21:30 CST 2009
An alternative way is to load system date and time from BIOS like this:
[admon@planet ~]# date Sun Dec 13 10:02:04 CST 2009 [admon@planet ~]# hwclock --hctosys [admon@planet ~]# date Sun Dec 13 10:09:29 CST 2009
Set the hardware clock
When Linux boots, A initialization script will run the /sbin/hwclock program to copy the current hardware clock time to the system. To set the hardware clock, a common way is to set the system clock first, and then sync the new system time to hardware clock by typing /sbin/hwclock –systohc (or /sbin/hwclock –systohc –utc if you are keeping the hardware clock in UTC).
[admon@planet ~]# date Sun Dec 13 10:29:57 CST 2009 [admon@planet ~]# hwclock --systohc [admon@planet ~]# hwclock Sun Dec 13 10:30:13 2009 -0.716300 seconds [admon@planet ~]# date Sun Dec 13 10:30:13 CST 2009
Sync local time with a time server
This can be done by an entry in root’s crontab like this:
[admon@planet ~]# crontab -l MAILTO="" 30 4,16 * * * (/usr/sbin/ntpdate -s clock.redhat.com time.nist.gov ntp.admon.org)
It means your system time will be synced with these time servers every 12 hours. If you have a cluster of machines which must share the same time, it’s suggested to build your own time server, and get other servers synchronized with it.
Instead of keeping your system time up-to-date with the world, a local time server can be configed to supply delayed time, that means you can keep your system time delayed with the public. It’s helpful for some specific environment, like if you need some delayed mysql replication servers.