home *** CD-ROM | disk | FTP | other *** search
/ PC World 1999 August / PCWorld_1999-08_cd.bin / doc / HOWTO / mini / Clock < prev    next >
Text File  |  1998-05-08  |  11KB  |  331 lines

  1.   The Clock Mini-HOWTO
  2.   Ron Bean, rbean@execpc.com
  3.   v, December 1996
  4.  
  5.   How to set and keep your computer clock on time.
  6.   ______________________________________________________________________
  7.  
  8.   Table of Contents
  9.  
  10.  
  11.   1. Introduction
  12.  
  13.   2. Using the ``
  14.  
  15.      2.1 Checking your installation
  16.      2.2 Measuring your clock's drift rate
  17.      2.3 Example
  18.         2.3.1 To set time
  19.         2.3.2 To reset time and check drift rate
  20.         2.3.3 Calculating the correction factor
  21.  
  22.   3. A few words about xntpd
  23.  
  24.  
  25.  
  26.   ______________________________________________________________________
  27.  
  28.   1.  Introduction
  29.  
  30.   The real-time-clock chips used on PC motherboards (and even expensive
  31.   workstations) are notoriously inaccurate. Linux provides a simple way
  32.   to correct for this in software, making the clock potentially *very*
  33.   accurate even without an external time source. But most people don't
  34.   seem to know about it, for several reasons:
  35.  
  36.   1. It's not mentioned in most of the general ``how to set up linux''
  37.      documentation, and it would be difficult to set up automatically at
  38.      install time (although not impossible in theory, if you have a
  39.      modem).
  40.  
  41.   2. If you check ``man clock'' you'll get clock(3), which is not what
  42.      you want. (try ``man 8 clock'').
  43.  
  44.   3. Most people don't seem to care what time it is anyway.
  45.  
  46.   4. Those few who do care often want to use the xntpd package from
  47.      louie.udel.edu to sync to an external time source, such as a
  48.      network time server or radio clock.
  49.  
  50.   This mini-HOWTO describes the low-tech approach.  If you're at all
  51.   interested in this sort of thing, I highly recommend that you spend
  52.   some time at <http://www.eecis.udel.edu/~ntp/> which includes all
  53.   kinds of interesting stuff, including complete info on xntpd and links
  54.   to NIST and USNO (I have a few more comments on xntpd at the end.)
  55.  
  56.  
  57.      Note
  58.         If you run more than one OS on your machine, you should only let
  59.         one of them reset the CMOS clock, so they don't confuse each
  60.         other.  If you regularly run both Linux and Windows on the same
  61.         machine, you may want to check out some of the shareware clock
  62.         programs that are available for Windows instead (follow the
  63.         links from the URL above).
  64.  
  65.  
  66.  
  67.   2.  Using the `` clock '' program
  68.  
  69.   Everything you need to know is in the clock(8) man page, but this
  70.   mini-HOWTO will walk you through the process.
  71.  
  72.  
  73.      Note
  74.         You must be root to run ``clock'', or any other program that
  75.         affects either the system time or the CMOS clock.
  76.  
  77.   2.1.  Checking your installation
  78.  
  79.   Check your system startup files for a command like ``clock -a'' or
  80.   ``clock -ua''.  Depending on which distribution you're using, it might
  81.   be in /etc/rc.local, or /etc/rc.d/rc.sysinit, or some similar place.
  82.  
  83.   If it says ``clock -s'' or ``clock -us'', change the ``s'' to an
  84.   ``a'', and then check to see if you have the file /etc/adjtime, which
  85.   contains a single line that looks something like this:
  86.  
  87.  
  88.        0.000000 842214901 0.000000
  89.  
  90.  
  91.  
  92.  
  93.   These numbers are the correction factor (in seconds per day), the time
  94.   the clock was last corrected (in seconds since Jan 1, 1970), and the
  95.   partial second that was rounded off last time.  If you don't have this
  96.   file, login as root and create it, with a single line that looks like
  97.   this (all zeros):
  98.  
  99.  
  100.        0.0 0 0.0
  101.  
  102.  
  103.  
  104.  
  105.   Then run ``clock -a'' or ``clock -ua'' manually from the shell to
  106.   update the 2nd number (use the ``u'' if your clock is set to Universal
  107.   instead of local time).
  108.  
  109.   2.2.  Measuring your clock's drift rate
  110.  
  111.   First, you need to know what time it is :-).  Your local time of day
  112.   number may or may not be accurate.  My favorite method is to call
  113.   WWV's voice announcment at (303)499-7111 (this is a toll call).  If
  114.   you have access to a network time server, you can use the ntpdate
  115.   program from the xntpd package (use the -b flag to keep the kernel
  116.   from messing with the CMOS clock).  Otherwise use ``date -s hh:mm:ss''
  117.   to set the kernel time by hand, and then ``clock -w'' to set the CMOS
  118.   clock from the kernel clock.  You'll need to remember when you last
  119.   set the clock, so write down the date someplace where you won't lose
  120.   it.  If you used ntpdate, do ``date +%s'' and write down the number of
  121.   seconds since Jan 1,1970.
  122.  
  123.   Then come back some days or weeks later and see how far the clock has
  124.   drifted.  If you're setting the clock by hand, I'd recommend waiting
  125.   at least two weeks, and only calculate the drift rate to the nearest
  126.   .1 sec/day.  After several months you could get to the nearest .01
  127.   sec/day (some people claim more accuracy than that, but I'm being
  128.   conservative here).  If you use ntpdate you shouldn't have to wait
  129.   that long, but in any case you can always fine-tune it later.
  130.  
  131.   You can have cron run ``clock -a'' at regular intervals to keep the
  132.   system time in line with the (corrected) CMOS time.  This command will
  133.   also be run from your startup file every time you boot the system, so
  134.   if you do that often (as some of us do), that may be enough for your
  135.   purposes.
  136.  
  137.   Note that certain programs may complain if the system time jumps by
  138.   more than one second at a time, or if it jumps backwards.  If you have
  139.   this problem, you can use xntpd or ntpdate to correct the time more
  140.   gradually.
  141.  
  142.   2.3.  Example
  143.  
  144.   2.3.1.  To set time
  145.  
  146.   Login as root.  Dial (303)499-7111 (voice), listen for time
  147.   announcement.  Then type:
  148.  
  149.  
  150.        date -s hh:mm:ss
  151.  
  152.  
  153.  
  154.  
  155.   but don't press enter until you hear the beep.  (You could use ``ntp¡
  156.   date'' here instead of ``date'', and skip the phone call) This sets
  157.   the ``kernel time''.  Then type:
  158.  
  159.  
  160.        clock -w
  161.  
  162.  
  163.  
  164.  
  165.   This sets the CMOS time to match the kernel time.  Then type:
  166.  
  167.  
  168.        date +%j
  169.  
  170.  
  171.  
  172.  
  173.   (or ``date +%s'' if you used ``ntpdate'' instead of ``date'' above)
  174.   and write down the number it gives you for next time.
  175.  
  176.   2.3.2.  To reset time and check drift rate
  177.  
  178.   Find the date you wrote down last time.  Login as root Then type:
  179.  
  180.  
  181.        clock -a
  182.  
  183.  
  184.  
  185.  
  186.   This sets the kernel time to match the current CMOS time.  Dial
  187.   (303)499-7111 (voice), listen for announcement.  Then type:
  188.  
  189.  
  190.        date
  191.  
  192.  
  193.  
  194.  
  195.   and press enter when you hear the beep, but while you're waiting,
  196.   write down the time they announce, and don't hang up yet.  This tells
  197.   you what time your machine thought it was, when it should have been
  198.   exactly on the minute.  Now type in
  199.        date hh:mm:00
  200.  
  201.  
  202.  
  203.  
  204.   using the minute *after* the one that was just announced, and press
  205.   enter when you hear the beep again (now you can hang up).  For hh use
  206.   the local hour.  This sets the ``kernel time''.  Then type:
  207.  
  208.  
  209.        clock -w
  210.  
  211.  
  212.  
  213.  
  214.   which writes the new (correct) time to the CMOS clock.  Now type:
  215.  
  216.  
  217.        date +%j
  218.  
  219.  
  220.  
  221.  
  222.   (or ``date +%s'' if that's what you used before)
  223.  
  224.   You now have three numbers (two dates and a time) that will allow you
  225.   to calculate the drift rate.
  226.  
  227.   2.3.3.  Calculating the correction factor
  228.  
  229.   When you ran ``date'' on the minute, was your machine slow or fast?
  230.   If it was fast, you'll have to subtract some number of seconds, so
  231.   write it down as a negative number.  If it was slow, you have to add
  232.   some seconds, so write it down as positive.
  233.  
  234.   Now subtract the two dates.  If you used ``date +%j'', the numbers
  235.   represent the day-of-year (1-365, or 1-366 in leap years).  If you've
  236.   passed Jan 1 since you last set the clock you'll have to add 365 (or
  237.   366) to the 2nd number.  If you used ``date +%s'' then your number is
  238.   in seconds, and you'll have to divide it by 86400 to get days.
  239.  
  240.   If you already had a correction factor in /etc/adjtime, you'll have to
  241.   account for the number of seconds you've already corrected.  If you've
  242.   overcorrected, this number will have the opposite sign of the one you
  243.   just measured; if you've undercorrected it will have the same sign.
  244.   Multiply the old correction factor by the number of days, and then add
  245.   the new number of seconds (signed addition-- if the two numbers have
  246.   the same sign, you'll get a larger number, if they have opposite
  247.   signs, you'll get a smaller number).
  248.  
  249.   Then divide the total number of seconds by the number of days to get
  250.   the new correction factor, and put it in /etc/adjtime in place of the
  251.   old one.  Write down the new date (in seconds or days) for next time.
  252.  
  253.   Here's what my /etc/adjtime looks like:
  254.  
  255.  
  256.        -9.600000 845082716 -0.250655
  257.  
  258.  
  259.  
  260.  
  261.   (note 9.6 seconds per day is nearly five minutes per month!)
  262.  
  263.  
  264.  
  265.   3.  A few words about xntpd
  266.  
  267.   Your system actually has two clocks-- the battery powered ``real time
  268.   clock'' that keeps track of time when the system is turned off (also
  269.   known as the ``CMOS clock'', ``Hardware clock'', or ``RTC'') and the
  270.   ``kernel time'' (sometimes called the ``software clock'' or ``system
  271.   clock'') which is based on the timer interrupt and is initialized from
  272.   the CMOS clock at boot time.  The two will drift at different rates,
  273.   so they will gradually drift apart from each other, and also away from
  274.   the ``real'' time.
  275.  
  276.   All references to ``the clock'' in the xntpd documentation refer to
  277.   the ``kernel clock''.  When you run xntpd or timed (or any other
  278.   program that uses the adjtimex system call), the linux kernel assumes
  279.   that the kernel clock is more accurate than the CMOS clock, and resets
  280.   the CMOS time every 11 minutes from then on (until you reboot the
  281.   machine).  This means that ``clock'' no longer knows when the CMOS
  282.   clock was last reset, so you can't use the correction factor in
  283.   /etc/adjtime.  You can use ntpdate in your startup file to initially
  284.   set the clock from a timeserver before starting xntpd.  If you don't
  285.   always have access to an accurate time source when you boot the
  286.   machine, this can be a bit awkward-- xntpd isn't really designed to be
  287.   used in situations like that.
  288.  
  289.   Xntpd includes drivers for many radio clocks, and can also be set to
  290.   call NIST's dial-up time service at regular intervals (be sure to
  291.   calculate the effect on your phone bill when setting the interval
  292.   between calls).  It can also apply a correction factor to the kernel
  293.   clock if it loses contact with its other sources for an extended
  294.   period of time.
  295.  
  296.   Most radio clocks cost $3-4K, but you can get plans for an inexpensive
  297.   ``gadget box'' (actually a 300 baud modem) that sits between your
  298.   computer and any shortwave radio tuned to Canada's CHU time station
  299.   (see  <ftp://ftp.udel.edu/pub/ntp/gadget.tar.Z>).  The Heathkit WWV
  300.   receiver (the ``Most Accurate Clock'') is also still available
  301.   (although not as a kit), and costs around $4-500.  GPS signals also
  302.   contain time information, and some GPS recievers can connect to a
  303.   serial port.  This may become the low cost solution in the near
  304.   future.
  305.  
  306.   In theory, someone could write a program to use NIST's dial up time
  307.   service to calculate the drift rate of both the CMOS clock and the
  308.   kernel clock automatically.  I am not aware of any stand-alone program
  309.   to do this, but most of the code could probably be borrowed from
  310.   xntpd.
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.