The chrony suite

This manual describes how to use

the programs chronyd and chronyc

Richard P. Curnow


Introduction

Overview

Chrony is a software package for maintaining the accuracy of computer system clocks. It consists of a pair of programs :

Acknowledgements

The chrony suite makes use of the algorithm known as RSA Data Security, Inc. MD5 Message-Digest Algorithm for authenticating messages between different machines on the network.

In writing the chronyd program, extensive use has been made of RFC1305, written by David Mills. I have occasionally referred to the xntp suite's source code to check details of the protocol that the RFC did not make absolutely clear. The core algorithms in chronyd are all completely distinct from xntp, however.

Availability

Getting the software

Links on the chrony home page describe how to obtain the software.

Platforms

Although most of the program is portable between Unix-like systems, there are parts that have to be tailored to each specific vendor's system. These are the parts that interface with the operating system's facilities for adjusting the system clock; different operating systems may provide different function calls to achieve this, and even where the same function is used it may have different quirks in its behaviour.

So far, the software is able to run in the following environments:

Closely related systems may work too, but they have not been tested.

Porting the software to other system (particularly to those supporting an adjtime system call) should not be difficult, however it requires access to such systems to test out the driver.

Relationship to other software packages

xntpd

The `reference' implementation of the Network Time Protocol is the program xntpd, available via The NTP home page.

xntpd is designed to support all the operating modes defined by RFC1305, and has driver support for a large number of reference clocks (such as GPS receivers) that can be connected directly to a computer, thereby providing a so-called 'stratum 1' server.

Things chronyd can do that xntpd can't:

Things xntpd can do that chronyd can't:

timed

timed is a program that is part of the BSD networking suite. It uses broadcast packets to find all machines running the daemon within a subnet. The machines elect a master which periodically measures the system clock offsets of the other computers using ICMP timestamps. Corrections are sent to each member as a result of this process.

Problems that may arise with timed are :

timed does have the benefit over chronyd that for isolated networks of computers, they will track the `majority vote' time. For such isolated networks, chronyd requires one computer to be the `master' with the others slaved to it. If the master has a particular defective clock, the whole set of computers will tend to slip relative to real time (but they will stay accurate relative to one another).

Distribution rights and (lack of) warranty

