108.1: Maintain System TimeEdit
Candidates should be able to properly maintain the system time and synchronise the clock via NTP
Key Knowledge Areas
- Set the system date and time.
- Set the hardware clock to the correct time in UTC.
- Configure the correct time zone.
- Basic NTP configuration.
- Knowledge of using the pool.ntp.org service
System Time and the Hardware clockEdit
There are two separate clocks in a Linux system. The hardware clock (on the motherboard) runs independently of the operating system, and even when the computer is switched off. The system time is maintained by the Linux kernel and is incremented by a timer interrupt. When Linux starts up it initialises the system time from the hardware clock. Linux does not normally refer to the hardware clock again while it is running.
The hardware clock can be examined and modified with the hwclock utility. The main options are:
-r or --showprints the current times
-w or --systohcset the hardware clock to the current system time
-s or --hctosysset the system time to the current hardware clock time
--setset the hardware clock to a specified time and date
Only the superuser can change the hardware clock setting
# hwclock --set --date="10/14/2010 16:55:05"
# hwclock --show
Thu 14 Oct 2010 04:55:09 PM BST -0.673528 seconds
The system time is maintained by the kernel as the number of seconds since the beginning of 1 January 1970. This moment of time is known as “the epoch”.
Display the system time using the date command:
Tue Sep 14 10:18:33 BST 2010
The system time can also be changed with the date command. Only the superuser can do this. The syntax is:
# date 101216552010
Tue Oct 12 16:55:00 BST 2010
The computer's hardware clock is usually set to UTC regardless of the computer's geographic location. However, the times reported (and set) by the date command are usually in the local time zone of the computer. The timezone defines the offset between local time and UTC. On some linux distributions the timezone is defined in the file /etc/timezone.
In addition to UCT time some countries apply “ daylight saving” policies which add or remove an hour at a given date every year. These policies are defined by files in /usr/share/zoneinfo/. These are binary files. By copying the appropriate zone file to /etc/localtime one can establish a particular zone policy.
For example if we copy /usr/share/zoneinfo/Hongkong to /etc/localtime the next time we run date this will give us the time in Hongkong. This is because date will read /etc/localtime each time it is run.
Both the hardware clock and the system time can drift due to small errors in clock frequencies. Over time these drifts can accumulate. Clock skew between computers may cause problems with tools such as make, which examines the timestamps on files, and with authentication protocols that use timestamps to limit the validity of authentication messages.
NTP (Network Time Protocol) is a protocol that can synchronise a computer's clock with a time server, to an accuracy of a few milliseconds.
Computers that are directly updated (by an atomic clock, say) are called primary time servers and are used to update a larger number of secondary time servers. This forms a tree structure similar to the DNS structure. The root servers are on the first level or stratum, the secondary servers on the second and so on.
Configuring a client to query an NTP server:
An NTP daemon called ntpd is used to regularly query one or more remote time servers. It adjusts the rate of the kernel's clock so that it is gradually adjusted to the correct time. Abrupt changes, especially adjusting the clock backwards, are avoided. The daemon is very easy to configure. All that is needed is one or more server entries in /etc/ntp.conf pointing to a public or corporate NTP server. A list of public NTP servers can be found online at http://support.ntp.org
The time servers at pool.ntp.org use DNS “round robin” to make a random selection from a pool of time servers that have volunteered to be in the pool. This is good enough for many end users. A simple ntp.conf file to use the pool might look like this:
server 0.pool.ntp.orgserver 1.pool.ntp.org driftfile /var/lib/ntp/ntp.drift
The NTP protocol can also estimate the frequency errors of the hardware clock from a sequence of queries; this estimate is written to a file referred to by the driftfile tag.
Once ntpd is started it will itself be an NTP server providing services on port 123 using UDP.
One off adjustments:
The ntp package also provides the ntpdate tool which can be use to set the time from the command line. The -q option simply queries the server, without -q the command will set the system time.
$ ntpdate -q ntp.ubuntu.com
server 126.96.36.199, stratum 2, offset 0.009249, delay 0.05046
14 Sep 11:25:23 ntpdate: adjust time server 188.8.131.52 offset 0.009249
The following is a partial list of the used files, terms and utilities:* /usr/share/zoneinfo