What's new

NTP Daemon for ASUSWRT/Merlin

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

MOVED: https://www.snbforums.com/threads/usb-gps-instead-of-ntp.39806/



Here's a sample script to synchronize the router clock with a GPS receiver. I verified this works on my RT-N16 router w/stock firmware. So it likely works for all Asuswrt-Merlin routers.

On my u-blox GPS, I suppress all NEMA sentences except $GPZDA, so it outputs only the current date/time and hides my geographic location.

The best news is that NTP is not needed to set the router's clock. It's script only. He works with @kvic.

/jffs/scripts/clock-sync-gps.sh
Code:
#!/bin/sh
GPS_DEVICE="/dev/ttyACM0"
NEMA_TYPE="GPZDA"

# search for NEMA sentence of type $GPZDA
READ_COUNT=50
read NEMA_DATA < $GPS_DEVICE
READ_ERROR=$?
while [ $READ_COUNT -ge 0 ] && [ $READ_ERROR -eq 0 ] && [ "${NEMA_DATA:0:6}" != "\$${NEMA_TYPE}" ]; do
  let READ_COUNT--
  read NEMA_DATA < $GPS_DEVICE
  READ_ERROR=$?
done

# found a NEMA sentence of type $GPZDA
if [ "${NEMA_DATA:0:6}" == "\$${NEMA_TYPE}" ]; then

  # compute the NEMA checksum
  L1=${#NEMA_DATA}
  let L2=$L1-3
  CHECKSUMHEX=${NEMA_DATA##*\*}
  CHECKSUM=$(/usr/bin/printf "%d" 0x${CHECKSUMHEX})
  P=1; CHECKSUM_CALC=0
  while [ $P -lt $L2 ]; do
    let CHECKSUM_CALC^=$(/usr/bin/printf '%d' "'${NEMA_DATA:$P:1}")
    let P++
  done

  # validate the NEMA checksum
  if [ $CHECKSUM_CALC -eq $CHECKSUM ]; then

    # parse current date/time from NEMA sentence of type $GPZDA
    if [ "$NEMA_TYPE" == "GPZDA" ]; then
      #           11111111112222222222333333
      # 012345678901234567890123456789012345
      # $GPZDA,132727.00,07,06,2017,00,00*61
      UTC="${NEMA_DATA:23:4}${NEMA_DATA:20:2}${NEMA_DATA:17:2}${NEMA_DATA:7:2}${NEMA_DATA:9:2}.${NEMA_DATA:11:2}"
    else
      UTC=""
    fi

    # set the system clock
    if [ -n "$UTC" ]; then
#      /bin/echo $UTC
      /bin/date -u -s $UTC
      if [ $? -eq 0 ]; then
        nvram set ntp_ready=1
      fi
    fi

  fi
fi


If you have a u-blox 7 chip in your GPS, then here's how to enable reporting of the current date/time only. Prevents your geographic location from being reported to the router. If you download and install the u-center evaluation kit for your PC, it can generates these codes for you. If you un-comment the last line it will save these settings to flash memory, so you only need to run the script once.

/jffs/scripts/ublox7-gpzda.sh
Code:
#!/bin/sh

DEV_GPS=/dev/ttyACM0

# HOWTO: generate these UBX codes yourself
# download and install the u-center evaluation kit from u-blox website
# plug in the GPS to your computer and run the u-center software
# select menu: View -> Messages
# select tree: UBX -> CFG (Config) -> MSG (Messages)

# Disable: NMEA GxGGA
printf "\xB5\x62\x06\x01\x08\x00\xF0\x00\x00\x00\x00\x00\x00\x00\xFF\x23" >> $DEV_GPS
# Disable: NMEA GxGLL
printf "\xB5\x62\x06\x01\x08\x00\xF0\x01\x00\x00\x00\x00\x00\x00\x00\x2A" >> $DEV_GPS
# Disable: NMEA GxGSA
printf "\xB5\x62\x06\x01\x08\x00\xF0\x02\x00\x00\x00\x00\x00\x00\x01\x31" >> $DEV_GPS
# Disable: NMEA GxGSV
printf "\xB5\x62\x06\x01\x08\x00\xF0\x03\x00\x00\x00\x00\x00\x00\x02\x38" >> $DEV_GPS
# Disable: NMEA GxRMC
printf "\xB5\x62\x06\x01\x08\x00\xF0\x04\x00\x00\x00\x00\x00\x00\x03\x3F" >> $DEV_GPS
# Disable: NMEA GxVTG
printf "\xB5\x62\x06\x01\x08\x00\xF0\x05\x00\x00\x00\x00\x00\x00\x04\x46" >> $DEV_GPS
# Disable: NMEA GxGRS
printf "\xB5\x62\x06\x01\x08\x00\xF0\x06\x00\x00\x00\x00\x00\x00\x05\x4D" >> $DEV_GPS
# Disable: NMEA GxGST
printf "\xB5\x62\x06\x01\x08\x00\xF0\x07\x00\x00\x00\x00\x00\x00\x06\x54" >> $DEV_GPS
# Enable: NMEA GxZDA
printf "\xB5\x62\x06\x01\x08\x00\xF0\x08\x01\x01\x01\x01\x01\x00\x0C\x6F" >> $DEV_GPS
# Disable: NMEA GxGBS
printf "\xB5\x62\x06\x01\x08\x00\xF0\x09\x00\x00\x00\x00\x00\x00\x08\x62" >> $DEV_GPS
# Disable: NMEA GxDTM
printf "\xB5\x62\x06\x01\x08\x00\xF0\x0A\x00\x00\x00\x00\x00\x00\x09\x69" >> $DEV_GPS
# Disable: NMEA GxGNS
printf "\xB5\x62\x06\x01\x08\x00\xF0\x0D\x00\x00\x00\x00\x00\x00\x0C\x7E" >> $DEV_GPS
# Disable: NMEA GxTHS
printf "\xB5\x62\x06\x01\x08\x00\xF0\x0E\x00\x00\x00\x00\x00\x00\x0D\x85" >> $DEV_GPS
# Disable: NMEA GxVLW
printf "\xB5\x62\x06\x01\x08\x00\xF0\x0F\x00\x00\x00\x00\x00\x00\x0E\x8C" >> $DEV_GPS

# Save MSG (Messages) to flash memory
printf "\xB5\x62\x06\x09\x0D\x00\x00\x00\x00\x00\xFF\xFF\x00\x00\x00\x00\x00\x00\x03\x1D\xAB" >> $DEV_GPS


If it don't work, you may need to disable the Asus USB modem drivers and programs.

/jffs/scripts/init-start
Code:
#!/bin/sh
deny_access() {
  local FILEPATH="$1"
  local FILENAME="$(/usr/bin/basename $FILEPATH)"
  local FILEEXT="${FILENAME##*.}"
  if [ "$FILEEXT" == "ko" ]; then
    local MODULENAME="${FILENAME%.*}"
    local FILEPATH="/lib/modules/$(/bin/uname -r)/$(/sbin/modprobe -l $MODULENAME)"
    if [ -f "$FILEPATH" ] && [ ! -h "$FILEPATH" ]; then
      /sbin/lsmod | /bin/grep -qF $MODULENAME && /sbin/modprobe -r $MODULENAME && /bin/sleep 1
      /bin/mount -o bind /dev/null "$FILEPATH"
    fi
  else
    if [ -f "$FILEPATH" ] && [ ! -h "$FILEPATH" ]; then
      [ -n "$(/bin/pidof $FILENAME)" ] && /usr/bin/killall $FILENAME && /bin/sleep 1
      /bin/mount -o bind /dev/null "$FILEPATH"
    fi
  fi
}

# disable automatic loading of USB modem drivers and programs
deny_access option.ko
deny_access usb_wwan.ko
deny_access drxvi314.ko
deny_access /usr/sbin/find_modem_node.sh
deny_access /usr/sbin/find_modem_type.sh
deny_access /usr/sbin/getrealip.sh
deny_access /usr/sbin/gobi_update.sh
deny_access /usr/sbin/modem_at.sh
deny_access /usr/sbin/modem_autoapn.sh
deny_access /usr/sbin/modem_enable.sh
deny_access /usr/sbin/modem_status.sh
deny_access /usr/sbin/modem_stop.sh
deny_access /usr/sbin/chat

# disable the Asus NTP CLIENT
[ -n "$(/bin/pidof ntp)" ] && /usr/bin/killall ntp && /bin/sleep 1
/bin/rm -f /var/run/ntp.pid
/bin/mkdir -p /var/run/ntp.pid
deny_access /usr/sbin/ntpclient
 
Last edited:
Mine stopped updating last night at 8pm, the NTP section in tools shows everything flatline?
Any idea how to troubleshoot as a reboot of the router has got it going again for now.

Also is it IP only or can I add a hostname instead?
 
Looks like the latest entware-ng update has broken the rrdtool by updating the the libpng from version 12 to version 16 - but not recompiling rrdtool to use the new version (re-install of rrdtool does not help).

chief@RT-AC87U:/tmp# /jffs/bin/ntpstats.sh
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory


I had a backup of libpng12 and restored it into the same lib path as the new libpng16 and then it works again.

chief@RT-AC87U:/tmp/mnt/ac87u# locate libpng
/tmp/mnt/ac87u/entware-ng.arm/lib/libpng16.so
/tmp/mnt/ac87u/entware-ng.arm/lib/libpng16.so.16
/tmp/mnt/ac87u/entware-ng.arm/lib/libpng16.so.16.28.0
/tmp/mnt/Data/ac87u.bac/ac87u/entware-ng.arm/lib/libpng12.so.0
/tmp/mnt/Data/ac87u.bac/ac87u/entware-ng.arm/lib/libpng12.so.0.57.0
 
Looks like the latest entware-ng update has broken the rrdtool by updating the the libpng from version 12 to version 16 - but not recompiling rrdtool to use the new version (re-install of rrdtool does not help).

You should ping @zyxmon if you believe it's a broken package.
 
If I'm not mistaken

Code:
$ opkg update

does nothing else then getting the latest list of available packages, right? So, opkg update shouldn't break anything if I'm correct.

I just ran opkg update followed by

Code:
$ opkg list-upgradable

and it shows no upgradable packages available. Now, I'm on a RT-AC68U (ARMv7) so that just might be architecture related, but everything is running smoothly here and I have no issues with plotting the graphs?
 
Looks like the latest entware-ng update has broken the rrdtool by updating the the libpng from version 12 to version 16 - but not recompiling rrdtool to use the new version (re-install of rrdtool does not help).

chief@RT-AC87U:/tmp# /jffs/bin/ntpstats.sh
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory
/opt/bin/rrdtool: error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory


I had a backup of libpng12 and restored it into the same lib path as the new libpng16 and then it works again.

chief@RT-AC87U:/tmp/mnt/ac87u# locate libpng
/tmp/mnt/ac87u/entware-ng.arm/lib/libpng16.so
/tmp/mnt/ac87u/entware-ng.arm/lib/libpng16.so.16
/tmp/mnt/ac87u/entware-ng.arm/lib/libpng16.so.16.28.0
/tmp/mnt/Data/ac87u.bac/ac87u/entware-ng.arm/lib/libpng12.so.0
/tmp/mnt/Data/ac87u.bac/ac87u/entware-ng.arm/lib/libpng12.so.0.57.0

Did you uninstall rrdtool and librrd ?....then reinstall rrdtool..
 
Did you uninstall rrdtool and librrd ?....then reinstall rrdtool..
Just reinstall the package in such cases - `opkg install --force-reinstall <pkg_name>`
Nope - does not fix the problem - it pulls only libpng16 (latest?) but depends on libpng12! :(

Lucky me: I made a backup of my Entware-NG installation before this "update" happened. :rolleyes:
 
Mine stopped updating last night at 8pm, the NTP section in tools shows everything flatline?
Any idea how to troubleshoot as a reboot of the router has got it going again for now.

Also is it IP only or can I add a hostname instead?
bump, wondering more about using hostnames side of things?
 
bump, wondering more about using hostnames side of things?

After some Googling, I found the following in the official documentation at http://doc.ntp.org/4.1.0/confopt.htm:

Configuration Support
Following is a description of the configuration commands in NTPv4. These commands have the same basic functions as in NTPv3 and in some cases new functions and new arguments. There are two classes of commands, configuration commands that configure a persistent association with a remote server or peer or reference clock, and auxilliary commands that specify environmental variables that control various related operations.

Configuration Commands
The various modes are determined by the command keyword and the type of the required IP address. Addresses are classed by type as (s) a remote server or peer (IP class A, B and C), (b) the broadcast address of a local interface, (m) a multicast address (IP class D), or (r) a reference clock address (127.127.x.x). Note that only those options applicable to each command are listed below. Use of options not listed may not be caught as an error, but may result in some weird and even destructive behavior.

server address [key key | autokey] [burst] [iburst] [version version] [prefer] [minpoll minpoll] [maxpoll maxpoll]
peer address [key key | autokey] [version version] [prefer] [minpoll minpoll] [maxpoll maxpoll]
broadcast address [key key | autokey] [version version] [minpoll minpoll] [ttl ttl]
manycastclient address [key key | autokey] [version version] [minpoll minpoll [maxpoll maxpoll] [ttl ttl]
These four commands specify the time server name or address to be used and the mode in which to operate. The address can be either a DNS name or a IP address in dotted-quad notation. Additional information on association behavior can be found in the Association Management page.

... I assume you can also use servernames instead of IP-addresses. I there's one possible downside to it (pinging @kvic to either confirm or correct me, if I'm wrong) and that's when you specify a global, continental or national pool-address, the request gets redirected to a random NTP-server in the pool you specified with the chosen server, which may lead to fluctuations in your results, most likely at the cost of accuracy. You can read more about that here: http://www.pool.ntp.org/en/use.html

I have specified 7 IP-adresses of Stratum One servers in /jffs/etc/ntp.conf, 5 in my own country and two in the countries to the east and the south of where I live. The results are incredibly accurate, I have nearly no jitter at all. That's something you can't achieve (IMHO) with random appointed servers. See if you can find Stratum One servers close to you (you can look them up here: http://support.ntp.org/bin/view/Servers/StratumOneTimeServers) If you click on them, you'll find some more details, such as the IP-address. If you can't find any servers close to you, search for Stratum Two servers (here: http://support.ntp.org/bin/view/Servers/StratumTwoTimeServers) of which there are many. If you see (in syslog) that one of them is serving incorrect info (wrong timestamps etc) or KOD-packages (Kiss Of Death), you're using a server you're not supposed to use. Remove it from your config file, as it won't provide you the requested data and save the file. Run

Code:
/opt/etc/init.d/S77ntpd-custom restart

to restart ntpd (and re-read the edited ntp.conf or just reboot your router.
 
Looks like an old list, given the date in the url. Best to use the official lists, which I linked above. Stratum One is great, but rare because they're officially not open to the public, unless you run a Stratum Two server or for other specific purposes (I'm lucky we have several still freely accessible in NL), Stratum Two will do just fine for the non-ocd people amongst us, I assume :rolleyes: No matter how you look at it (unless you're living next to a company at 1 Microsoft Way, Redmond, WA 98052, USA, it's way more accurate than time.windows.com for instance ;).
 
I never paid attention to the URL having the date, nice spot!
I will remove the entries from that list.

Thanks again, appreciate the help.
 
rebooted the router this morning and for some reason NTPD didn't start, when I ran
/opt/etc/init.d/S77ntpd-custom restart is confirmed this as it didn't shut it down 1st.


And it is borked still, even after restarting it is doing nothing, total flatline on the stats :(
 
Last edited:
Nope - does not fix the problem - it pulls only libpng16 (latest?) but depends on libpng12! :(

Lucky me: I made a backup of my Entware-NG installation before this "update" happened. :rolleyes:
Is this why it has stopped working for me, I have not updated anything but it has basically just stopped doing anything.
 
rebooted the router this morning and for some reason NTPD didn't start, when I ran
/opt/etc/init.d/S77ntpd-custom restart is confirmed this as it didn't shut it down 1st.

And it is borked still, even after restarting it is doing nothing, total flatline on the stats

Hmm... strange. It's working like a charm here on the same router as you have. Try restarting ntpd again, it should confirm it stopped and restarted ntpd. Just to make sure: when you edited ntp.conf with the new servers, did you remove the hashtags on the lines with the servers you entered? Otherwise there are no ntp-servers your router can actually use to update its time which would explain the lack of data. Otherwise, take a look at the syslog, you should see cron call the script to update the stats every 5 minutes:

Code:
Jun 16 12:40:00 dMP17 crond: USER marco pid 9307 cmd /jffs/bin/ntpstats.sh

Also, check your syslog for any clues as to why it's not starting and isn't updating/refreshing stats.
 
Yeah bit odd that, it did respond after about 20 minutes on the stats page. Can you point me to the startup script so I can check it is still there with it failing to start it after the reboot?

And I did it without hashtags :)
Code:
# replace the following three time servers to
# the ones close to you
server 92.27.75.51 iburst
server ntp-galway.hea.net iburst
server 94.247.49.238 iburst
server 89.101.218.6 iburst
server chronos.cru.fr iburst
server 79.143.250.152 iburst[/code

Thanks.
 
Well that lasted under an hour and now flatlined again :(
I ran
/opt/etc/init.d/S77ntpd-custom restart
and it just started it so it was down again.

From the logs

Jun 16 10:56:42 GoNz0: Started ntpd from .
Jun 16 10:56:42 ntpd[1248]: ntpd 4.2.8p9-win@1.3728 Sat Mar 18 09:20:25 UTC 2017 (2): Starting
Jun 16 10:56:42 ntpd[1248]: Command line: ntpd -c /jffs/etc/ntp.conf
Jun 16 10:56:42 ntpd[1257]: proto: precision = 1.608 usec (-19)
Jun 16 10:56:42 ntpd[1257]: Listen normally on 0 lo 127.0.0.1:123
Jun 16 10:56:42 ntpd[1257]: Listen normally on 1 br0 192.168.1.1:123
Jun 16 10:56:42 ntpd[1257]: Listening on routing socket on fd #18 for interface updates
Jun 16 11:53:40 ntpd[1257]: 140.203.204.77 local addr 192.168.1.1 -> <null>
Jun 16 11:53:40 ntpd[1257]: 94.247.49.238 local addr 192.168.1.1 -> <null>
Jun 16 11:53:40 ntpd[1257]: 92.27.75.51 local addr 192.168.1.1 -> <null>
Jun 16 11:53:44 ntpd[1257]: Deleting interface #0 lo, 127.0.0.1#123, interface stats: received=11, sent=11, dropped=0, active_time=3322 secs

after another restart when it went down

Jun 11 20:24:18 ntpd[1241]: Deleting interface #1 br0, 192.168.1.1#123, interface stats: received=1517, sent=2642, dropped=0, active_time=440352 secs
Jun 11 20:24:18 ntpd[1241]: 194.164.127.4 local addr 192.168.1.1 -> <null>
Jun 11 20:24:18 ntpd[1241]: 143.210.16.201 local addr 192.168.1.1 -> <null>
Jun 11 20:24:18 ntpd[1241]: 81.168.77.149 local addr 192.168.1.1 -> <null>
Jun 11 20:24:18 ntpd[1241]: 194.35.252.7 local addr 192.168.1.1 -> <null>

That is it until I restart it again.
 
Last edited:
If it starts showing stats after 20 minutes, your router probably didn't getting any (usable) data at the first three tries... Sounds to me like it was successful for the first time on the fourth attempt to sync. Can't say why, you really need to dive into syslog to find the cause.

As for the two FQDN in your config, in case I misinformed you a few posts back, you can easily replace them with their IP's:

nslookup ntp-galway.hea.net
Server: 127.0.0.1
Address 1: 127.0.0.1 localhost.localdomain

Name: ntp-galway.hea.net
Address 1: 140.203.204.77

nslookup chronos.cru.fr
Server: 127.0.0.1
Address 1: 127.0.0.1 localhost.localdomain

Name: chronos.cru.fr
Address 1: 195.220.94.163 chronos.cru.fr

The fact that nslookup returns a single IP shows that they're running on a single server, so no need to use their server names instead of their IP's (in bold above). In case of DNS-problems, the servers are still reachable, as their addresses don't need to be resolved.

As for the scripts: the ntpd is started as a service from /tmp/mnt/<your USB device here>/entware/etc/init.d/S77ntpd-custom (/opt as mentioned in one of my previous posts is a symlink to where entware-ng is installed). If you open it, it should look like:

Code:
#!/bin/sh

ENABLED=yes
PROCS=ntpd
ARGS="-c /jffs/etc/ntp.conf"
PREARGS=""
PRECMD="killall ntp"
DESC=$PROCS
PATH=/jffs/bin:/opt/sbin:/opt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

. /opt/etc/init.d/rc.func

The script to gather data and generate graphs is /jffs/bin/ntpstats.sh
It's called by crond, you can list all cron tasks by entering

Code:
crontab -l

which should contain a line

Code:
*/5 * * * * /jffs/bin/ntpstats.sh #NtpdStats#

If you can't figure it out, just make a backup of /jffs/etc/ntp.conf and follow the excellent instructions by @kvic at https://github.com/kvic-z/goodies-asuswrt/wiki/Install-NTP-Daemon-for-Asuswrt-Merlin (exactly) to reinstall and replace the NTP-servers you prefer to use afterwards by copying back the previously made backup of ntp.conf. Or just replace the IP-addresses in the fresh ntp.conf.
 
Cheers, I had thought of reinstalling and using the conf file again I will certaily change hostnames for IP's, I had hashed them out for now to see what it did.

Thanks again, I would be clueless without help :)
 

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top