Building and Installing htpdate as a Package
After ensuring you have a network connection, install checkinstallsudo apt-get install checkinstall
Create a directory to download htpdate
mkdir ~/download/htpdate && cd ~/download/htpdate
Find the URL to the current version of htpdate-xxx.tar.gz here. I am currently using htpdate-1.0.5.tar.gz for this example.
Now download the current version using the URL you discovered above and extract it (of course change the version number if you are not downloading 1.0.5)
wget http://www.vervest.org/htp/archive/c/htpdate-1.0.5.tar.gz && tar -xvfz htpdate-1.0.5.tar.gz
Lets change into the extracted directory and compile the project (takes about 15s on the Model B)
cd htpdate-1.0.5 && make
Now we are ready to build the debian ARM package. (Running this as root is very important)
sudo checkinstall -D make install
Accept the default set of package docs when asked
Should I create a default set of package docs? [y]: y
You will be asked to provide a short description; I chose HTTP Time Protocol. (Note you have to hit ENTER twice)
Please write a description for the package. End your description with an empty line or EOF. >> HTTP Time Protocol >>
You will be asked if you want to change any of the default package values. I opted not to since I am not publishing this package so just hit ENTER to continue.
Congrats. The package has been built and installed. You could even copy the resulting .deb file to another Pi and install it, changing the dir to the dir of your copied deb file.
sudo dpkg -i /home/pi/download/htpdate/htpdate-1.0.5/htpdate_1.0.5-1_armhf.deb
Testing and Verification of htpdate
Now that we have a package and its installed, we can move on to testing that htpdate does what we need.Lets ensure that htpdate can reach out and fetch the dates from some websites that are geographically close to you. -d infers debug and -q says to merely query the sites, don't set the time.
sudo htpdate -d -q www.google.com wwww.nist.gov www.uic.edu
You should see some output similar to the below
#: 3 mean: 0 average: 0.333 Timezone: GMT-6 (CST,CDT) Offset 0.333 seconds
Looks good. Now lets commit and set the date and keep it up to date by running this as a dameon. -D runs htpdate as a dameon, -s will force change the clock on the first poll and then provide adjustments there after, and -l will log the status to the syslog (/var/log/syslog)
sudo htpdate -D -s -l www.google.com wwww.nist.gov www.uic.edu
Making htpdate Startup on Boot
So far we have downloaded the source code, compiled it, created a nice installable and removable debian package, and shown how to query and set the date using htpdate. Unfortnately, if you reboot or have the pi off for long periods of time, your system time will quickly drift. To fix that, we can start the htpdate in dameon mode on boot. This section assumes you have familarity editing text files on linux and I will use nano for my examples but feel free to use your favorite text editor such as vim or emacs.Lets become root
sudo -s
Enter into the init.d directory, and create a new startup script for htpdate, and make it executable
cd /etc/init.d/ && nano htpdate && chmod +x htpdate
Paste the below into your text editor (modify HTPHOSTS with a list of your own geographically close sites if you wish) and then save (CTRL + O) and exit (CTRL + X)
#!/bin/sh ### BEGIN INIT INFO # Provides: htpdate # 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 htpdate in daemon mode ### END INIT INFO # RUNASUSER will generally be pi, unless you've created your specific user account export RUNASUSER='pi' # Space delimited list of sites to pull time info from without the http:// export HTPHOSTS='www.google.com wwww.nist.gov www.uic.edu' case "$1" in start) # if it's start, then start htpdate su $USER -c "/usr/bin/htpdate -D -s -l -i /tmp/htpdate.pid -u nobody:nogroup $HTPHOSTS" echo "Started htpdate for $RUNASUSER using /usr/bin/htpdate -D -s -l -i /tmp/htpdate.pid -u nobody:nogroup $HTPHOSTS" ;; stop) # if it's stop, then just kill the process and delete the pid file pkill -F /tmp/htpdate.pid rm -rf /tmp/htpdate.pid #kill any rogue instances of htpdate killall -q -w htpdate echo "htpdate stopped" ;; *) echo "Usage: /etc/init.d/htpdate {start|stop}" exit 1 ;; esac exit 0
Make sure that you have no mistakes by stopping and starting htpdate
/etc/init.d/htpdate stop && /etc/init.d/htpdate start
You should see that the process started Started htpdate...
Now lets add it to the startup process
update-rc.d htpdate defaults
Congratulations! You can now reboot and htpdate will automatically update the time even if you have a restrictive firewall and will only do so after the network connection has been established.