Setting The Clock

The clock on the Mythbuntu system must be kept synchronized with a source of accurate time, in order for programs to be recorded at the correct start/stop times. Typically, NTP is used for this job. Mythbuntu comes precofigured with NTP installed and running.

To set up NTP properly, you will need to edit the config file and point it at two or three NTP servers. To do so:

     sudoedit /etc/ntp.conf

After the line that says "server ntp.ubuntu.com", you can add a few more servers, like this:

     server ntp.aserver.com

Look at the following list of public NTP servers and pick a couple more stratum 2 servers to synchronize with. Servers, from the list, that are close to you will provide better synchronization:

     http://support.ntp.org/bin/view/Servers/StratumTwoTimeServers

If you have a local clock that is regulated well enough, you can synchronize your Mythbuntu systems to it.

/etc/ntp.conf:

     Here is a complete replacement for /etc/ntp.conf that synchronizes the
     system to a local clock.  Note that the local server is marked as a true
     chimer by the "true" keyword and it is also marked as the preferred server.
     Unfortunately, the "true" keyword seems not to work in the earlier versions
     of NTP (4.2.4) that are installed on some of the earlier Mythbuntu
     versions (e.g. 9.10) so you may have to take other steps (see below).
     Here is the file:
     #
     # /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help.
     # Read by ntpd at startup.
     #
     # For ntpd version 4.2.6.
     #
     #
     # Prohibit general access to this service.
     #
     restrict default ignore
     #
     # Local users may interrogate the ntp server more closely.
     #
     restrict 127.0.0.1
     restrict ::1
     #
     # Permit time synchronization with our local time source but do not
     # permit the source to query or modify the service on this system.
     #
     restrict 192.168.1.1
     #
     # Synchronize this system with our local NTP server.
     #
     server 192.168.1.1 true prefer minpoll 4 maxpoll 5
     #
     # Undisciplined Local Clock.  This is a fake driver intended for backup and
     # when no outside source of synchronized time is available.  The normal,
     # running stratum is usually 3, but in this case we elect to use stratum
     # 10.  Since the server line does not have the prefer keyword, this clock
     # is never used for synchronization, unless no other other synchronization
     # source is available.
     #
     # Actually, this happens way more than you'd think it might so it is
     # important to have this clock defined to keep things ticking along.
     #
     server 127.127.1.0
     fudge  127.127.1.0 stratum 10
     #
     # Drift file.  Put this in a directory which the daemon can write to.  No
     # symbolic links allowed, either, since the daemon updates the file by
     # creating a temporary file in the same directory and then renameing it to
     # the file named.
     #
     # This file is read at startup to give the daemon a leg up on getting
     # running without having to go through the laborious chore of figuring out
     # drift from scratch.
     #
     driftfile /var/lib/ntp/ntp.drift
     #
     # Enable this if you want statistics to be logged.
     #
     #statsdir /var/log/ntp/
     #
     # Statistics to be logged.
     #
     statistics loopstats peerstats clockstats
     filegen loopstats file loopstats type day enable
     filegen peerstats file peerstats type day enable
     filegen clockstats file clockstats type day enable

/etc/init.d/ntpd:

     You may also need to alter the NTP startup script to begin by synchronizing
     the Mythbuntu machine's clock with NTP on the reference clock's machine.
     This is necessary if there can be large amounts of drift between the
     reference clock and the local clock when the Mythbuntu machine is down.  As
     is well known, NTP will not synchronize clocks that are too far apart, hence
     the need to do it manually at startup.  The following is a complete
     replacement for the ntpd script that carries out synchronization at startup
     as well as fixes a few other problems.  You may wish to use it in lieu of the
     script provided by Mythbuntu:
     #!/bin/sh
     ### BEGIN INIT INFO
     # Provides:        ntp
     # Required-Start:  $network $remote_fs $syslog
     # Required-Stop:   $network $remote_fs $syslog
     # Default-Start:   2 3 4 5
     # Default-Stop:    0 1 6
     # Short-Description: Start NTP daemon
     ### END INIT INFO
     PATH=/sbin:/bin:/usr/sbin:/usr/bin
     . /lib/lsb/init-functions
     NAME=ntp
     DAEMON=/usr/sbin/ntpd
     PIDFILE=/var/run/ntpd.pid
     # Use a local clock as the step ticker at startup.
     STEP_TICKER=192.168.1.1
     test -x $DAEMON || exit 5
     if [ -r /etc/default/$NAME ]; then
         . /etc/default/$NAME
     fi
     if [ -e /etc/ntp.conf.dhcp ]; then
         NTPD_OPTS="$NTPD_OPTS -c /etc/ntp.conf.dhcp"
     fi
     # Running as user ntp doesn't cut it.
     # RUNASUSER=ntp
     RUNASUSER=root
     UGID=$(getent passwd $RUNASUSER | cut -f 3,4 -d:) || true
     case $1 in
         start)
             log_daemon_msg "Synchronizing with $STEP_TICKER" "ntpd"
             ntpdate $STEP_TICKER
             log_daemon_msg "Starting NTP server" "ntpd"
             if [ -z "$UGID" ]; then
                 log_failure_msg "user \"$RUNASUSER\" does not exist"
                 exit 1
             fi
             start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE \
                 --startas $DAEMON -- -p $PIDFILE -u $UGID $NTPD_OPTS
             log_end_msg $?
             ;;
         stop)
             log_daemon_msg "Stopping NTP server" "ntpd"
             start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
             log_end_msg $?
             rm -f $PIDFILE
             ;;
         restart|force-reload)
             $0 stop && sleep 2 && $0 start
             ;;
         try-restart)
             if $0 status >/dev/null; then
                 $0 restart
             else
                 exit 0
             fi
             ;;
         reload)
             exit 3
             ;;
         status)
             pidofproc -p $PIDFILE $DAEMON >/dev/null
             status=$?
             if [ $status -eq 0 ]; then
                 log_success_msg "NTP server is running."
             else
                 log_failure_msg "NTP server is not running."
             fi
             exit $status
             ;;
         *)
             echo "Usage: $0 {start|stop|restart|try-restart|force-reload|\
                             status}"
             exit 2
             ;;
     esac

