December 15, 2013

Setting up NTP

My BBB came with the ntpdate command, so I can use the following command to set the time by hand.
su
ntpdate -b -s -u pool.ntp.org
This works fine, but I keep forgetting to do it, then I get ridiculous timestamps on my files, make complains about clock skew, and I am generally annoyed.

And the BBB has no handy rc.local file, which would be a perfect place to put the above command as a quick and easy solution. Apparently some pinhead somewhere decided that there was something evil about rc.local and wants to force us all to deal with systemd.

The ntpdate service

Pretty soon I discover there are two services (ntpd and ntpdate). The first question that comes to my mind is why they both exist. More particularly why I need the ntpdate service if I go ahead and run ntpd. Apparently the ntpdate service is a one-time thing that trys to set the clock on system boot. After that the ntpd service will keep it up to date.

It turns out that the ntpdate "one time" service is indeed a proper thing on a system like the BBB without a hardware clock. My regular desktop linux (fedora) systems do not have an ntpdate service. They do have some kind of reasonable battery clock - so they have a half-way decent time right after boot. This is not true of the BBB which thinks it is Jan 1, 2000 after every boot. It turns out that ntp will refuse to adjust the time (unless a special switch is given) if the time error is too great.

Set up and run the ntpdate service

You get this out of the box with the BBB, so it makes a certain amount of sense to try to make it run before going any further. The first thing I try is editing the file /etc/defaults/ntpdate. I tell it to use pool.ntp.org as a server and set the "yes" to set the hardware clock. Then:
su
systemctl enable ntpdate.service
systemctl start ntpdate.service
reboot
To all appearances this does nothing, but you have to be patient as it turns out. Even after the reboot, the time is January 1, 2000. And if you look at the logs using the following command, you see the following error:
journalctl | grep ntp
Can't find host pool.ntp.org: Name or service not known (-2)
This looks like one of the usual dependency issues that plague systemd. It is trying to do the first time sync before the network is up, or something like that. However if you wait a while, voila! Apparently the ntpdate.service tries again later and is able to get a time sync.

The next thing to do is to set the timezone. I am in Arizona which has special rules (it is MST without daylight savings time). When I look in /usr/share/zoneinfo/America (and elsewhere) there is no rule that is correct for Arizona. What I do is to copy an Arizona entry from a Fedora linux system and put it in /usr/share/zoneinfo/America/Arizona (and cross my fingers). Then:

su
cd /etc
echo "Arizona" >timezone
rm localtime
ln -s /usr/share/zoneinfo/America/Arizona localtime
After this I can type "date" and I am getting the correct time. And it works just fine after a reboot (it is now keeping the time across a boot and making a correction later, or something like that).

If you are really curious, take a look at:

Get the ntp package

You may or may not care about this if you get the ntpdate service to work, as discussed above. If you want a disciplined clock, you will also want to run ntpd, and to do that you will need the "ntp" package.
opkg update
opkg install ntp

XXX

You can read past here if you like, but be warned that this documents some things I did and a state of confusion that I never fully sorted out. At this point I am happy with what the ntpdate.service is doing for me all by itself.

Some links

Then I edit /etc/ntp.conf and comment out the server and fudge lines. I also dig up some NTP servers and add them, commenting out the bogus server entry that this package ships with.
Then I issue these commands:
systemctl enable ntpd.service
systemctl enable ntpdate.service
Then I edit the file /lib/systemd/system/ntpdate.service and change one line to two:
ExecStart=/usr/bin/ntpdate-sync silent
   - change to: ...
ExecStart=/usr/bin/ntpd -q -g -x
ExecStart=/sbin/hwclock --systohc
I found that this was not working. And some investigation revealed that the ntpdate service was in conflict with the ntpd service (both trying to use port 123), so I disabled ntpdate. Things still do not work. Very confusing -- not to mention irritating.
Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org