Time Synchronisation on OpenWrt
This page contains an overview on how to configure time synchronisation on
a Linksys WRT54GS running OpenWrt
(also referred to as "time synchronization" in some countries).
Introductory Information
The WRT doesn't contain a hardware clock, so the clock is reset each time the WRT is rebooted.
Even setting the clock to the correct time after a reboot isn't sufficient, as I've found the
clock drifts a lot over time (upto several minutes per hour).
The solution to this is to synchronise the clock when the WRT is rebooted, and then to periodically
synchronise it once the WRT is running.
Note that you'll need to have cron running on your WRT,
to be able to create a scheduled task to perform the time synchronisation.
Time Synchronisation
Time Zone
Prior to performing time synchronisation, you should ensure that the time zone is set correctly on
your WRT, by checking the value in /etc/TZ.
I'm located in Western Australia (which is GMT+8), so I set the time zone using:
echo GMT-8 > /etc/TZ
Refer to this page
for details of the valid values for TZ.
Confusingly, the offset in the TZ value specifies the time value you must add to the local time
to get a UTC value, so this is positive if the local time zone is west of the Prime Meridian
and negative if it is east. As a result, TZ must be set to GMT-8
for a timezone that is GMT+8.
Install IPK Package
Install the ntpclient package on OpenWrt:
ipkg install ntpclient
In addition to installing the ntpclient components, this package may also create
an init script, /etc/init.d/S55ntpclient, with the following
contents:
#!/bin/sh
/usr/sbin/ntpclient -c 1 -s -h pool.ntp.org &
This will cause ntpclient to attempt a once-off time synchronisation to
pool.ntp.org at boot time.
Additional parameters can also be passed to ntpclient to tell it to periodically perform
a time synchronisation with a specified host.
Refer to the ntpclient man page
for more information on command line options for ntpclient.
Usage information is as follows:
Usage: ntpclient [-c count] [-d] [-g goodness] -h hostname [-i interval]
[-l] [-p port] [-r] [-s]
I've found if the initial time synchronisation fails (ie, no route to the specified
time server or similar), ntpclient will hang, and will never attempt another synchronisation.
This can typically occur if the time synchronisation is being done over a wireless link
or a VPN, which may not yet be up when the init script is run.
Because of this issue, I've used a slightly heavy-handed approach, as detailed below.
Modify Init Script
The init script, /etc/init.d/S55ntpclient, is modified to the following:
#!/bin/sh
# kill any existing ntpclient processes
# (they can get stuck if no route to target host)
/usr/bin/killall ntpclient
# do time sync
/usr/sbin/ntpclient -l -h 10.60.74.2 -c 1 -s &
The init script will now terminate any existing instances of ntpclient, and
then attempts to perform a single time synchronisation.
Be sure to change the IP address to a valid time server on your own private network,
or refer to the pool.ntp.org
website for a public NTP server near you.
The init script will be run when the WRT boots, thus attempting an initial time synchronisation.
However, we'll also periodically call the init script, just in case the initial
synchronisation at boot time failed, and to ensure on-going synchronisation.
Periodic Time Synchronisation
Create /etc/crontabs/root with the following contents:
# to timesync every 10 minutes
*/10 * * * * /etc/init.d/S55ntpclient
Restart crond to make this change take effect:
killall crond; /etc/init.d/S60cron
Your WRT's clock should now be synchronised every 10 minutes.
last updated 22 Oct 2013
|