Setting the Time on a Once-off Basis

On my current distribution the Beaglebone Black does not come with a working NTP installation in order to set the time. Because the board doesn’t have a battery backup, this means that we need a way to set the time when the board is running. If you wish to set the time you could do it on a once-off basis…

When you boot the Beaglebone black, the time comes up as:

Which is quite some time ago! I noticed this because I did a git commit and in the github status it said that I did the update 13 years ago!

To set the date once off you could use something like:

However, if you reboot your Beaglebone the time will be back to the good old year 2000. So, we need a way to update this each time the Beaglebone boots.

Install the NTP Software

Next, we need to install the ntp software on the Beaglebone black – so, at the command prompt (I am using Angstrom here):

Next, find a NTP server that is close to your location. We need to do this to be good ‘ntp citizens’ as it is not good to use a NTP root server as they are already heavily loaded … it is better to use a NTP pool server that is close to your location in order to help with load balancing.

A Google search for my location “ntp server ireland” returns the website http://www.pool.ntp.org/ which you can search for your location. For Ireland I get:

So, I have edited my /etc/ntp.conf file to contain:

The driftfile specifies the file that is used to store information about previous responses from the NTP servers you are using. This file contains internal information for NTP – leave it at /etc/ntp.drift

If you only want to allow machines within your own network to synchronize their clocks with your server, but ensure they are not allowed to configure the server, set the restrict value as above. In my case, 192.168.1.0 is the IP address of my network and 255.255.255.0 is my network’s netmask.

Important – make sure that you comment out the server and fudge lines or the server will sync to itself!

Setting the logfile above is very useful as it allows you to check the /var/log/ntpd.log file to see what is going wrong if you are having problems.

Set your localtime file according to your time zone

Next you need to set your /etc/localtime file according to your timezone. If you go into the directory /usr/share/zoneinfo you will see a lot of different timezone files, e.g.

You can copy the file directly or use one of the presets. In my case, Irish time is the same as London time. So,

So, I am going to use the London file – back to the /etc directory:

by setting a symbolic link from /etc/localtime to the timezone file. The advantage of this over copying the file is that you can see the timezone that was set when you ls:

Okay, next we need to start the services. Follow the steps carefully…

Enabling the NTP Services

Setup the ntpd server by typing:

This installs two services:

and a second service:

However, and importantly on the Beaglebone black you have to modify the second service to:

So, importantly you must replace the line “ExecStart=/usr/bin/ntpdate-sync silent” with the line “ExecStart=/usr/bin/ntpd -q -g -x”. The time will not automatically update unless you make this change. The reason for this is given in the ntpd man page (http://linux.die.net/man/8/ntpd):

Most operating systems and hardware of today incorporate a time-of-year (TOY) chip to maintain the time during periods when the power is off. When the machine is booted, the chip is used to initialize the operating system time. After the machine has synchronized to a NTP server, the operating system corrects the chip from time to time. In case there is no TOY chip or for some reason its time is more than 1000s from the server time, ntpd assumes something must be terribly wrong and the only reliable action is for the operator to intervene and set the clock by hand. This causes ntpd to exit with a panic message to the system log. The -g option overrides this check and the clock will be set to the server time regardless of the chip time. However, and to protect against broken hardware, such as when the CMOS battery fails or the clock counter becomes defective, once the clock has been set, an error greater than 1000s will cause ntpd to exit anyway.

Finally, reboot and the date/time should be fine:

Note: If your ssh client ever hangs on a reboot type ~. and it should kill the ssh client session.

When the system comes up:

And, this could be the case from now on. Enjoy, and remember to remain a good NTP citizen.

Fixing the Hardware Clock

Thanks to Louis Thiery in the comments below – he pointed out that the RTC Time was still out after you perform the steps above. I have made one additional change to the configuration and it seems to do the trick – It is probably a bit of a hack! but it seems to work…

Modify the ntpdate.service file to add one more line:

that calls the hwclock and requests it to use the system time to set the hardware clock. This appears to work  as when you reboot and call timedatectl, you get the following output:

You can see here that the RTC time is now the same as Local time and all appears to be in order – And, in case you are concerned, yes, it is actually 1:06am, time for my bed!