Example Code

Installing and Configuring NTP on NI Linux Real-Time Devices

Overview

Network Time Protocol (NTP) is a daemon that can be run on a GNU/Linux device to contact servers running a NTP server for time synchronization and adjustment.  It is a viable option for keeping NI Linux Real-Time devices time-synchronized with a server of your choice for accurate time-stamping of data and other time-based functionality.  This tutorial will equip users with the knowledge to install and configure NTP on a NI Linux Real-Time device.  It will show how to synchronize to www.ntp.org, a popular NTP server in the GNU/Linux community.

Setting up NTP on NI Linux Real-Time (Real-Time Module 13.1 or older)

Configuring Third-Party Installation Sources On NI Linux Real-Time:

  1. Ensure your NI Linux Real-Time device is connected to a network with internet access.
  2. Follow the instructions for accessing the Shell on a NI Linux Real-Time device found here.
  3. Once logged into your NI Linux Real-Time device, you will need to perform the following steps to enable a suitable third-party software installation repository:


Installing NTP on NI Linux Real-Time:

  1. Issue the command: opkg flag ok libc6 to confirm that it is suitable for the libc6 package to be updated on the NI Linux Real-Time system.  This is necessary because NTP requires a newer version to function properly.  Note: This is not supported and may cause other components of the NI Linux Real-Time system to function improperly.
  2. Issue the command: opkg install ntp ntp-tickadj to install NTP on the NI Linux Real-Time system. 
  3. Issue the command: vi /etc/ntp.conf to edit the NTP configuration file with the "vi" text editor.
  4. Modify the settings in the NTP configuration file as needed.  Many guides are available, but a basic ntp.conf file may look similar to the following:
    • # This is the most basic ntp configuration file

      # The driftfile must remain in a place specific to this

      # machine - it records the machine specific clock error

      driftfile /etc/ntp.drift

      # This obtains a random server which will be close

      # (in IP terms) to the machine.  Add other servers

      # as required, or change this.

      server 0.pool.ntp.org

      server 1.pool.ntp.org

      server 2.pool.ntp.org

      server 3.pool.ntp.org

      # Using local hardware clock as fallback

      server 127.127.1.0

      fudge 127.127.1.0 stratum 14

      # Defining a default security setting

      restrict default kod nomodify notrap nopeer noquery

  5. Save and exit the "vi" text editor using the :wq command.
  6. Restart the NI Linux Real-Time system.  NTP should now be running as a background process to synchronize time with your chosen NTP server.

Setting up NTP on NI Linux Real-Time (Real-Time Module 14.0 or newer)

  1. Follow the instructions for accessing the Shell on a NI Linux Real-Time device found here.

Installing NTP on NI Linux Real-Time:

  1. Issue the command: opkg install ntp ntp-tickadj to install NTP on the NI Linux Real-Time system. 
  2. Issue the command: vi /etc/ntp.conf to edit the NTP configuration file with the "vi" text editor.
  3. Modify the settings in the NTP configuration file as needed.  Many guides are available, but a basic ntp.conf file may look similar to the following:
    • # This is the most basic ntp configuration file
      # The driftfile must remain in a place specific to this
      # machine - it records the machine specific clock errordriftfile /etc/ntp.drift# This obtains a random server which will be close
      # (in IP terms) to the machine.  Add other servers
      # as required, or change this.
      server 0.pool.ntp.org
      server 1.pool.ntp.org
      server 2.pool.ntp.org
      server 3.pool.ntp.org

      # Using local hardware clock as fallback
      server 127.127.1.0
      fudge 127.127.1.0 stratum 14
      # Defining a default security setting

      restrict default kod nomodify notrap nopeer noquery
  4. Save and exit the "vi" text editor using the :wq command.
  5. Restart the NI Linux Real-Time system.  NTP should now be running as a background process to synchronize time with your chosen NTP server.

Requirements

Software

NI Real-Time Module 13.0 or newer

NI-RIO 13.0 or newer

NI Measurement and Automation Explorer

Hardware

NI Real-Time target utilizing NI Linux Real-Time OS

| Zach J. | Systems Engineer, HIL and Test Cells | National Instruments |

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
A.Rohde
Active Participant
Active Participant
on

Tip: If you run into the error ntp and ntp-tickadj unknown package / cannot install package on a fresh system for 2014 after running step (1) it might be nessasary to run the command "opkg update"

phell
Member
Member
on

I got the myRIO to sync the time with NTP server at boot.

The following was done using PuTTY to connect to the myRIO.

1) opkg update

2) opkg install ntp ntp-tickadj ntp-utils

3) vi /etc/ntp.conf
driftfile /etc/ntp.drift
server time.nist.gov
restrict default kod nomodify notrap nopeer noquery
tos maxdist 16

4) vi /etc/init.d/callbootntp
#!/bin/bash
/etc/init.d/ntpd stop
until ping -q -c3 8.8.8.8; do
echo "Waiting for network..."
done
ntpd -gq
/etc/init.d/ntpd start

5) chmod +x /etc/init.d/callbootntp
chmod 777 /etc/init.d/callbootntp

6) /usr/sbin/update-rc.d -f callbootntp default 99 0


Best regards

phell

Mads
Active Participant
Active Participant
on

I seem to have ntpd running after following this guide (thanks), but how can I check the status of the time sync?

I have found lots of sites on the topic, but they all refer to ntpq for this, and ntpq does not seem to be installed(?).

Edit - found out from Phells comment here...I had not installed ntp-utils.


metux
Member
Member
on

General notes on clocks and time measurement (yes, especially important for RT):

