OpenWRT router isn’t shipped with hardware clock like PC does. Therefore, the clock is reset each time the WRT is reboot to a hardcoded time setting.
Applying OpenWRT to some time-critical device would result in unexpected result. A valid clock system need to be implemented. As there are no local source could be used as reference, the only option is using external clock as resource. The cheapest afford will be connecting to a NTP (Network Time Protocol) Server and synchronizing the time with it. The synchronization is done automatically every reboot and the time will be periodically synchronized when OpenWRT is running.
To periodically synchronizing the device, make sure cron is running and able to create a scheduled task to perform the time synchronization.
In this article we will discuss about time synchronization between OpenWRT and NTP Server. Here, I assume we have build and NTP server or at least have a NTP to be used as reference.
Prior to performing time synchronization, make sure the time zone is set correctly on OpenWRT device. Check the value of /etc/TZ to know it.
Since I’m located in Indonesia (West Indonesia Time / WIT), my timezone would be GMT+7. Therefore I will set my Timezone by:
echo GMT-7 > /etc/TZ
Well, the offset in the TZ valu specifies the time value added to local time to get a UTC value. Therefore, if the local timezone is West of the Prime Meridian, the value is positive and it would be negative if it is East. As a result, TZ Must be set to GMT-7 for a timezone that is GMT+7.
To communicate with NTP Server, we need a capable client. The client in this scope is ntpclient which can be installed using opkg.
opkg install ntpclient
In addition, this package may also create an init script, /etc/init.d/S55ntpclient, with following content:
#!/bin/sh /usr/sbin/ntpclient -c 1 -S -h pool.ntp.org &
This will cause ntpclient to attempt a once-off time synchronization to pool.ntp.org at boot time. Additional parameters can also be passed to ntpclient to tell it to periodically perform a time synchronization with a specified host.
Here is the content of /etc/init.d/S55ntpclient I use.
#!/bin/sh # Kill any existing ntpclient process (they can get stuck if no route to target host) /usb/bin/killall ntpclient # do time synchronization /usr/sbin/ntpclient -l -h 10.10.135.1 -c 1 -s &
Here I have a local NTP Server so I use it. Replace the address to ntp address you want.
A fellow user said that when initial time synchronization fails (ie, no route to the specified time server or similar), ntpclient will hang and will never attempt another synchronization. This can typically occur if the time synchronization is being done over a wireless link or a VPN which may not yet be up then the init script is run.
For periodic time synchronization, we will utilize cron.
Create /etc/crontabs/root with the following content:
# timesync every 15 minutes */15 * * * * /etc/init.d/555ntpclient
Restart crond to make change take effect:
killall crond; /etc/init.d/S60cron
At this point, our device should be synchronized for every 15 minutes