4.5 Setting the Time


4.5 Setting the Time

A well-functioning Unix machine depends on accurate timekeeping. The kernel is responsible for maintaining the system clock , the clock consulted when you run commands such as date . You can also set the system clock with the date command, but this is usually a bad idea, because you'll never get the time exactly right. You want your system clock to be very close to the correct time.

PC hardware has a battery- backed "real-time" clock. It's not the greatest clock in the world, but it's better than nothing. The kernel usually sets its time based on this clock at boot time, and you can reset the system clock to the current hardware time with hwclock . If your hardware clock is in UTC (Universal Coordinated Time, or Greenwich Mean Time), run this command:

 hwclock --hctosys --utc 

You should try to keep your hardware clock in UTC to avoid any trouble with time zone or daylight savings time corrections. However, if you insist on keeping the hardware clock set to the local time, run this command instead:

 hwclock --hctosys --localtime 

Unfortunately, the kernel isn't terribly good at keeping time, and because Unix machines often stay up for months and years at a time, they tend to develop a drift . In addition, it's fairly rude to make the system clock jump ahead or back by running hwclock on a frequent basis; time-based system events can get lost or munged. If you want to gradually change the system clock based on the hardware clock, run this command:

 adjtimex --adjust 

However, this is only good for updating the clock manually when it has already drifted. To keep the time up to date, you need to run a network time daemon (see Section 4.5.2).

4.5.1 Time Zones

The system clock runs in UTC. System libraries convert this into local time and compensate for daylight savings time and any other strange circumstances (such as living in Indiana).

To set your system's time zone, make a symbolic link to the appropriate time zone file in /usr/share/zoneinfo from /etc/localtime . For example, here is the command to configure a United States Pacific Time Zone setting:

 ln -sf /usr/share/zoneinfo/US/Pacific /etc/localtime 

A long listing of the link should contain this output:

 /etc/localtime -> /usr/share/zoneinfo/US/Pacific 

If you want to use a different time zone than the system default, you can do so by setting the TZ environment variable like this:

 $ export TZ=US/Central $ date 

4.5.2 Network Time

If your machine is permanently connected to the Internet, you can run an NTP (Network Time Protocol) daemon and get the time from a remote server. The main NTP Web page is http://www.ntp.org/, but if you don't really feel like reading through the mounds of documentation there, do this:

  1. Ask your ISP for the closest NTP time server.

  2. Put that time server in /etc/ntpd.conf .

  3. Run ntpdate server at boot time.

  4. Run ntpd at boot time.

If your machine does not have a permanent Internet connection, you can use a daemon such as chronyd to maintain the time during disconnections.

HINT  

You can also set your hardware clock based on the network time. First, set your system time from the network with ntpdate (or ntpd ). Then run this command:

 hwclock --systohc -utc 



How Linux Works
How Linux Works: What Every Superuser Should Know
ISBN: 1593270356
EAN: 2147483647
Year: 2004
Pages: 189
Authors: Brian Ward

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net