{@tensf

This is the licence for the programs "chronyd" and "chronyc". Their source code files which reference this licence, object files / programs (binary form) built from such source code, and the supporting documentation are collectively referred to in this licence as "the Software".

Certain source code files required for the construction of the Software may be included under different licensing arrangements; you should refer to such source code files directly for further information. Any such files are outside the scope of this licence.

Person(s) and/or organisation(s) holding the copyright to any part of the source code to which this licence applies are referred to in this licence as "the Copyright Holder(s)".

Copying and use of this licence itself, with or without modification, are permitted for any purpose subject only to condition 6 below.

Copying, use and redistribution of the Software in source and/or binary forms, with or without modification, and use of any part of the Software in the creation of another work, are permitted without fee subject to the following conditions 1 through 8 and the disclaimer below :

  1. Redistributions of the Software as source code, in whole or in part, with or without modification, must retain all existing copyright notices and references to this licence, and must be accompanied by a copy of this licence. You may rename the file containing this licence and change all references to it accordingly if you need to. Such redistributions must be under terms that require this list of conditions and the disclaimer below to be carried on any subsequent redistribution.
  2. Redistributions in binary form (of the original version of the Software, a modified version of the Software, or any other work which incorporates any part of the Software), must carry in accompanying documentation and/or other materials a notice to the effect of either (A) or (B) below.
    1. This original version of this software was written by <name(s) of Copyright Holder(s)> and is used subject to the disclaimer in the file <name of file containing the disclaimer>.
    2. The parts of this software which <describe their role in your software> were originally written by <name(s) of Copyright Holder(s)> and are used subject to the disclaimer in the file <name of file containing the disclaimer>.
    (You must replace each <...> with the appropriate information.) A copy of the disclaimer below must also be included. Such redistributions must be under terms that require the relevant notice (A) or (B) and the disclaimer below to be carried on any subsequent redistribution. As an exception, if all recipients of your distribution receive additional rights under condition 5, and those rights supersede the disclaimer in its entirety, then you need not include the disclaimer, any reference to it, or the requirement for further redistributions to carry it.
  3. Unless modifications to the original Software are retained for the exclusive personal use of the modifier, each source code file that has been modified must identify the original source version and carry a notice describing the nature and purpose of all modifications and the person responsible for them. Where modified source code is widely redistributed (e.g. outside the modifier's own organisation), it should preferably be supplied as the original version plus `patches' which can be applied to the original version to obtain the modified version.
  4. If a modified version of the Software is distributed in any form, it must not be misrepresented (whether by the method of distribution or by any related advertisement or documentation) as being the original version. Both the documentation accompanying the software and any advertisement related to the distribution must expressly state that the distributed software is not the original work, and must identify both the original work (by name and version) and the nature and purpose of all modifications made.
  5. You may offer to any party (and charge a fee for) additional warranty, liability rights and/or support for the Software (or a work derived from it). Any such offer made must be entirely on your own behalf, and you must indemnify the Copyright Holder(s) and all other parties through whose redistributions you have received the Software for any liability arising out of such an offer.
  6. This list of conditions and the disclaimer below, when applied either to the Software as a whole or to any part of the Software, may only be modified by or with the express permission of the Copyright Holder(s).
  7. The Copyright Holder(s) retain all rights to the Software, including without limitation the rights to use the Software for any purpose, to create modified and/or enhanced versions of the Software, and to distibute such versions under terms different to those in this licence.
  8. Except as required by this licence, the name(s) of the Copyright Holder(s) may not be used to endorse or promote products derived from the Software without their express permission obtained in advance.

== Start of disclaimer ==

BECAUSE THE SOFTWARE IS PROVIDED FREE OF CHARGE, THERE IS NO WARRANTY FOR IT, TO THE MAXIMUM EXTENT THAT APPLICABLE LAW ALLOWS. UNLESS OTHERWISE STATED IN WRITING OR REQUIRED BY APPLICABLE LAW, THE COPYRIGHT HOLDER(S) AND/OR OTHER PARTIES PROVIDE THE SOFTWARE `AS IS' AND DISCLAIM ALL WARRANTIES, EITHER EXPRESSED OR IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, MERCHANTABLE OR FIT FOR A PARTICULAR PURPOSE. THE USER BEARS THE ENTIRE RISK AS TO THE QUALITY, ACCURACY AND PERFORMANCE OF THE SOFTWARE AND THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION IN THE EVENT THAT THE SOFTWARE SHOULD PROVE DEFECTIVE.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE, BE LIABLE TO YOU OR TO ANY OTHER PARTY FOR DAMAGES OF ANY CHARACTER, INCLUDING WITHOUT LIMITATION ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES WHICH ARISE FROM THE USE OF OR INABILITY TO USE THE SOFTWARE (INCLUDING WITHOUT LIMITATION LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH COPYRIGHT HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

== End of disclaimer ==

}

Installation

The software is distributed as source code which has to be compiled. The source code is supplied in the form of a gzipped tar file, which unpacks to a subdirectory identifying the name and version of the program.

After unpacking the source code, change directory into it, and type

./configure

This is a shell script that automatically determines the system type. There is a single optional parameter, --prefix which indicates the directory tree where the software should be installed. For example,

./configure --prefix=/opt/free

will install the chronyd daemon into /opt/free/sbin and the chronyc control program into /opt/free/bin. The default value for the prefix is /usr/local.

If the software cannot (yet) be built on your system, an error message will be shown. Otherwise, the files `options.h' and `Makefile' will be generated.

Now type

make

to build the programs.

Once the programs have been successfully compiled, they need to be installed in their target locations. This step normally needs to be performed by the superuser, and requires the following command to be entered.

make install

Now that the software is successfully installed, the next step is to set up a configuration file. The contents of this depend on the network environment in which the computer operates. The Debian package installs a simple configuration file suitable for a dial-up pc. You should edit it to suit your situation. Typical scenarios are described in the following section of the document.

Typical operating scenarios

Computers connected to the internet

In this section we discuss how to configure chrony for computers that have permanent connections to the internet (or to any network containing true NTP servers which ultimately derive their time from a reference clock).

To operate in this mode, you will need to know the names of the NTP server machines you wish to use. You may be able to find names of suitable servers by one of the following methods:

Assuming that you have found some servers, you need to set up a configuration file to run chrony. The (compiled-in) default location for this file is `/etc/chrony.conf'. In the Debian package the configuration files are in the directory `/etc/chrony'. Assuming that your ntp servers are called a.b.c and d.e.f, your `chrony.conf' file could contain as a minimum

server a.b.c
server d.e.f
server g.h.i

However, you will probably want to include some of the other directives described later. The following directives will be particularly useful : driftfile, commandkey, keyfile. The smallest useful configuration file would look something like

server a.b.c
server d.e.f
server g.h.i
keyfile /etc/chrony.keys
commandkey 1
driftfile /etc/chrony.drift

Infrequent connection to true NTP servers

In this section we discuss how to configure chrony for computers that have occasional connections to the internet.

Setting up the configuration file for infrequent connections

As in the previous section, you will need access to NTP servers on the internet. The same remarks apply for how to find them.

In this case, you will need some additional configuration to tell chronyd when the connection to the internet goes up and down. This saves the program from continuously trying to poll the servers when they are inaccessible.

Again, assuming that your ntp servers are called a.b.c and d.e.f, your `chrony.conf' file would need to contain something like

server a.b.c
server d.e.f
server g.h.i

However, the following issues need to be addressed:

  1. Your computer probably doesn't have DNS access whilst offline to turn the machine names into IP addresses.
  2. Your computer will keep trying to contact the servers to obtain timestamps, even whilst offline. If you operate a dial-on-demand system, things are even worse, because the link to the internet will keep getting established.

For this reason, it would be better to specify this part of your configuration file in the following way:

server 1.2.3.4 offline
server 5.6.7.8 offline
server 9.10.11.12 offline

Because numeric IP addresses have been used, the first problem is overcome. The offline keyword indicates that the servers start in an offline state, and that they should not be contacted until chronyd receives notification that the link to the internet is present.

In order to notify chronyd of the presence of the link, you will need to be able to log in to it with the program chronyc. To do this, chronyd needs to be configured with an administrator password. The Debian package puts a default key in `/etc/chrony/chrony.keys'. You should change it. To set up an administrator password, you can create a file `/etc/chrony.keys' containing a single line

1 xyzzy

and add the following line to `/etc/chrony.conf' (the order of the lines does not matter)

commandkey 1

The smallest useful configuration file would look something like

server 1.2.3.4 offline
server 5.6.7.8 offline
server 9.10.11.12 offline
keyfile /etc/chrony.keys
commandkey 1
driftfile /etc/chrony.drift

The next section describes how to tell chronyd when the internet link goes up and down.

How to tell chronyd when the internet link is available.

To use this option, you will need to configure a command key in chronyd's configuration file `/etc/chrony.conf', as described in the previous section.

To tell chronyd when to start and finish sampling the servers, the online and offline commands of chronyc need to be used. To give an example of their use, we assume that pppd is the program being used to connect to the internet, and that chronyc has been installed at its default location `/usr/local/bin/chronyc'. We also assume that the command key has been set up as described in the previous section.

In the file `/etc/ppp/ip-up' we add the command sequence

cat <<EOF | /usr/local/bin/chronyc
password xyzzy
online
EOF

and in the file `/etc/ppp/ip-down' we add the sequence

cat <<EOF | /usr/local/bin/chronyc
password xyzzy
offline
EOF

The Debian package puts scripts similar to those above in the directories `/etc/ppp/ip-up.d' and `/etc/ppp/ip-down.d'.

chronyd's polling of the servers will now only occur whilst the machine is actually connected to the Internet.

Isolated networks

In this section we discuss how to configure chrony for computers that never have network conectivity to any computer which ultimately derives its time from a reference clock.

In this situation, one computer is selected to be the master timeserver. The other computers are either direct clients of the master, or clients of clients.

The rate value in the master's drift file needs to be set to the average rate at which the master gains or loses time. chronyd includes support for this, in the form of the manual directive in the configuration file and the settime command in the chronyc program.

If the master is rebooted, chronyd can re-read the drift rate from the drift file. However, the master has no accurate estimate of the current time. To get around this, the system can be configured so that the master can initially set itself to a `majority-vote' of selected clients' times; this allows the clients to `flywheel' the master across its outage.

A typical configuration file for the master (called master) might be (assuming the clients are in the 192.168.165.x subnet and that the master's address is 192.168.169.170)

driftfile /etc/chrony.drift
commandkey 25
keyfile /etc/chrony.keys
initstepslew 10 client1 client3 client6
local stratum 8
manual
allow 192.168.165

For the clients that have to resynchronise the master when it restarts, the configuration file might be

server master
driftfile /etc/chrony.drift
logdir /var/log/chrony
log measurements statistics tracking
keyfile /etc/chrony.keys
commandkey 24
local stratum 10
initstepslew 20 master
allow 192.168.169.170

The rest of the clients would be the same, except that the local and allow directives are not required.

The home PC with a dial-up connection

Assumptions/how the software works

This section considers the home computer which has a dial-up connection. It assumes that Linux is run exclusively on the computer. Dual-boot systems may work; it depends what (if anything) the other system does to the system's real-time clock.

Much of the configuration for this case is discussed earlier (see section Infrequent connection to true NTP servers). This section addresses specifically the case of a computer which is turned off between 'sessions'.

In this case, chronyd relies on the computer's real-time clock (RTC) to maintain the time between the periods when it is powered up. The arrangement is shown in the figure below.

            trim if required                          PSTN
      +---------------------------+               +----------+
      |                           |               |          |
      v                           |               |          |
+---------+                    +-------+       +-----+     +---+
| System's|  measure error/    |chronyd|       |modem|     |ISP|
|real-time|------------------->|       |-------|     |     |   |
|  clock  |   drift rate       +-------+       +-----+     +---+
+---------+                       ^                          |
      |                           |                          |
      +---------------------------+                  --o-----o---
         set time at boot up                           |
                                                  +----------+
                                                  |NTP server|
                                                  +----------+

When the computer is connected to the Internet (via the modem), chronyd has access to external NTP servers which it makes measurements from. These measurements are saved, and straight-line fits are performed on them to provide an estimate of the computer's time error and rate of gaining/losing time.

When the computer is taken offline from the Internet, the best estimate of the gain/loss rate is used to free-run the computer until it next goes online.

Whilst the computer is running, chronyd makes measurements of the real-time clock (RTC) (via the `/dev/rtc' interface, which must be compiled into the kernel). An estimate is made of the RTC error at a particular RTC second, and the rate at which the RTC gains or loses time relative to true time.

For kernels in the 2.0 series prior to 2.0.32, the kernel was set up to trim the RTC every 11 minutes. This would be disasterous for chronyd -- there is no reliable way of synchronising with this trimming. For this reason, chronyd only supports the RTC in 2.0 kernels from v2.0.32 onwards. (I don't know anything about the kernel's RTC behaviour in other kernel series).

When the computer is powered down, the measurement histories for all the NTP servers are saved to files (if the dumponexit directive is specified in the configuration file), and the RTC tracking information is also saved to a file (if the rtcfile directive has been specified). These pieces of information are also saved if the dump and writertc commands respectively are issued through chronyc.

When the computer is rebooted, chronyd reads the current RTC time and the RTC information saved at the last shutdown. This information is used to set the system clock to the best estimate of what its time would have been now, had it been left running continuously. The measurement histories for the servers are then reloaded.

The next time the computer goes online, the previous sessions' measurements can contribute to the line-fitting process, which gives a much better estimate of the computer's gain/loss rate.

One problem with saving the measurements and RTC data when the machine is shut down is what happens if there is a power failure; the most recent data will not be saved. Although chronyd is robust enough to cope with this, some performance may be lost. (The main danger arises if the RTC has been changed during the session, with the trimrtc command in chronyc. Because of this, trimrtc will make sure that a meaningful RTC file is saved out after the change is completed).

The easiest protection against power failure is to put the dump and writertc commands in the same place as the offline command is issued to take chronyd offline; because chronyd free-runs between online sessions, no parameters will change significantly between going offline from the Internet and any power failure.

A final point regards home computers which are left running for extended periods and where it is desired to spin down the hard disc when it is not in use (e.g. when not accessed for 15 minutes). chronyd has been planned so it supports such operation; this is the reason why the RTC tracking parameters are not saved to disc after every update, but only when the user requests such a write, or during the shutdown sequence. The only other facility that will generate periodic writes to the disc is the log rtc facility in the configuration file; this option should not be used if you want your disc to spin down.

Typical configuration files.

To illustrate how a dial-up home computer might be configured, example configuration files are shown in this section.

For the `/etc/chrony.conf' file, the following can be used as an example. NOTE : The server directives are only applicable to customers of Demon Internet; users of other ISPs will need to use their own ISP's NTP servers or public NTP servers.

server 158.152.1.65 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 158.152.1.76 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.159.253.2 minpoll 5 maxpoll 10 maxdelay 0.4 offline
logdir /var/log/chrony
log statistics measurements tracking
driftfile /etc/chrony.drift
keyfile /etc/chrony.keys
commandkey 25
maxupdateskew 100.0
dumponexit
dumpdir /var/log/chrony
rtcfile /etc/chrony.rtc

With Freeserve as the ISP, I use the following server lines :

server 194.152.64.68 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.35 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.34 minpoll 5 maxpoll 10 maxdelay 0.4 offline

I use pppd for connecting to my ISP. This runs two scripts `/etc/ppp/ip-up' and `/etc/ppp/ip-down' when the link goes online and offline respectively.

The relevant part of the `/etc/ppp/ip-up' file is (with a dummy password)

cat <<EOF | /usr/local/bin/chronyc
password xxxxxxxx
online
EOF

and the relevant part of the `/etc/ppp/ip-down' script is

cat <<EOF | /usr/local/bin/chronyc
password xxxxxxxx
offline
dump
writertc
EOF

(Because they have to contain the administrator password, it would be desirable to make the files readable only by root on a multiuser machine).

To start chronyd during the boot sequence, I have the following in `/etc/rc.d/rc.local' (this is a Slackware system)

if [ -f /usr/local/sbin/chronyd -a -f /etc/chrony.conf ]; then
  /usr/local/sbin/chronyd -r -s
  echo "Start chronyd"
fi

The Debian package puts a script which handles this and shutdown in `/etc/init.d/chrony'.

The placement of this command may be important on some systems. In particular, chronyd may need to be started several seconds (about 10 as a minimum) before any software that depends on the system clock not jumping or moving backwards, depending on the directives in chronyd's configuration file.

For the system shutdown, chronyd should receive a SIGTERM several seconds before the final SIGKILL; the SIGTERM causes the measurement histories and RTC information to be saved out. There should be no need to add anything to the shutdown sequence, unless (as my system had) there is no pause between the SIGTERM and SIGKILL being delivered to the remaining processes. So if you find something like

killall5 -15 
killall5 -9

in your /etc/rc.d/rc.0 script, you will need to insert a sleep, e.g.

killall5 -15 
sleep 5
killall5 -9

Otherwise, chronyd will not always save information on shutdown, which could be a problem if you don't use dump and writertc when you go offline.

Other important configuration options

The most common option to include in the configuration file is the driftfile option. One of the major tasks of chronyd is to work out how fast or how slow the system clock runs relative to real time - e.g. in terms of seconds gained or lost per day. Measurements over a long period are usually required to refine this estimate to an acceptable degree of accuracy. Therefore, it would be bad if chronyd had to work the value out each time it is restarted, because the system clock would not run so accurately whilst the determination is taking place.

To avoid this problem, chronyd allows the gain or loss rate to be stored in a file, which can be read back in when the program is restarted. This file is called the drift file, and might typically be stored in `/etc/chrony.drift'. By specifying an option like the following

driftfile /etc/chrony.drift

in the configuration file (`/etc/chrony.conf'), the drift file facility will be activated.

Usage reference

Starting chronyd

If chronyd has been installed to its default location `/usr/local/sbin/chronyd', starting it is simply a matter of entering the command

/usr/local/sbin/chronyd

The Debian package uses `/usr/sbin/chronyd'.

Information messages and warnings will be logged to syslog.

The command line options supported are as follows:

-d
When run in this mode, the program will not detach itself from the terminal, and all messages will be sent to the terminal instead of to syslog.
-f <conf-file>
This option can be used to specify an alternate location for the configuration file (default `/etc/chrony.conf').
-r
This option will reload sample histories for each of the servers being used. These histories are created by using the dump command in chronyc, or by setting the dumponexit directive in the configuration file. This option is useful if you want to stop and restart chronyd briefly for any reason, e.g. to install a new version. However, it only makes sense on systems where the kernel can maintain clock compensation whilst not under chronyd's control. The only version where this happens so far is Linux. On systems where this is not the case, e.g. Solaris and SunOS the option should not be used.
-s
This option will set the system clock from the computer's real-time clock. This is analogous to supplying the `-s' flag to the `/sbin/clock' program during the Linux boot sequence. Support for real-time clocks is limited at present - the criteria are described in the section on the rtcfile directive (see section rtcfile). If chronyd cannot support the real time clock on your computer, this option cannot be used and a warning message will be logged to the syslog. If used in conjunction with the `-r' flag, chronyd will attempt to preserve the old samples after setting the system clock from the real time clock. This can be used to allow chronyd to perform long term averaging of the gain or loss rate across system reboots, and is useful for dial-up systems that are shut down when not in use. For this to work well, it relies on chronyd having been able to determine accurate statistics for the difference between the real time clock and system clock last time the computer was on.
-v
This option displays chronyd's version number to the terminal and exits.

On systems that support an `/etc/rc.local' file for starting programs at boot time, chronyd can be started from there.

On systems with a System V style initialisation (e.g. Solaris), a suitable start/stop script might be as shown below. This might be placed in the file `/etc/rc2.d/S83chrony'.

#!/bin/sh
# This file should have uid root, gid sys and chmod 744
#

killproc() {            # kill the named process(es)
        pid=`/usr/bin/ps -e |
             /usr/bin/grep -w $1 |
             /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
        [ "$pid" != "" ] && kill $pid
}

case "$1" in

'start')
   if [ -f /opt/free/sbin/chronyd -a -f /etc/chrony.conf ]; then
     /opt/free/sbin/chronyd
   fi
   ;;
'stop')
   killproc chronyd
   ;;
*)
   echo "Usage: /etc/rc2.d/S83chrony { start | stop }"
   ;;
esac

(In both cases, you may want to bear in mind that chronyd can step the time when it starts. There may be other g. Solari

and in the file `/etc/ppp/ip-down' we add the sequence

cat <<EOF | /usr/local/bin/chronyc
password xyzzy
offline
EOF

The Debian package puts scripts similar to those above in the directories `/etc/ppp/ip-up.d' and `/etc/ppp/ip-down.d'.

chronyd's polling of the servers will now only occur whilst the machine is actually connected to the Internet.

Isolated networks

In this section we discuss how to configure chrony for computers that never have network conectivity to any computer which ultimately derives its time from a reference clock.

In this situation, one computer is selected to be the master timeserver. The other computers are either direct clients of the master, or clients of clients.

The rate value in the master's drift file needs to be set to the average rate at which the master gains or loses time. chronyd includes support for this, in the form of the manual directive in the configuration file and the settime command in the chronyc program.

If the master is rebooted, chronyd can re-read the drift rate from the drift file. However, the master has no accurate estimate of the current time. To get around this, the system can be configured so that the master can initially set itself to a `majority-vote' of selected clients' times; this allows the clients to `flywheel' the master across its outage.

A typical configuration file for the master (called master) might be (assuming the clients are in the 192.168.165.x subnet and that the master's address is 192.168.169.170)

driftfile /etc/chrony.drift
commandkey 25
keyfile /etc/chrony.keys
initstepslew 10 client1 client3 client6
local stratum 8
manual
allow 192.168.165

For the clients that have to resynchronise the master when it restarts, the configuration file might be

server master
driftfile /etc/chrony.drift
logdir /var/log/chrony
log measurements statistics tracking
keyfile /etc/chrony.keys
commandkey 24
local stratum 10
initstepslew 20 master
allow 192.168.169.170

The rest of the clients would be the same, except that the local and allow directives are not required.

The home PC with a dial-up connection

Assumptions/how the software works

This section considers the home computer which has a dial-up connection. It assumes that Linux is run exclusively on the computer. Dual-boot systems may work; it depends what (if anything) the other system does to the system's real-time clock.

Much of the configuration for this case is discussed earlier (see section Infrequent connection to true NTP servers). This section addresses specifically the case of a computer which is turned off between 'sessions'.

In this case, chronyd relies on the computer's real-time clock (RTC) to maintain the time between the periods when it is powered up. The arrangement is shown in the figure below.

            trim if required                          PSTN
      +---------------------------+               +----------+
      |                           |               |          |
      v                           |               |          |
+---------+                    +-------+       +-----+     +---+
| System's|  measure error/    |chronyd|       |modem|     |ISP|
|real-time|------------------->|       |-------|     |     |   |
|  clock  |   drift rate       +-------+       +-----+     +---+
+---------+                       ^                          |
      |                           |                          |
      +---------------------------+                  --o-----o---
         set time at boot up                           |
                                                  +----------+
                                                  |NTP server|
                                                  +----------+

When the computer is connected to the Internet (via the modem), chronyd has access to external NTP servers which it makes measurements from. These measurements are saved, and straight-line fits are performed on them to provide an estimate of the computer's time error and rate of gaining/losing time.

When the computer is taken offline from the Internet, the best estimate of the gain/loss rate is used to free-run the computer until it next goes online.

Whilst the computer is running, chronyd makes measurements of the real-time clock (RTC) (via the `/dev/rtc' interface, which must be compiled into the kernel). An estimate is made of the RTC error at a particular RTC second, and the rate at which the RTC gains or loses time relative to true time.

For kernels in the 2.0 series prior to 2.0.32, the kernel was set up to trim the RTC every 11 minutes. This would be disasterous for chronyd -- there is no reliable way of synchronising with this trimming. For this reason, chronyd only supports the RTC in 2.0 kernels from v2.0.32 onwards. (I don't know anything about the kernel's RTC behaviour in other kernel series).

When the computer is powered down, the measurement histories for all the NTP servers are saved to files (if the dumponexit directive is specified in the configuration file), and the RTC tracking information is also saved to a file (if the rtcfile directive has been specified). These pieces of information are also saved if the dump and writertc commands respectively are issued through chronyc.

When the computer is rebooted, chronyd reads the current RTC time and the RTC information saved at the last shutdown. This information is used to set the system clock to the best estimate of what its time would have been now, had it been left running continuously. The measurement histories for the servers are then reloaded.

The next time the computer goes online, the previous sessions' measurements can contribute to the line-fitting process, which gives a much better estimate of the computer's gain/loss rate.

One problem with saving the measurements and RTC data when the machine is shut down is what happens if there is a power failure; the most recent data will not be saved. Although chronyd is robust enough to cope with this, some performance may be lost. (The main danger arises if the RTC has been changed during the session, with the trimrtc command in chronyc. Because of this, trimrtc will make sure that a meaningful RTC file is saved out after the change is completed).

The easiest protection against power failure is to put the dump and writertc commands in the same place as the offline command is issued to take chronyd offline; because chronyd free-runs between online sessions, no parameters will change significantly between going offline from the Internet and any power failure.

A final point regards home computers which are left running for extended periods and where it is desired to spin down the hard disc when it is not in use (e.g. when not accessed for 15 minutes). chronyd has been planned so it supports such operation; this is the reason why the RTC tracking parameters are not saved to disc after every update, but only when the user requests such a write, or during the shutdown sequence. The only other facility that will generate periodic writes to the disc is the log rtc facility in the configuration file; this option should not be used if you want your disc to spin down.

Typical configuration files.

To illustrate how a dial-up home computer might be configured, example configuration files are shown in this section.

For the `/etc/chrony.conf' file, the following can be used as an example. NOTE : The server directives are only applicable to customers of Demon Internet; users of other ISPs will need to use their own ISP's NTP servers or public NTP servers.

server 158.152.1.65 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 158.152.1.76 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.159.253.2 minpoll 5 maxpoll 10 maxdelay 0.4 offline
logdir /var/log/chrony
log statistics measurements tracking
driftfile /etc/chrony.drift
keyfile /etc/chrony.keys
commandkey 25
maxupdateskew 100.0
dumponexit
dumpdir /var/log/chrony
rtcfile /etc/chrony.rtc

With Freeserve as the ISP, I use the following server lines :

server 194.152.64.68 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.35 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.34 minpoll 5 maxpoll 10 maxdelay 0.4 offline

I use pppd for connecting to my ISP. This runs two scripts `/etc/ppp/ip-up' and `/etc/ppp/ip-down' when the link goes online and offline respectively.

The relevant part of the `/etc/ppp/ip-up' file is (with a dummy password)

cat <<EOF | /usr/local/bin/chronyc
password xxxxxxxx
online
EOF

and the relevant part of the `/etc/ppp/ip-down' script is

cat <<EOF | /usr/local/bin/chronyc
password xxxxxxxx
offline
dump
writertc
EOF

(Because they have to contain the administrator password, it would be desirable to make the files readable only by root on a multiuser machine).

To start chronyd during the boot sequence, I have the following in `/etc/rc.d/rc.local' (this is a Slackware system)

if [ -f /usr/local/sbin/chronyd -a -f /etc/chrony.conf ]; then
  /usr/local/sbin/chronyd -r -s
  echo "Start chronyd"
fi

The Debian package puts a script which handles this and shutdown in `/etc/init.d/chrony'.

The placement of this command may be important on some systems. In particular, chronyd may need to be started several seconds (about 10 as a minimum) before any software that depends on the system clock not jumping or moving backwards, depending on the directives in chronyd's configuration file.

For the system shutdown, chronyd should receive a SIGTERM several seconds before the final SIGKILL; the SIGTERM causes the measurement histories and RTC information to be saved out. There should be no need to add anything to the shutdown sequence, unless (as my system had) there is no pause between the SIGTERM and SIGKILL being delivered to the remaining processes. So if you find something like

killall5 -15 
killall5 -9

in your /etc/rc.d/rc.0 script, you will need to insert a sleep, e.g.

killall5 -15 
sleep 5
killall5 -9

Otherwise, chronyd will not always save information on shutdown, which could be a problem if you don't use dump and writertc when you go offline.

Other important configuration options

The most common option to include in the configuration file is the driftfile option. One of the major tasks of chronyd is to work out how fast or how slow the system clock runs relative to real time - e.g. in terms of seconds gained or lost per day. Measurements over a long period are usually required to refine this estimate to an acceptable degree of accuracy. Therefore, it would be bad if chronyd had to work the value out each time it is restarted, because the system clock would not run so accurately whilst the determination is taking place.

To avoid this problem, chronyd allows the gain or loss rate to be stored in a file, which can be read back in when the program is restarted. This file is called the drift file, and might typically be stored in `/etc/chrony.drift'. By specifying an option like the following

driftfile /etc/chrony.drift

in the configuration file (`/etc/chrony.conf'), the drift file facility will be activated.

Usage reference

Starting chronyd

If chronyd has been installed to its default location `/usr/local/sbin/chronyd', starting it is simply a matter of entering the command

/usr/local/sbin/chronyd

The Debian package uses `/usr/sbin/chronyd'.

Information messages and warnings will be logged to syslog.

The command line options supported are as follows:

-d
When run in this mode, the program will not detach itself from the terminal, and all messages will be sent to the terminal instead of to syslog.
-f <conf-file>
This option can be used to specify an alternate location for the configuration file (default `/etc/chrony.conf').
-r
This option will reload sample histories for each of the servers being used. These histories are created by using the dump command in chronyc, or by setting the dumponexit directive in the configuration file. This option is useful if you want to stop and restart chronyd briefly for any reason, e.g. to install a new version. However, it only makes sense on systems where the kernel can maintain clock compensation whilst not under chronyd's control. The only version where this happens so far is Linux. On systems where this is not the case, e.g. Solaris and SunOS the option should not be used.
-s
This option will set the system clock from the computer's real-time clock. This is analogous to supplying the `-s' flag to the `/sbin/clock' program during the Linux boot sequence. Support for real-time clocks is limited at present - the criteria are described in the section on the rtcfile directive (see section rtcfile). If chronyd cannot support the real time clock on your computer, this option cannot be used and a warning message will be logged to the syslog. If used in conjunction with the `-r' flag, chronyd will attempt to preserve the old samples after setting the system clock from the real time clock. This can be used to allow chronyd to perform long term averaging of the gain or loss rate across system reboots, and is useful for dial-up systems that are shut down when not in use. For this to work well, it relies on chronyd having been able to determine accurate statistics for the difference between the real time clock and system clock last time the computer was on.
-v
This option displays chronyd's version number to the terminal and exits.

On systems that support an `/etc/rc.local' file for starting programs at boot time, chronyd can be started from there.

On systems with a System V style initialisation (e.g. Solaris), a suitable start/stop script might be as shown below. This might be placed in the file `/etc/rc2.d/S83chrony'.

#!/bin/sh
# This file should have uid root, gid sys and chmod 744
#

killproc() {            # kill the named process(es)
        pid=`/usr/bin/ps -e |
             /usr/bin/grep -w $1 |
             /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
        [ "$pid" != "" ] && kill $pid
}

case "$1" in

'start')
   if [ -f /opt/free/sbin/chronyd -a -f /etc/chrony.conf ]; then
     /opt/free/sbin/chronyd
   fi
   ;;
'stop')
   killproc chronyd
   ;;
*)
   echo "Usage: /etc/rc2.d/S83chrony { start | stop }"
   ;;
esac

(In both cases, you may want to bear in mind that chronyd can step the time when it starts. There may be other g. Solari

and in the file `/etc/ppp/ip-down' we add the sequence

cat <<EOF | /usr/local/bin/chronyc
password xyzzy
offline
EOF

The Debian package puts scripts similar to those above in the directories `/etc/ppp/ip-up.d' and `/etc/ppp/ip-down.d'.

chronyd's polling of the servers will now only occur whilst the machine is actually connected to the Internet.

Isolated networks

In this section we discuss how to configure chrony for computers that never have network conectivity to any computer which ultimately derives its time from a reference clock.

In this situation, one computer is selected to be the master timeserver. The other computers are either direct clients of the master, or clients of clients.

The rate value in the master's drift file needs to be set to the average rate at which the master gains or loses time. chronyd includes support for this, in the form of the manual directive in the configuration file and the settime command in the chronyc program.

If the master is rebooted, chronyd can re-read the drift rate from the drift file. However, the master has no accurate estimate of the current time. To get around this, the system can be configured so that the master can initially set itself to a `majority-vote' of selected clients' times; this allows the clients to `flywheel' the master across its outage.

A typical configuration file for the master (called master) might be (assuming the clients are in the 192.168.165.x subnet and that the master's address is 192.168.169.170)

driftfile /etc/chrony.drift
commandkey 25
keyfile /etc/chrony.keys
initstepslew 10 client1 client3 client6
local stratum 8
manual
allow 192.168.165

For the clients that have to resynchronise the master when it restarts, the configuration file might be

server master
driftfile /etc/chrony.drift
logdir /var/log/chrony
log measurements statistics tracking
keyfile /etc/chrony.keys
commandkey 24
local stratum 10
initstepslew 20 master
allow 192.168.169.170

The rest of the clients would be the same, except that the local and allow directives are not required.

The home PC with a dial-up connection

Assumptions/how the software works

This section considers the home computer which has a dial-up connection. It assumes that Linux is run exclusively on the computer. Dual-boot systems may work; it depends what (if anything) the other system does to the system's real-time clock.

Much of the configuration for this case is discussed earlier (see section Infrequent connection to true NTP servers). This section addresses specifically the case of a computer which is turned off between 'sessions'.

In this case, chronyd relies on the computer's real-time clock (RTC) to maintain the time between the periods when it is powered up. The arrangement is shown in the figure below.

            trim if required                          PSTN
      +---------------------------+               +----------+
      |                           |               |          |
      v                           |               |          |
+---------+                    +-------+       +-----+     +---+
| System's|  measure error/    |chronyd|       |modem|     |ISP|
|real-time|------------------->|       |-------|     |     |   |
|  clock  |   drift rate       +-------+       +-----+     +---+
+---------+                       ^                          |
      |                           |                          |
      +---------------------------+                  --o-----o---
         set time at boot up                           |
                                                  +----------+
                                                  |NTP server|
                                                  +----------+

When the computer is connected to the Internet (via the modem), chronyd has access to external NTP servers which it makes measurements from. These measurements are saved, and straight-line fits are performed on them to provide an estimate of the computer's time error and rate of gaining/losing time.

When the computer is taken offline from the Internet, the best estimate of the gain/loss rate is used to free-run the computer until it next goes online.

Whilst the computer is running, chronyd makes measurements of the real-time clock (RTC) (via the `/dev/rtc' interface, which must be compiled into the kernel). An estimate is made of the RTC error at a particular RTC second, and the rate at which the RTC gains or loses time relative to true time.

For kernels in the 2.0 series prior to 2.0.32, the kernel was set up to trim the RTC every 11 minutes. This would be disasterous for chronyd -- there is no reliable way of synchronising with this trimming. For this reason, chronyd only supports the RTC in 2.0 kernels from v2.0.32 onwards. (I don't know anything about the kernel's RTC behaviour in other kernel series).

When the computer is powered down, the measurement histories for all the NTP servers are saved to files (if the dumponexit directive is specified in the configuration file), and the RTC tracking information is also saved to a file (if the rtcfile directive has been specified). These pieces of information are also saved if the dump and writertc commands respectively are issued through chronyc.

When the computer is rebooted, chronyd reads the current RTC time and the RTC information saved at the last shutdown. This information is used to set the system clock to the best estimate of what its time would have been now, had it been left running continuously. The measurement histories for the servers are then reloaded.

The next time the computer goes online, the previous sessions' measurements can contribute to the line-fitting process, which gives a much better estimate of the computer's gain/loss rate.

One problem with saving the measurements and RTC data when the machine is shut down is what happens if there is a power failure; the most recent data will not be saved. Although chronyd is robust enough to cope with this, some performance may be lost. (The main danger arises if the RTC has been changed during the session, with the trimrtc command in chronyc. Because of this, trimrtc will make sure that a meaningful RTC file is saved out after the change is completed).

The easiest protection against power failure is to put the dump and writertc commands in the same place as the offline command is issued to take chronyd offline; because chronyd free-runs between online sessions, no parameters will change significantly between going offline from the Internet and any power failure.

A final point regards home computers which are left running for extended periods and where it is desired to spin down the hard disc when it is not in use (e.g. when not accessed for 15 minutes). chronyd has been planned so it supports such operation; this is the reason why the RTC tracking parameters are not saved to disc after every update, but only when the user requests such a write, or during the shutdown sequence. The only other facility that will generate periodic writes to the disc is the log rtc facility in the configuration file; this option should not be used if you want your disc to spin down.

Typical configuration files.

To illustrate how a dial-up home computer might be configured, example configuration files are shown in this section.

For the `/etc/chrony.conf' file, the following can be used as an example. NOTE : The server directives are only applicable to customers of Demon Internet; users of other ISPs will need to use their own ISP's NTP servers or public NTP servers.

server 158.152.1.65 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 158.152.1.76 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.159.253.2 minpoll 5 maxpoll 10 maxdelay 0.4 offline
logdir /var/log/chrony
log statistics measurements tracking
driftfile /etc/chrony.drift
keyfile /etc/chrony.keys
commandkey 25
maxupdateskew 100.0
dumponexit
dumpdir /var/log/chrony
rtcfile /etc/chrony.rtc

With Freeserve as the ISP, I use the following server lines :

server 194.152.64.68 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.35 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.34 minpoll 5 maxpoll 10 maxdelay 0.4 offline

I use pppd for connecting to my ISP. This runs two scripts `/etc/ppp/ip-up' and `/etc/ppp/ip-down' when the link goes online and offline respectively.

The relevant part of the `/etc/ppp/ip-up' file is (with a dummy password)

cat <<EOF | /usr/local/bin/chronyc
password xxxxxxxx
online
EOF

and the relevant part of the `/etc/ppp/ip-down' script is

cat <<EOF | /usr/local/bin/chronyc
password xxxxxxxx
offline
dump
writertc
EOF

(Because they have to contain the administrator password, it would be desirable to make the files readable only by root on a multiuser machine).

To start chronyd during the boot sequence, I have the following in `/etc/rc.d/rc.local' (this is a Slackware system)

if [ -f /usr/local/sbin/chronyd -a -f /etc/chrony.conf ]; then
  /usr/local/sbin/chronyd -r -s
  echo "Start chronyd"
fi

The Debian package puts a script which handles this and shutdown in `/etc/init.d/chrony'.

The placement of this command may be important on some systems. In particular, chronyd may need to be started several seconds (about 10 as a minimum) before any software that depends on the system clock not jumping or moving backwards, depending on the directives in chronyd's configuration file.

For the system shutdown, chronyd should receive a SIGTERM several seconds before the final SIGKILL; the SIGTERM causes the measurement histories and RTC information to be saved out. There should be no need to add anything to the shutdown sequence, unless (as my system had) there is no pause between the SIGTERM and SIGKILL being delivered to the remaining processes. So if you find something like

killall5 -15 
killall5 -9

in your /etc/rc.d/rc.0 script, you will need to insert a sleep, e.g.

killall5 -15 
sleep 5
killall5 -9

Otherwise, chronyd will not always save information on shutdown, which could be a problem if you don't use dump and writertc when you go offline.

Other important configuration options

The most common option to include in the configuration file is the driftfile option. One of the major tasks of chronyd is to work out how fast or how slow the system clock runs relative to real time - e.g. in terms of seconds gained or lost per day. Measurements over a long period are usually required to refine this estimate to an acceptable degree of accuracy. Therefore, it would be bad if chronyd had to work the value out each time it is restarted, because the system clock would not run so accurately whilst the determination is taking place.

To avoid this problem, chronyd allows the gain or loss rate to be stored in a file, which can be read back in when the program is restarted. This file is called the drift file, and might typically be stored in `/etc/chrony.drift'. By specifying an option like the following

driftfile /etc/chrony.drift

in the configuration file (`/etc/chrony.conf'), the drift file facility will be activated.

Usage reference

Starting chronyd

If chronyd has been installed to its default location `/usr/local/sbin/chronyd', starting it is simply a matter of entering the command

/usr/local/sbin/chronyd

The Debian package uses `/usr/sbin/chronyd'.

Information messages and warnings will be logged to syslog.

The command line options supported are as follows:

-d
When run in this mode, the program will not detach itself from the terminal, and all messages will be sent to the terminal instead of to syslog.
-f <conf-file>
This option can be used to specify an alternate location for the configuration file (default `/etc/chrony.conf').
-r
This option will reload sample histories for each of the servers being used. These histories are created by using the dump command in chronyc, or by setting the dumponexit directive in the configuration file. This option is useful if you want to stop and restart chronyd briefly for any reason, e.g. to install a new version. However, it only makes sense on systems where the kernel can maintain clock compensation whilst not under chronyd's control. The only version where this happens so far is Linux. On systems where this is not the case, e.g. Solaris and SunOS the option should not be used.
-s
This option will set the system clock from the computer's real-time clock. This is analogous to supplying the `-s' flag to the `/sbin/clock' program during the Linux boot sequence. Support for real-time clocks is limited at present - the criteria are described in the section on the rtcfile directive (see section rtcfile). If chronyd cannot support the real time clock on your computer, this option cannot be used and a warning message will be logged to the syslog. If used in conjunction with the `-r' flag, chronyd will attempt to preserve the old samples after setting the system clock from the real time clock. This can be used to allow chronyd to perform long term averaging of the gain or loss rate across system reboots, and is useful for dial-up systems that are shut down when not in use. For this to work well, it relies on chronyd having been able to determine accurate statistics for the difference between the real time clock and system clock last time the computer was on.
-v
This option displays chronyd's version number to the terminal and exits.

On systems that support an `/etc/rc.local' file for starting programs at boot time, chronyd can be started from there.

On systems with a System V style initialisation (e.g. Solaris), a suitable start/stop script might be as shown below. This might be placed in the file `/etc/rc2.d/S83chrony'.

#!/bin/sh
# This file should have uid root, gid sys and chmod 744
#

killproc() {            # kill the named process(es)
        pid=`/usr/bin/ps -e |
             /usr/bin/grep -w $1 |
             /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
        [ "$pid" != "" ] && kill $pid
}

case "$1" in

'start')
   if [ -f /opt/free/sbin/chronyd -a -f /etc/chrony.conf ]; then
     /opt/free/sbin/chronyd
   fi
   ;;
'stop')
   killproc chronyd
   ;;
*)
   echo "Usage: /etc/rc2.d/S83chrony { start | stop }"
   ;;
esac

(In both cases, you may want to bear in mind that chronyd can step the time when it starts. There may be other g. Solari

and in the file `/etc/ppp/ip-down' we add the sequence

cat <<EOF | /usr/local/bin/chronyc
password xyzzy
offline
EOF

The Debian package puts scripts similar to those above in the directories `/etc/ppp/ip-up.d' and `/etc/ppp/ip-down.d'.

chronyd's polling of the servers will now only occur whilst the machine is actually connected to the Internet.

Isolated networks

In this section we discuss how to configure chrony for computers that never have network conectivity to any computer which ultimately derives its time from a reference clock.

In this situation, one computer is selected to be the master timeserver. The other computers are either direct clients of the master, or clients of clients.

The rate value in the master's drift file needs to be set to the average rate at which the master gains or loses time. chronyd includes support for this, in the form of the manual directive in the configuration file and the settime command in the chronyc program.

If the master is rebooted, chronyd can re-read the drift rate from the drift file. However, the master has no accurate estimate of the current time. To get around this, the system can be configured so that the master can initially set itself to a `majority-vote' of selected clients' times; this allows the clients to `flywheel' the master across its outage.

A typical configuration file for the master (called master) might be (assuming the clients are in the 192.168.165.x subnet and that the master's address is 192.168.169.170)

driftfile /etc/chrony.drift
commandkey 25
keyfile /etc/chrony.keys
initstepslew 10 client1 client3 client6
local stratum 8
manual
allow 192.168.165

For the clients that have to resynchronise the master when it restarts, the configuration file might be

server master
driftfile /etc/chrony.drift
logdir /var/log/chrony
log measurements statistics tracking
keyfile /etc/chrony.keys
commandkey 24
local stratum 10
initstepslew 20 master
allow 192.168.169.170

The rest of the clients would be the same, except that the local and allow directives are not required.

The home PC with a dial-up connection

Assumptions/how the software works

This section considers the home computer which has a dial-up connection. It assumes that Linux is run exclusively on the computer. Dual-boot systems may work; it depends what (if anything) the other system does to the system's real-time clock.

Much of the configuration for this case is discussed earlier (see section Infrequent connection to true NTP servers). This section addresses specifically the case of a computer which is turned off between 'sessions'.

In this case, chronyd relies on the computer's real-time clock (RTC) to maintain the time between the periods when it is powered up. The arrangement is shown in the figure below.

            trim if required                          PSTN
      +---------------------------+               +----------+
      |                           |               |          |
      v                           |               |          |
+---------+                    +-------+       +-----+     +---+
| System's|  measure error/    |chronyd|       |modem|     |ISP|
|real-time|------------------->|       |-------|     |     |   |
|  clock  |   drift rate       +-------+       +-----+     +---+
+---------+                       ^                          |
      |                           |                          |
      +---------------------------+                  --o-----o---
         set time at boot up                           |
                                                  +----------+
                                                  |NTP server|
                                                  +----------+

When the computer is connected to the Internet (via the modem), chronyd has access to external NTP servers which it makes measurements from. These measurements are saved, and straight-line fits are performed on them to provide an estimate of the computer's time error and rate of gaining/losing time.

When the computer is taken offline from the Internet, the best estimate of the gain/loss rate is used to free-run the computer until it next goes online.

Whilst the computer is running, chronyd makes measurements of the real-time clock (RTC) (via the `/dev/rtc' interface, which must be compiled into the kernel). An estimate is made of the RTC error at a particular RTC second, and the rate at which the RTC gains or loses time relative to true time.

For kernels in the 2.0 series prior to 2.0.32, the kernel was set up to trim the RTC every 11 minutes. This would be disasterous for chronyd -- there is no reliable way of synchronising with this trimming. For this reason, chronyd only supports the RTC in 2.0 kernels from v2.0.32 onwards. (I don't know anything about the kernel's RTC behaviour in other kernel series).

When the computer is powered down, the measurement histories for all the NTP servers are saved to files (if the dumponexit directive is specified in the configuration file), and the RTC tracking information is also saved to a file (if the rtcfile directive has been specified). These pieces of information are also saved if the dump and writertc commands respectively are issued through chronyc.

When the computer is rebooted, chronyd reads the current RTC time and the RTC information saved at the last shutdown. This information is used to set the system clock to the best estimate of what its time would have been now, had it been left running continuously. The measurement histories for the servers are then reloaded.

The next time the computer goes online, the previous sessions' measurements can contribute to the line-fitting process, which gives a much better estimate of the computer's gain/loss rate.

One problem with saving the measurements and RTC data when the machine is shut down is what happens if there is a power failure; the most recent data will not be saved. Although chronyd is robust enough to cope with this, some performance may be lost. (The main danger arises if the RTC has been changed during the session, with the trimrtc command in chronyc. Because of this, trimrtc will make sure that a meaningful RTC file is saved out after the change is completed).

The easiest protection against power failure is to put the dump and writertc commands in the same place as the offline command is issued to take chronyd offline; because chronyd free-runs between online sessions, no parameters will change significantly between going offline from the Internet and any power failure.

A final point regards home computers which are left running for extended periods and where it is desired to spin down the hard disc when it is not in use (e.g. when not accessed for 15 minutes). chronyd has been planned so it supports such operation; this is the reason why the RTC tracking parameters are not saved to disc after every update, but only when the user requests such a write, or during the shutdown sequence. The only other facility that will generate periodic writes to the disc is the log rtc facility in the configuration file; this option should not be used if you want your disc to spin down.

Typical configuration files.

To illustrate how a dial-up home computer might be configured, example configuration files are shown in this section.

For the `/etc/chrony.conf' file, the following can be used as an example. NOTE : The server directives are only applicable to customers of Demon Internet; users of other ISPs will need to use their own ISP's NTP servers or public NTP servers.

server 158.152.1.65 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 158.152.1.76 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.159.253.2 minpoll 5 maxpoll 10 maxdelay 0.4 offline
logdir /var/log/chrony
log statistics measurements tracking
driftfile /etc/chrony.drift
keyfile /etc/chrony.keys
commandkey 25
maxupdateskew 100.0
dumponexit
dumpdir /var/log/chrony
rtcfile /etc/chrony.rtc

With Freeserve as the ISP, I use the following server lines :

server 194.152.64.68 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.35 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.34 minpoll 5 maxpoll 10 maxdelay 0.4 offline

I use pppd for connecting to my ISP. This runs two scripts `/etc/ppp/ip-up' and `/etc/ppp/ip-down' when the link goes online and offline respectively.

The relevant part of the `/etc/ppp/ip-up' file is (with a dummy password)

cat <<EOF | /usr/local/bin/chronyc
password xxxxxxxx
online
EOF

and the relevant part of the `/etc/ppp/ip-down' script is

cat <<EOF | /usr/local/bin/chronyc
password xxxxxxxx
offline
dump
writertc
EOF

(Because they have to contain the administrator password, it would be desirable to make the files readable only by root on a multiuser machine).

To start chronyd during the boot sequence, I have the following in `/etc/rc.d/rc.local' (this is a Slackware system)

if [ -f /usr/local/sbin/chronyd -a -f /etc/chrony.conf ]; then
  /usr/local/sbin/chronyd -r -s
  echo "Start chronyd"
fi

The Debian package puts a script which handles this and shutdown in `/etc/init.d/chrony'.

The placement of this command may be important on some systems. In particular, chronyd may need to be started several seconds (about 10 as a minimum) before any software that depends on the system clock not jumping or moving backwards, depending on the directives in chronyd's configuration file.

For the system shutdown, chronyd should receive a SIGTERM several seconds before the final SIGKILL; the SIGTERM causes the measurement histories and RTC information to be saved out. There should be no need to add anything to the shutdown sequence, unless (as my system had) there is no pause between the SIGTERM and SIGKILL being delivered to the remaining processes. So if you find something like

killall5 -15 
killall5 -9

in your /etc/rc.d/rc.0 script, you will need to insert a sleep, e.g.

killall5 -15 
sleep 5
killall5 -9

Otherwise, chronyd will not always save information on shutdown, which could be a problem if you don't use dump and writertc when you go offline.

Other important configuration options

The most common option to include in the configuration file is the driftfile option. One of the major tasks of chronyd is to work out how fast or how slow the system clock runs relative to real time - e.g. in terms of seconds gained or lost per day. Measurements over a long period are usually required to refine this estimate to an acceptable degree of accuracy. Therefore, it would be bad if chronyd had to work the value out each time it is restarted, because the system clock would not run so accurately whilst the determination is taking place.

To avoid this problem, chronyd allows the gain or loss rate to be stored in a file, which can be read back in when the program is restarted. This file is called the drift file, and might typically be stored in `/etc/chrony.drift'. By specifying an option like the following

driftfile /etc/chrony.drift

in the configuration file (`/etc/chrony.conf'), the drift file facility will be activated.

Usage reference

Starting chronyd

If chronyd has been installed to its default location `/usr/local/sbin/chronyd', starting it is simply a matter of entering the command

/usr/local/sbin/chronyd

The Debian package uses `/usr/sbin/chronyd'.

Information messages and warnings will be logged to syslog.

The command line options supported are as follows:

-d
When run in this mode, the program will not detach itself from the terminal, and all messages will be sent to the terminal instead of to syslog.
-f <conf-file>
This option can be used to specify an alternate location for the configuration file (default `/etc/chrony.conf').
-r
This option will reload sample histories for each of the servers being used. These histories are created by using the dump command in chronyc, or by setting the dumponexit directive in the configuration file. This option is useful if you want to stop and restart chronyd briefly for any reason, e.g. to install a new version. However, it only makes sense on systems where the kernel can maintain clock compensation whilst not under chronyd's control. The only version where this happens so far is Linux. On systems where this is not the case, e.g. Solaris and SunOS the option should not be used.
-s
This option will set the system clock from the computer's real-time clock. This is analogous to supplying the `-s' flag to the `/sbin/clock' program during the Linux boot sequence. Support for real-time clocks is limited at present - the criteria are described in the section on the rtcfile directive (see section rtcfile). If chronyd cannot support the real time clock on your computer, this option cannot be used and a warning message will be logged to the syslog. If used in conjunction with the `-r' flag, chronyd will attempt to preserve the old samples after setting the system clock from the real time clock. This can be used to allow chronyd to perform long term averaging of the gain or loss rate across system reboots, and is useful for dial-up systems that are shut down when not in use. For this to work well, it relies on chronyd having been able to determine accurate statistics for the difference between the real time clock and system clock last time the computer was on.
-v
This option displays chronyd's version number to the terminal and exits.

On systems that support an `/etc/rc.local' file for starting programs at boot time, chronyd can be started from there.

On systems with a System V style initialisation (e.g. Solaris), a suitable start/stop script might be as shown below. This might be placed in the file `/etc/rc2.d/S83chrony'.

#!/bin/sh
# This file should have uid root, gid sys and chmod 744
#

killproc() {            # kill the named process(es)
        pid=`/usr/bin/ps -e |
             /usr/bin/grep -w $1 |
             /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
        [ "$pid" != "" ] && kill $pid
}

case "$1" in

'start')
   if [ -f /opt/free/sbin/chronyd -a -f /etc/chrony.conf ]; then
     /opt/free/sbin/chronyd
   fi
   ;;
'stop')
   killproc chronyd
   ;;
*)
   echo "Usage: /etc/rc2.d/S83chrony { start | stop }"
   ;;
esac

(In both cases, you may want to bear in mind that chronyd can step the time when it starts. There may be other g. Solari

and in the file `/etc/ppp/ip-down' we add the sequence

cat <<EOF | /usr/local/bin/chronyc
password xyzzy
offline
EOF

The Debian package puts scripts similar to those above in the directories `/etc/ppp/ip-up.d' and `/etc/ppp/ip-down.d'.

chronyd's polling of the servers will now only occur whilst the machine is actually connected to the Internet.

Isolated networks

In this section we discuss how to configure chrony for computers that never have network conectivity to any computer which ultimately derives its time from a reference clock.

In this situation, one computer is selected to be the master timeserver. The other computers are either direct clients of the master, or clients of clients.

The rate value in the master's drift file needs to be set to the average rate at which the master gains or loses time. chronyd includes support for this, in the form of the manual directive in the configuration file and the settime command in the chronyc program.

If the master is rebooted, chronyd can re-read the drift rate from the drift file. However, the master has no accurate estimate of the current time. To get around this, the system can be configured so that the master can initially set itself to a `majority-vote' of selected clients' times; this allows the clients to `flywheel' the master across its outage.

A typical configuration file for the master (called master) might be (assuming the clients are in the 192.168.165.x subnet and that the master's address is 192.168.169.170)

driftfile /etc/chrony.drift
commandkey 25
keyfile /etc/chrony.keys
initstepslew 10 client1 client3 client6
local stratum 8
manual
allow 192.168.165

For the clients that have to resynchronise the master when it restarts, the configuration file might be

server master
driftfile /etc/chrony.drift
logdir /var/log/chrony
log measurements statistics tracking
keyfile /etc/chrony.keys
commandkey 24
local stratum 10
initstepslew 20 master
allow 192.168.169.170

The rest of the clients would be the same, except that the local and allow directives are not required.

The home PC with a dial-up connection

Assumptions/how the software works

This section considers the home computer which has a dial-up connection. It assumes that Linux is run exclusively on the computer. Dual-boot systems may work; it depends what (if anything) the other system does to the system's real-time clock.

Much of the configuration for this case is discussed earlier (see section Infrequent connection to true NTP servers). This section addresses specifically the case of a computer which is turned off between 'sessions'.

In this case, chronyd relies on the computer's real-time clock (RTC) to maintain the time between the periods when it is powered up. The arrangement is shown in the figure below.

            trim if required                          PSTN
      +---------------------------+               +----------+
      |                           |               |          |
      v                           |               |          |
+---------+                    +-------+       +-----+     +---+
| System's|  measure error/    |chronyd|       |modem|     |ISP|
|real-time|------------------->|       |-------|     |     |   |
|  clock  |   drift rate       +-------+       +-----+     +---+
+---------+                       ^                          |
      |                           |                          |
      +---------------------------+                  --o-----o---
         set time at boot up                           |
                                                  +----------+
                                                  |NTP server|
                                                  +----------+

When the computer is connected to the Internet (via the modem), chronyd has access to external NTP servers which it makes measurements from. These measurements are saved, and straight-line fits are performed on them to provide an estimate of the computer's time error and rate of gaining/losing time.

When the computer is taken offline from the Internet, the best estimate of the gain/loss rate is used to free-run the computer until it next goes online.

Whilst the computer is running, chronyd makes measurements of the real-time clock (RTC) (via the `/dev/rtc' interface, which must be compiled into the kernel). An estimate is made of the RTC error at a particular RTC second, and the rate at which the RTC gains or loses time relative to true time.

For kernels in the 2.0 series prior to 2.0.32, the kernel was set up to trim the RTC every 11 minutes. This would be disasterous for chronyd -- there is no reliable way of synchronising with this trimming. For this reason, chronyd only supports the RTC in 2.0 kernels from v2.0.32 onwards. (I don't know anything about the kernel's RTC behaviour in other kernel series).

When the computer is powered down, the measurement histories for all the NTP servers are saved to files (if the dumponexit directive is specified in the configuration file), and the RTC tracking information is also saved to a file (if the rtcfile directive has been specified). These pieces of information are also saved if the dump and writertc commands respectively are issued through chronyc.

When the computer is rebooted, chronyd reads the current RTC time and the RTC information saved at the last shutdown. This information is used to set the system clock to the best estimate of what its time would have been now, had it been left running continuously. The measurement histories for the servers are then reloaded.

The next time the computer goes online, the previous sessions' measurements can contribute to the line-fitting process, which gives a much better estimate of the computer's gain/loss rate.

One problem with saving the measurements and RTC data when the machine is shut down is what happens if there is a power failure; the most recent data will not be saved. Although chronyd is robust enough to cope with this, some performance may be lost. (The main danger arises if the RTC has been changed during the session, with the trimrtc command in chronyc. Because of this, trimrtc will make sure that a meaningful RTC file is saved out after the change is completed).

The easiest protection against power failure is to put the dump and writertc commands in the same place as the offline command is issued to take chronyd offline; because chronyd free-runs between online sessions, no parameters will change significantly between going offline from the Internet and any power failure.

A final point regards home computers which are left running for extended periods and where it is desired to spin down the hard disc when it is not in use (e.g. when not accessed for 15 minutes). chronyd has been planned so it supports such operation; this is the reason why the RTC tracking parameters are not saved to disc after every update, but only when the user requests such a write, or during the shutdown sequence. The only other facility that will generate periodic writes to the disc is the log rtc facility in the configuration file; this option should not be used if you want your disc to spin down.

Typical configuration files.

To illustrate how a dial-up home computer might be configured, example configuration files are shown in this section.

For the `/etc/chrony.conf' file, the following can be used as an example. NOTE : The server directives are only applicable to customers of Demon Internet; users of other ISPs will need to use their own ISP's NTP servers or public NTP servers.

server 158.152.1.65 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 158.152.1.76 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.159.253.2 minpoll 5 maxpoll 10 maxdelay 0.4 offline
logdir /var/log/chrony
log statistics measurements tracking
driftfile /etc/chrony.drift
keyfile /etc/chrony.keys
commandkey 25
maxupdateskew 100.0
dumponexit
dumpdir /var/log/chrony
rtcfile /etc/chrony.rtc

With Freeserve as the ISP, I use the following server lines :

server 194.152.64.68 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.35 minpoll 5 maxpoll 10 maxdelay 0.4 offline
server 194.152.64.34 minpoll 5 maxpoll 10 maxdelay 0.4 offline

I use pppd for connecting to my ISP. This runs two scripts `/etc/ppp/ip-up' and `/etc/ppp/ip-down'