Advertise with Googlier.com pages tagged admin http://www.bitpipeline.eu/tags/admin/ BitPipeline ikiwiki Mon, 23 Sep 2013 11:24:57 +0200 Keeping the clock on time http://www.bitpipeline.eu/Blog/20120324-Keeping_the_clock_on_time/ http://www.bitpipeline.eu/Blog/20120324-Keeping_the_clock_on_time/ OS admin bitpipeline-it linux Sat, 24 Mar 2012 03:27:00 +0100 2013-09-23T09:24:57Z <p>Keeping the computer clock synchronize as close as possible with the rest of the world is important for a number of reasons, form just being on time when the calendar events warning goes off, to more IT stuff like making sure that file synchronization with remote servers works properly.</p> <p>The network protocol everyone uses for this is <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Network_Time_Protocol">NTP</a> (Network Time Protocol) and there are several implementation one can choose from (and as usually Microsoft totally messed it up with it's windows time service).</p> <p>On a device that is most of the times connected, although it's to different networks, but is sometimes on its own I think it makes sense to use something that can adapt itself to its environment. So I went in search for a NTP program that was created with this on/off in mind and behold I found <a href="http://chrony.tuxfamily.org/">chrony</a>, a small and easy to configure NTP client and server (I'll be using it only as a client).</p> <p>When installing <a href="https://www.gentoo.org">gentoo</a> (my favourite linux distribution) on my current laptop I decided to give NetworkManager another try. Although it's a bloated piece of software (just look at the number of dependencies it has) it has become more or less pervasive in the Linux desktop (what a pitty) so I guess I should get familiar with it. Somewhere in the not so distant past it also started to allow for scripts to be run when a network interface became connected or disconnected, unfortunately in a very basic way. When any of those events takes place the scripts that are in the <code>dispatcher.d</code> directory will be called with two arguments: the name of the interface and the "status" (up or down).</p> <p>Rather basic but enough to use chrony.</p> <p>First step is of course installing chrony. Then create a file that will contain a key for the access to the chrony daemon. I created it in <code>/etc/chrony/chrony.keys</code>. The content of that file is just one number and a string separated by a space. Several keys can be defined by using several lines, although I don't really understand the advantage of that. Something like:</p> <pre>1 keypassword 2 secondkey</pre> <p>After creating the keys file we can now edit <code>/etc/chrony/chrony.conf</code> to configure the daemon. Here is my configuration:</p> <pre>## selected servers by running ## netselect -s 4 -t 3 pool.ntp.org server 95.211.148.1 offline server 83.98.155.30 offline keyfile /etc/chrony/chrony.keys commandkey 1 driftfile /etc/chrony/chrony.drift</pre> <p>You can see that I set two NTP servers but mark them as being offline. I will use a NetworkManager dispatcher script to bring them on or off line. <a href="http://alumnit.ca/~apenwarr/netselect/index.html">netselect</a> is a wonderful tool to find out the IP addresses of servers that are closest to you.</p> <p>This static definition of the NTP servers is a Achilles heel of this current configuration and I might look into making it more dynamic in the future (for example the dhcp lease might include information on NTP server in the current network).</p> <p>Then at last we have to provide the dispatch script for NetworkManager to use. I named it <code>10.chrony</code> and placed it in the directory <code>/etc/NetworkManager/dispatcher.d</code>:</p> <!-- Generator: GNU source-highlight 3.1.9 by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> <pre><tt><font color="#000000"> 1:</font> <i><font color="#9A1900">#!/bin/bash</font></i> <font color="#000000"> 2:</font> <font color="#000000"> 3:</font> <font color="#009900">INTERFACE</font><font color="#990000">=</font><font color="#009900">$1</font> <i><font color="#9A1900"># The interface which is brought up or down</font></i> <font color="#000000"> 4:</font> <font color="#009900">STATUS</font><font color="#990000">=</font><font color="#009900">$2</font> <i><font color="#9A1900"># The new state of the interface</font></i> <font color="#000000"> 5:</font> <font color="#000000"> 6:</font> <font color="#009900">CHRONY_PASSWORD</font><font color="#990000">=</font>`cat /etc/chrony/chrony<font color="#990000">.</font>keys <font color="#990000">|</font> cut -d <font color="#FF0000">" "</font> -f <font color="#993399">2</font>` <font color="#000000"> 7:</font> <font color="#000000"> 8:</font> <b><font color="#0000FF">case</font></b> <font color="#FF0000">"$STATUS"</font> <b><font color="#0000FF">in</font></b> <font color="#000000"> 9:</font> <font color="#FF0000">'up'</font><font color="#990000">)</font> <i><font color="#9A1900"># $INTERFACE is up</font></i> <font color="#000000"> 10:</font> <font color="#000000"> 11:</font> /usr/bin/chronyc <font color="#990000">&lt;&lt;</font>EOF <font color="#000000"> 12:</font> password <font color="#009900">$CHRONY_PASSWORD</font> <font color="#000000"> 13:</font> online <font color="#000000"> 14:</font> EOF <font color="#000000"> 15:</font> <font color="#000000"> 16:</font> <font color="#990000">;;</font> <font color="#000000"> 17:</font> <font color="#FF0000">'down'</font><font color="#990000">)</font> <i><font color="#9A1900"># $INTERFACE is down</font></i> <font color="#000000"> 18:</font> <i><font color="#9A1900"># Check for active interface and down if no one active</font></i> <font color="#000000"> 19:</font> <font color="#000000"> 20:</font> <b><font color="#0000FF">if</font></b> <font color="#990000">[</font> <font color="#990000">!</font> `nm-tool<font color="#990000">|</font>grep State<font color="#990000">|</font>cut -f<font color="#993399">2</font> -d<font color="#FF0000">' '</font>` <font color="#990000">=</font> <font color="#FF0000">"connected"</font> <font color="#990000">];</font> <b><font color="#0000FF">then</font></b> <font color="#000000"> 21:</font> <font color="#000000"> 22:</font> /usr/bin/chronyc <font color="#990000">&lt;&lt;</font>EOF <font color="#000000"> 23:</font> password <font color="#009900">$CHRONY_PASSWORD</font> <font color="#000000"> 24:</font> offline <font color="#000000"> 25:</font> EOF <font color="#000000"> 26:</font> <font color="#000000"> 27:</font> <b><font color="#0000FF">fi</font></b> <font color="#000000"> 28:</font> <font color="#990000">;;</font> <font color="#000000"> 29:</font> <b><font color="#0000FF">esac</font></b></tt></pre> <p>What the script does is read the key from the chrony key file and then use the chrony command line interface to change its state to online or offline (only if all interface are offline).</p>