For NTP to work well, the local clock should run fairly closely to the clock that you are trying to synchronize it with. If it does not, you can try adjusting the clock, using the tickadj command. The default value for the tick (usually 10000) can be found by running:

     tickadj

If the clock is constantly running behind, you can cause it to slew ahead a bit, thereby regulating it more closely to the source you are trying to synchronize with. Small deviations from the default value are usually all that is required. For example:

     tickadj 9990

If the clock is constantly running ahead, you can cause it to slew behaind a bit, thereby regulating it more closely to the source you are trying to synchronize with. Once again, small deviations from the default value are usually all that is required. For example:

     tickadj 10010

If you cannot keep the clock on the Mythbuntu machine synchronized using NTP, because the clock you are trying to synchronize against is too poorly regulated or for other reasons, and the "true" keyword does not force NTP on the Mythbuntu machine to use the clock anyway (as is the case on earlier versions of Mythbuntu, such as 9.10), you may have to resort to synchronizing it at regular intervals using ntpdate.

The first step is to stop the NTP daemon from running. Begin by checking to see if it is actually installed and being started as a system service. Unfortunately, the update-rc.d command on Mythbuntu does not provide any "List" option to show whether a system service is set up to be run automatically. To find this out, you can do:

     ls -l /etc/rc.d/ntp

Look at the results of this command. If the symlink to /etc/init.d/ntp starts with 'K', NTP is shutdown at the runlevel given by "rcx". If the symlink starts with 'S', NTP is started at the runlevel given by "rcx". If NTP is started at runlevels 2, 3, 4, or 5, you will need to stop it. To do this, using the dain-bramaged update-rc.d, run:

     sudo /etc/init.d/ntp stop
     sudo rm -f /etc/rc.d/ntp
     sudo update-rc.d ntp stop 20 0 1 2 3 4 5 6 .

However, even this may not be sufficient to stop the ntp daemon. On some versions of Mythbuntu, this daemon is automatically started at boot time, regardless of the symlinks in /etc/init.d. If this is the case with your system, see the additional line to be added to crontab (below).

Incidentally, if you ever need to put NTP back the way it was originally installed, remove the crontab entries for ntpdate (see below) and run:

     sudo rm -f /etc/rc.d/ntp
     sudo update-rc.d ntp stop 20 0 1 6 . start 20 2 3 4 5 .
     sudo /etc/init.d/ntp start

You'll then need to add a line or two to crontab to cause ntpdate to be run often enough to keep the clocks synchronized. Using the editor:

     sudoedit /etc/crontab

Add something like the following:

     # Run ntpdate at regular intervals to synchronize the clock (ntp itself fails
     # in this respect).  Note that some annoying piece of s**t keeps starting the
     # ntp daemon, which prevents ntpdate from working.  So, just before we run
     # ntpdate, we kill the daemon.
     55 * * * * root /etc/init.d/ntp stop 2>&1 >/dev/null
     56 * * * * root /usr/sbin/ntpdate 192.168.1.1 2>&1 >/dev/null

Note that you best ensure that this is working. To do so, you can issue the ntpdate command periodically until you are sure all is well:

     /usr/sbin/ntpdate 192.168.1.1

will windge if the daemon is still running. If so, figure out why and fix it. Its no fun having 56-minute hour shows.