Etliche Debianer / Ubuntuianer haben das Bedürfnis, ihre Uhr auf dem Server oder Desktop automatisch stellen zu lassen.
Zwei Varianten sind üblich. Einen eigenen ntp Server installieren, der das syncen übernimmt (ntpd) und dann auch noch als Server für andere lokale Rechner übernehmen kann, und das kleinere Paket ntpdate.
Ich benutze ntpdate, weil mir der Aufwand für einen Server zu groß erscheint. Wichtig sollte einem sein, das man einen Pool als Timesserver einträgt, damit man immer einen anderen Server erwischt. Aber das ist ja schon genug diskutiert worden. Interessanter Weise ist man mit dem installieren von ntpdate noch nicht erlöst, denn die Hardwareuhr bekommt damit nicht die aktuelle Uhrzeit, somit ist nach jedem boot die alte leicht ungenaue Uhrzeit wieder aktuell.
Ich habe entdeckt, das es für debian im Paket ntpdate das Script ntpdate-debian gibt. Es liest seine Einstellungen aus /etc/default/ntpdate. Dort sind die debian Pool ntpserver hinterlegt. Da dieses Script also eigentlich nicht zum "normalen" ntpdate Paket gehört, hat es einen angepassten Namen. Ich habe mir das Script nach /usr/local/bin kopiert und um zwei Zeilen ergänzt.
hwclock --adjust Justiert die Hardware Zeit zu Systemzeit Funktion mit einem Korrekturfaktor
hwclock --systohc Schreibt die Systemzeit in die Hardware Uhr
Ausführlich ist das in der manpage von hwclock beschrieben, die ich hier Auszugsweise mal darstelle
The Adjust Function
The Hardware Clock is usually not very accurate. However, much of its inaccuracy is completely predictable - it gains or loses the same amount of time every day. This is called sysâ€
tematic drift. hwclock’s "adjust" function lets you make systematic corrections to correct the systematic drift.
It works like this: hwclock keeps a file, /etc/adjtime, that keeps some historical information. This is called the adjtime file.
Suppose you start with no adjtime file. You issue a hwclock --set command to set the Hardware Clock to the true current time. Hwclock creates the adjtime file and records in it the
current time as the last time the clock was calibrated. 5 days later, the clock has gained 10 seconds, so you issue another hwclock --set command to set it back 10 seconds. Hwclock
updates the adjtime file to show the current time as the last time the clock was calibrated, and records 2 seconds per day as the systematic drift rate. 24 hours go by, and then you
issue a hwclock --adjust command. Hwclock consults the adjtime file and sees that the clock gains 2 seconds per day when left alone and that it has been left alone for exactly one day.
So it subtracts 2 seconds from the Hardware Clock. It then records the current time as the last time the clock was adjusted. Another 24 hours goes by and you issue another hwclock
--adjust. Hwclock does the same thing: subtracts 2 seconds and updates the adjtime file with the current time as the last time the clock was adjusted.
Every time you calibrate (set) the clock (using --set or --systohc ), hwclock recalculates the systematic drift rate based on how long it has been since the last calibration, how long
it has been since the last adjustment, what drift rate was assumed in any intervening adjustments, and the amount by which the clock is presently off.
A small amount of error creeps in any time hwclock sets the clock, so it refrains from making an adjustment that would be less than 1 second. Later on, when you request an adjustment
again, the accumulated drift will be more than a second and hwclock will do the adjustment then.
It is good to do a hwclock --adjust just before the hwclock --hctosys at system startup time, and maybe periodically while the system is running via cron.
The adjtime file, while named for its historical purpose of controlling adjustments only, actually contains other information for use by hwclock in remembering information from one
invocation to the next.
The format of the adjtime file is, in ASCII:
Line 1: 3 numbers, separated by blanks: 1) systematic drift rate in seconds per day, floating point decimal; 2) Resulting number of seconds since 1969 UTC of most recent adjustment or
calibration, decimal integer; 3) zero (for compatibility with clock(8)) as a decimal integer.
Line 2: 1 number: Resulting number of seconds since 1969 UTC of most recent calibration. Zero if there has been no calibration yet or it is known that any previous calibration is moot
(for example, because the Hardware Clock has been found, since that calibration, not to contain a valid time). This is a decimal integer.
Line 3: "UTC" or "LOCAL". Tells whether the Hardware Clock is set to Coordinated Universal Time or local time. You can always override this value with options on the hwclock command
line.
You can use an adjtime file that was previously used with the clock(8) program with hwclock.
Fehlt nur noch ein zünftiger regelmäßiger Aufruf:
#!/bin/sh
#
# cron script to call the ntpdate-debian
# the ntpdate-debian is a enhanced version of the original ntpdate-debian in /usr/sbin
# see my blog zockertown.de/s9y/; search for ntpdate-debian
#
# Written by Bernd Dau bed<@>zockertown.de
#
# in /var/log/ntpdate.log you find a log
if [ -f /usr/local/bin/ntpdate-debian ]; then
/usr/local/bin/ntpdate-debian >>/var/log/ntpdate.log
fi
Weil sich hier eine Grundsatzdiskussion anzubahnen scheint: Nein, ich bin nicht der Meinung, das Meine Lösung die bessere Alternative ist, sondern ich bin ein Pragmatiker, da es auf dem Server nicht zufriedenstellend funktioniert hat, habe ich diese Lösung genommen. Etwas mehr Ehrgeiz hätte mich evtl. auch zur akademischen Lösung bringen können. (Siehe auch rorschachstagebuch und christoph-langner)