Monday, November 2, 2009

linux - timezone/time changes

Brief clock-setting example       Ntpd daemon documentation (in depth)
Clock processes on Red Hat     Clock processes in Archlinux

The Red Hat link describes time features functionally with some commands. The Archlinux link includes specific configuration file information.
Note: "RTC" and "UTC" are similar acronyms, and of course we want to keep them straight. My device is to remember, "U is close to Z", since UTC is Zulu time (Zulu=GMT=UTC). "RTC" is the BIOS clock.

Overview of clock/timezones in Slack/Zenwalk

The hardware clock, more technically the RTC (Real Time Clock) in the BIOS, is the battery powered CMOS clock that sits on the motherboard. During startup, we can enter BIOS and set the BIOS clock directly, or we can set it through some commands (see below). The software system clock reads the hardware clock at boot time. To assist with system clock accuracy, some users also employ an ntpd daemon. After the system boots, ntpd periodically checks calibrated servers via the Web and make corrections to the system clock. At shutdown, the system clock's time is written to the hardware clock, to assist with the hardware clock's accuracy going into a powered-off state and battery power.

RTC -> System Clock (once,at boot) ->System Clock (corrected by NTPD) -> Timezone offset -> Desktop display

Linux views the hardware time in two potential ways: as carrying "local time" or to "UTC" (aka "GST", "GMT", "Zulu") time. This decision is made by the user during installation, but the file can be directly accessed and toggled any time by the (root) user. In Zenwalk, the file is /etc/hardwareclock. The file contains only one word, either "localtime" or "UTC" (minus quotes). In other Linux systems, local or UTC is noted in /etc/sysconfig/clock. Zenwalk/Linux systems use either the local or UTC when calculating the current time to display to the user.

It's up to the individual, but I set BIOS to UTC and select "UTC" in /etc/hardwareclock. With UTC: 1) my system remains consistent across both the hardware and system clocks and, 2) UTC is the time served by Internet ntp time servers. Also, when traveling, I don't needn't alter clock settings beyond changing /usr/share/zoneinfo or, eg Orage (if I want to use a GUI).

What appears when I type "date"
Neither the system clock nor hardware clock. "Date" is the system clock (taken from BIOS at boot), modified according to /etc/hardwareclock and /usr/share/zoneinfo settings. As noted above, because I keep system and hardware clocks set to Zulu, the simplest way to keep things accurate at all 3 levels (bios, system, user-display), is to run a time server update on systemclock and then send it to the BIOS:
# ntpdate
# hwclock --systohc
These commands will be explained more below, but the above command makes life easy after, say, one replaces a CMOS battery on the motherboard. Browsers won't be able to SSL lock unless the time is relatively close to the current date. For example, Chromium won't navigate to the Google home page unless the system time is within the current CA certificate, (eg, June 1, 2001 won't allow access in 2009), because Google always connects "https".

Hardware clock (RTC)
It's possible to directly set the time on one's hardware clock to whatever one wishes using hwclock commands.

To view the hardware clock:
# hwclock

To change the hardware clock to whatever the system clock currently indicates:
# hwclock --systohc

To change the system clock to whatever the hardware clock currently indicates (this is what happens at boot):
# hwclock --hctosys

System clock
The system clock periodically receives an accurate UTC/GST/GMT from ntpd . Settings are configured in /etc/ntp.conf and one can check operation with # service list. One can also force an ntp update to the system clock. Turn off ntpd, (# service stop ntpd), to free the ntp port. Google an ntp time server and run ntpdate or just go to the ntp pool, which will determine and use the nearest server:
# ntpdate

To view the system clock, for example to verify an update:
$ date

Timezone offset
Suppose I fly from Chicago to NYC or vice versa; how do I set the timezone? From the desktop, I open Orage and change the timezone there. If I want to do it without a GUI, most of the answer is in /usr/share/zoneinfo.

The file /etc/localtime is supposed to be a soft ("sym")link pointing to the correct timezone in /usr/share/zoneinfo. In my system, there was a hard file in /etc/localtime instead of a symlink. Accordingly, to be sure, I removed both files and and created a new symlink to the correct timezone. For instance, since NYC is EST, this would be the process when flying to NYC:

# rm -r /etc/localtime
# rm -r /usr/share/zoneinfo/localtime
# ln -s /usr/share/zoneinfo/US/Eastern /etc/localtime

With these three commands, and setting /etc/hardwareclock to UTC, all should be good at the next reboot.

Sometimes a system is sticky even with this. If that's the case, do all of the above and also export the time variable $TZ, to the kernel. For example in PST regions:
$ export TZ=PST8PDT

Ancillary notes
  • the directory /usr/share/zoneinfo contains the premade time zone options. It appears all potential time zones are in here.
  • the timezone file /etc/localtime appears filled with weird symbols like it's a bin file. It can't therefore be edited with a text editor.
  • the /usr/share/zoneinfo directory contains two soft links: localtime -> /etc/localtime, and timeconfig -> /usr/sbin/timeconfig. The symlink for /etc/localtime is explained above, but I'm uncertain why the timeconfig application would be linked here since it can easily be ran in any terminal.

No comments: