Setting time zone
In /etc the file, localtime, is a link to or copy of a file containing information about your time zone. Zone information files are usually in /usr/share/zoneinfo but this depends on your distribution. So if your localtime file points to a zone info file that is not your time zone you can change it by browsing the directories in /usr/share/zoneinfo to find your country, then find your city or a city in the same time zone and link localtime to it.
|$ ln -sf /usr/share/zoneinfo/America/NYC /etc/localtime|
Some applications may use the configuration file /etc/sysconfig/clock to determine the current time zone so it’s a good idea to set the ZONE entry (e.g. “America/NYC”).
Changing the date and time
Changing the date and time requires two steps. First, Linux’s date and time must be changed and then the new time has to be written to the hardware clock.
The date command can be used for both viewing and changing the date and time.
To change the time use date followed by the month, day, hour, minute, and year all numeric and no spaces. So, to set the date and time to November 2nd, 2003 12:57The hardware clock can be updated in UTC (coordinated universal time) or your local time. It is standard practice to update it in UTC.
To update it to your local time leave off the –utc or add –localtime and leave off the –utc.
The date and time can be changed directly to the hardware clock and then used to update the system clock.
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 “server DNS name or IP address”
4 Nov 22:31:28 ntpdate: step time server 220.127.116.11 offset 22317290.440932 sec
$ hwclock –systohc
A public time server can be found at http://support.ntp.org/bin/view/Servers/WebHome
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|
To stay independent of a particiluar server you can use 0.pool.ntp.org (0, 1, or 2) for the server name. This domain uses DNS round robin to choose different time servers every so often. This keeps certain nameservers from having high loads. The only disadvantage is the increased potential of updating time from a nameserver who is in the pool but has an incorrect time settings.
These are volunteer public servers so be polite, do not constantly access the public servers, use only public servers (not private), and if you have multiple machines, set up a ntp server and have your other machines retrieve the time from your local server. Checkhttp://www.eecis.udel.edu/~mills/ntp/servers.html for detailed rules.
rdate is a different package that is used in the same way that ntpdate is. It doesn’t support running your own time server like NTP does, but can access the same time servers.
|rdate “server name” && hwclock -w|
Use the following syntax to set new data and time:
For example, set new data to 2 Oct 2006 18:00:00, type the following command as root user:
# date -s "2 OCT 2006 18:00:00"
# date --set="2 OCT 2006 18:00:00"
You can also simplify format using following syntax:
# date +%Y%m%d -s "20081128"
Linux Set Time
To set time use the following syntax:
# date +%T -s "10:13:13"
- 10: Hour (hh)
- 13: Minute (mm)
- 13: Second (ss)
Use %p locale’s equivalent of either AM or PM, enter:
# date +%T%p -s "6:10:30AM"
# date +%T%p -s "12:10:30PM"