http://forums.ni.com/t5/NI-Linux-Real-Time-Discussions/Permissions-Setting-date-time-with-system-exe...

Linux Embedded / Kernel Hacker / BSP / Driver development / Systems engineering
Lily1979
Active Participant
Active Participant
on

Hi,

I did the same, but it does't work.

 

Lily

mtitoo
Active Participant
Active Participant
on
mtitoo
Active Participant
Active Participant
on

also if  

6) /usr/sbin/update-rc.d -f callbootntp default 99 0   change it to defaults ('s' at the end)

MPCC
Member
Member
on

I found what appears to be a typo or possibly a difference in targets.

This will open a file in the "vi" text editor.  After opening the file, you will see a line that contains the following text:
# src/gc angstrom-base http://us.feeds.angstrom-distribution.org/feeds/next/ipk/eglibc/armv7a/base/

Modify this line to be the following:

src/gc angstrom-base http://feeds.angstrom-distribution.org/feeds/next/ipk/eglibc/armv7a/base/

src/gc should be src/gz

 

I also didn't have an initial line on my target and I needed to change to core2-64 because I'm using a cRIO with an Intel proc and not an Arm.

The resulting path that worked in my angstrom-base-feed.conf file is this:

src/gz angstrom-base http://feeds.angstrom-distribution.org/feeds/next/ipk/glibc/core2-64/base/
MPCC
Member
Member
on

It also appears that one of the packages is broken. I don't have an init.d/ntp or /ntpd file on my system.

Anyone mess with this recently?

 

MPCC
Member
Member
on

We also found that having NI Sync installed can cause additional problems. In our case, it wasn't configured to any time sync streams but it was causing an unknown offset. I recommend uninstalling NI Sync from the target if you are using NTP.

kosist90
Active Participant
Active Participant
on

Detailed and helpful article, but I'd like to add the following:

  • Link to help document for vi editor is broken (maybe it was pointing to this page - https://www.cs.colostate.edu/helpdocs/vi.html?)
  • It would be nice to add to this description example of real time server, like time.nist.gov - and show, how final configuration file could look like
  • If one can't execute command opkg update and receives an error that "download.ni.com" is bad address - then check DNS server configuration on cRIO, it should not be default; you must to set up it.
  • Check NTP synchronization state you could by executing command ntpq -p.

Thank you very much,

 

Sincerely, kosist90.

Check one more article about malleable VIs and class adaptation!

Drew_G
Member
Member
on

Couple of things:

1. How do I work out what version of Linux Real Time Module I'm using. I'm using a cRIO 9064. According to MAX and the web interface the operating system is: NI Linux Real-Time ARMv7-A 3.2.35-rt52-2.10.0f0?

2. Is there anyway to update the opkg without the internet (my cRIO lives behind our corporate firewall)?

 

kosist90
Active Participant
Active Participant
on

Hello Drew_G,

 

1. Yes, you could see it in NI MAX - or you'd like to check version of Linux core? Then you'll need to connect to cRIO via putty, to use terminal, and try to execute some of the Linux commands, let's say, from here - https://www.cyberciti.biz/faq/command-to-show-linux-version/.


2. I guess you can't update it without the internet. But, you could download packages which you'd like to install (by this link https://forums.ni.com/t5/NI-Linux-Real-Time-Discussions/LabVIEW-2017-RIO-17-0-PSA-Feed-Adjustment-fo... are mentioned some ftp repositaries from it, also you'll see addresses in terminal while try to install packages). After downloading of packages on your PC, just put them on cRIO drive, and install them via terminal commands.


The only issue what could be in this case - some packages require dependencies, so you'll need to download all the dependencies before the installation, and it could be a challenge.

 

Hope, that it would help,

Sincerely, kosist90

jordankuehn
Member
Member
on

A couple notes:

 

I found it necessary to add in "restrict ::1" to the ntp.conf file.

 

Also I disabled NI-Sync Linux OS Time Synchronization in order to leave the driver installed and not fight when other packages install it as a dependency:

 

/usr/lib/x86_64-linux-gnu/ni-timesync/configgen --tsmJsonFile /var/lib/ni-timesync/tsm.json --setTKPluginConfig phc2sysEnable false

 

https://www.ni.com/en-us/support/documentation/supplemental/21/use-ntp-for-timestamping-measurements...

 

Below is a script I put together to do all of this using a package and SystemLink. In the package I manually added the dependencies and SystemLink installed them automatically (I like nano so I included that too). "defaults" is corrected in the script as noted above in the comments too. Just create, save, and add the script (with a .sh exension) to your project, then add it as a custom action in the Advanced section, post-install.

 

jordankuehn_0-1626795249988.png

 

 

#!/bin/bash
rm /etc/ntp.conf

cat <<EOF >/etc/ntp.conf
driftfile /etc/ntp.drift
server time.nist.gov
restrict default kod nomodify notrap nopeer noquery
tos maxdist 16
restrict ::1
EOF

cat <<EOF >/etc/init.d/callbootntp
#!/bin/bash
/etc/init.d/ntpd stop
until ping -q -c3 8.8.8.8; do
echo "Waiting for network..."
done
ntpd -gq
/etc/init.d/ntpd start
EOF

chmod +x /etc/init.d/callbootntp
chmod 777 /etc/init.d/callbootntp

/usr/sbin/update-rc.d -f callbootntp defaults 99 0

/usr/lib/x86_64-linux-gnu/ni-timesync/configgen --tsmJsonFile /var/lib/ni-timesync/tsm.json --setTKPluginConfig phc2sysEnable false

/etc/init.d/callbootntp

 

 

The Colex Group
Lead Software Engineer
Certified LabVIEW Developer
Contributors