{"id":16520,"date":"2017-01-27T10:38:19","date_gmt":"2017-01-27T16:38:19","guid":{"rendered":"http:\/\/www.multitech.net\/developer\/?page_id=16520"},"modified":"2022-08-11T15:41:57","modified_gmt":"2022-08-11T20:41:57","slug":"ntpd","status":"publish","type":"page","link":"https:\/\/www.multitech.net\/developer\/software\/mlinux\/using-mlinux\/ntpd\/","title":{"rendered":"NTPD"},"content":{"rendered":"<p><span style=\"font-size: medium;\"><b>NTPD Overview<\/b><\/span><\/p>\n<p>NTPD is a program that provides time as a service.\u00a0 Time is input to NTPD from a GPS, the local system clock, or an external NTP server. \u00a0NTPD adjusts the system clock and hardware clock to match the external time source. \u00a0The program ranks GPS as the top priority source.<\/p>\n<p>This program can be used in conjunction with the GPS (if this capability is included in your device) to provide accurate times for the device or a network attached to the device via Internet Protocol. If GPS or an external NTP server is not available, NTP continues using the local time. NTP is the network time protocol. Refer to the figure called <strong>NTPD using GPS Time<\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2017\/02\/NPTD-Using-GPS-Time-Figure1.png\" width=\"635\" height=\"357\" \/><\/p>\n<p>GPSD communicates with NTPD via shared memory to provide the GPS time. If the GPS cannot acquire a satellite fix (possibly the system is indoors, or has no antenna)\u00a0 NTPD never starts and the default configuration continues where it uses local time.\u00a0 The NTPD server is delayed to prevent NTP from serving an incorrect time.<\/p>\n<p>If there is a GPS fix, \u00a0NTPD starts and creates a virtual local time service for the <strong>GPS at 127.127.28.0<\/strong> and the <strong>PPS at 127.127.28.2.<\/strong>\u00a0 \u00a0GPSD first sets the system clock to the GPS time and then the NTPD one shot time update (<strong>ntpd -gq<\/strong>) guarantees smooth operation when NTPD first starts as a daemon. Then, NTPD does subsequent and periodic minor adjustments of the local time (both hardware and system clock) to synchronize it with the GPS time. NPTD normally avoids huge corrections or adjustments to the system clock.<\/p>\n<p>By default, the GPS receiver is configured in <strong>\/etc\/ntp.conf<\/strong> with the value of <strong>true<\/strong>.\u00a0 This prevents NTPD from eliminating the GPS from consideration as a <i>falseticker.<\/i><\/p>\n<p>If NTP detects too many anomalies between the GPS and the system clock, the GPS may be ignored by NTP if the \u201ctrue\u201d flag is not set.\u00a0 The <i>falseticker<\/i> flag may be set if the GPS antenna signal strength is poor, or the CPU on the device is overloaded, causing poor interrupt servicing.\u00a0 If the GPS is a falseticker, leap seconds from the GPS will be ignored, and the system clock will not be kept in sync with the GPS.<\/p>\n<p><span style=\"font-size: medium;\"><b>Relationship to GPSD<\/b><\/span><\/p>\n<p>GPSD has code to interpret uBlox binary strings. This is less CPU intensive than using the ASCII NMEA strings.<\/p>\n<p><b>NOTE:<\/b> Do not start NTPD before the GPS has acquired a fix. NTPD immediately uses the GPS if the system time is less than a second from the GPS time. If the GPS time is not close to the system time (a second or more off), NTPD labels the GPS a falseticker and does not use the GPS for setting the system time.<\/p>\n<p>The hardware clock on the device is not of sufficient accuracy to use it alone to set the system time and then expect NTPD to switch to the GPS later. Instead, GPS must acquire a fix, next the system time sets to the GPS, and then NTPD can start.<\/p>\n<p>If the GPS antenna is inadequate or cannot detect a signal and the device is configured to use the GPS for the NTPD timebase, NTPD may never start.<\/p>\n<p><span style=\"font-size: medium;\"><b>NTP configuration file<\/b><\/span><\/p>\n<p>This configuration file <b>\/etc\/ntp.conf\u00a0<\/b>is used to configure ntp and has several functions including it defines clock sources, and it restricts access to the clock from other systems on the network. The file also specifies the minimum and maximum poll times of clock sources.<\/p>\n<p>The uBlox GPS server is at address 127.127.28.0 and the uBlox PPS server is at address 172.127.28.2.<\/p>\n<p>To collect statistics,<\/p>\n<ol>\n<li>Create the directory <b>\/var\/log\/ntpstats<\/b> with owner ntp and group ntp.<\/li>\n<li>Uncomment the statistics, statdir, and filegen lines and restart ntpd to start recording statistics.<\/li>\n<\/ol>\n<p>For more information on ntpd: <a href=\"http:\/\/www.ntp.org\/documentation.html\">NTPD Documentation<\/a><\/p>\n<p><span style=\"font-size: medium;\"><b>Restarting after changing the configuration<\/b><\/span><\/p>\n<p>To restart after changing the configuration file, enter the following command:<\/p>\n<pre class=\"brush:shell\">mtcdt: \/etc\/init.d\/ntpd restart<\/pre>\n<p><span style=\"font-size: medium;\"><b>Verification with NTPQ<\/b><\/span><\/p>\n<p><b>Waiting till GPS fix <\/b><\/p>\n<p>Wait until after the GPS has a 3D fix and the system is configured to set the system clock from the GPS. If you do so, the following results display:<\/p>\n<p><span style=\"font-family: 'andale mono', times;\">\u00a0<\/span><\/p>\n<pre class=\"brush:shell\">mtcdt: ntpq -p<\/pre>\n<pre class=\"brush:shell\">remote\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 refid \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 st t \u00a0  when   poll   reach \u00a0 \u00a0   delay \u00a0 offset \u00a0 \u00a0 jitter\r\n==============================================================================\r\n*SHM(0)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .GPS.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0 \u00a0l \u00a0 \u00a042 \u00a0 \u00a0 64 \u00a0 \u00a0 \u00a01 \u00a0 \u00a0 \u00a0 \u00a0 0.000\u00a0\u00a0\u00a0 3.525 \u00a0 \u00a0 \u00a00.004\r\n SHM(2)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .PPS. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a00 \u00a0l \u00a0 \u00a042 \u00a0 \u00a0 64 \u00a0 \u00a0 \u00a01 \u00a0 \u00a0 \u00a0 \u00a0 0.000\u00a0\u00a0\u00a0 2.821 \u00a0 \u00a0 \u00a00.004\r\n LOCAL(0) \u00a0 \u00a0    LOCL. \u00a0 \u00a0 \u00a0     14 \u00a0l \u00a0 106 \u00a0 \u00a0 64 \u00a0 \u00a0 \u00a02 \u00a0 \u00a0 \u00a0 \u00a0 0.000\u00a0\u00a0\u00a0 0.000 \u00a0 \u00a0 \u00a00.004<\/pre>\n<p>After several minutes NTP should switch to using the PPS:<\/p>\n<p><span style=\"font-family: 'andale mono', times;\">\u00a0<\/span><\/p>\n<pre class=\"brush:shell\">mtcdt: ntpq -p<\/pre>\n<pre class=\"brush:shell\"> \u00a0remote\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 refid \u00a0 \u00a0st \u00a0t when \u00a0     poll  reach\u00a0\u00a0    delay \u00a0 offset \u00a0 \u00a0jitter\r\n==============================================================================\r\n\u00a0 \u00a0+SHM(0)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .GPS. \u00a0 \u00a00 \u00a0l\u00a0\u00a0\u00a0 7 \u00a0 \u00a0 \u00a0 64 \u00a0 \u00a0 \u00a03 \u00a0 \u00a0 \u00a0 \u00a00.000\u00a0\u00a0\u00a0 3.734 \u00a0 \u00a00.209\r\n\u00a0 \u00a0*SHM(2)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .PPS. \u00a0 \u00a00 \u00a0l\u00a0\u00a0\u00a0 7 \u00a0 \u00a0 \u00a0 64 \u00a0 \u00a0 \u00a03 \u00a0 \u00a0 \u00a0 \u00a00.000\u00a0\u00a0\u00a0 2.858 \u00a0 \u00a00.037\r\n  LOCAL(0) \u00a0 \u00a0 \u00a0   .LOCL. \u00a0 14 \u00a0l\u00a0 135 \u00a0 \u00a0   64 \u00a0 \u00a0 \u00a04 \u00a0 \u00a0 \u00a0 \u00a00.000\u00a0\u00a0\u00a0 0.000 \u00a0 \u00a00.004<\/pre>\n<p><span style=\"font-family: 'andale mono', times;\"><b>*<\/b> = this source is being used for the clock.<\/span><br \/>\n<span style=\"font-family: 'andale mono', times;\"><b>+<\/b> = this source is is an alternative clock.<\/span><br \/>\n<span style=\"font-family: 'andale mono', times;\"><b>x <\/b>= this source is not used for clock (i.e. the system has a problem with it).<\/span><\/p>\n<p><b>SHM<\/b> indicates that shared memory is being used to synchronize the clock. <b>refid<\/b> shows the type of clock source either <b>GPS, PPS<\/b> or <b>local<\/b>.<\/p>\n<p><b>Jitter<\/b> is the measured variation in the clock relative to the system clock. The <b>offset<\/b> is the offset from the system clock. If you do not see this after ten minutes or so, check your antenna connection. You can view the reception of each satellite with <b>gpsmon<\/b>.<\/p>\n<p><b>t<\/b>\u00a0is the type of source like local, unicast, multicast and broadcast. <strong>l<\/strong> is local. Delay,<b> offset<\/b>, and <b>jitter<\/b> are expressed in milliseconds. <b>Reach<\/b> is in bits. <b>When<\/b> and <b>poll<\/b> are in seconds. <b>St <\/b>is strata which evaluates the accuracy of the time source on a scale of<b> 0-16 <\/b>where<b> 0 <\/b>is the best (such as an atomic clock)<b> <\/b>and<b> 16 <\/b>is the worst (such as a local hardware clock).<\/p>\n<p><b><br \/>\n<\/b><span style=\"font-size: medium;\"><b>NTP started before GPS fix<\/b><\/span><\/p>\n<p>Do not start NTP before the GPS has a fix. In this case, the system clock is set by the hardware clock. If you do so, the following results display:<\/p>\n<p><span style=\"font-family: 'andale mono', times;\">\u00a0<\/span><\/p>\n<pre class=\"brush:shell\">mtcdt: ntpq -p<\/pre>\n<pre class=\"brush:shell\">remote\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 refid\u00a0\u00a0\u00a0\u00a0\u00a0 st \u00a0 t \u00a0 \u00a0 when \u00a0    poll \u00a0  reach\u00a0\u00a0 delay \u00a0 \u00a0 offset \u00a0jitter\r\n=========================================================================================\r\nxSHM(0)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .GPS. \u00a0 \u00a0 \u00a0 0 \u00a0 l \u00a0 \u00a0 \u00a040 \u00a0 \u00a0 \u00a0 \u00a064 \u00a0 \u00a0 \u00a0377\u00a0\u00a0\u00a0 0.000 \u00a0 \u00a01495.346\u00a0\u00a02.620\r\nxSHM(2)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .PPS. \u00a0 \u00a0 \u00a0 0 \u00a0 l \u00a0 \u00a0 \u00a038 \u00a0 \u00a0 \u00a0 \u00a064 \u00a0 \u00a0 \u00a0377\u00a0\u00a0\u00a0 0.000 \u00a0 \u00a01498.024 \u00a00.005\r\n*LOCAL(0) \u00a0 \u00a0   .LOCL. \u00a0    14 \u00a0 l \u00a0 \u00a0 \u00a039 \u00a0 \u00a0 \u00a0 \u00a064 \u00a0 \u00a0\u00a0 \u00a0 1 \u00a0 \u00a00.000 \u00a0 \u00a0 \u00a0 0.000 \u00a00.004<\/pre>\n<p>Because the GPS time is 1.5 seconds from the system clock, the system marks the GPS as a <b>falseticker <\/b>and NTP prefers the local clock. The asterisk next to <b>LOCAL <\/b>indicates that NTPD is using the local clock, which is not optimal.<\/p>\n<p><b>\/etc\/default\/ntpd<\/b><\/p>\n<p>This file enables ntpd at boot time and controls the relationship between ntpd and gpsd.<span style=\"font-family: 'andale mono', times;\">\u00a0<\/span><\/p>\n<pre class=\"brush:shell\">NTPD_ENABLED=1\r\n# The GPSD_* parameters in this file are ignored\r\n# if the uBlox GPS is not present.\r\n\r\n# Require a GPS lock\/fix before starting NTP\r\n# This is needed if we are not using NTP servers.\r\n# NTP will not work with the GPS if\u00a0 the GPS is not\r\n# locked before starting.\r\n# See \/etc\/default\/gpsd for the states required.\r\nGPSD_REQUIRED=1\r\n\r\n# Number of seconds between testing for a GPS\r\n# lock prior to calling ntpd.\r\nGPSD_WAIT_TIME=120\r\n\r\n# Since the HW Clock could be off by a second or\r\n# so, our GPS might get marked as a false ticker\r\n# if we do not set the system clock to the GPS\r\n# first.\u00a0 The current correct way to do this\r\n# according to the ntp doc is ntpd -gq -c conffile\r\n# which must be done before ntpd is started.\r\n# conffile should exclude the local clock, so it is\r\n# ignored while doing the initial sync.\r\n# ntpd -gq apparently does not work with the GPS\r\n# when tested with the clock more than one day off\r\n# and no ntpd.\u00a0 The gps shared memory is never polled.\r\n\r\n#\r\n# If there is a uBlox GPS present, the time is\r\n# read from the GPS to initialize the system time\r\n# before NTP is started.\r\nSET_SYSTEM_CLOCK=1<\/pre>\n<p>&nbsp;<\/p>\n<p>If NTP time synchronization is based on a GPS, <strong>GPSD_REQUIRED<\/strong> causes NTPD to wait until GPSD determines that the GPS has a lock. If there is no antenna, NTPD may never start when this flag is set.<\/p>\n<p><strong>GPSD_WAIT_TIME<\/strong> specifies a delay between testing to see if the GPS is ready.<\/p>\n<p>NTPD should not start before the system clock is close to <strong>UTC. SET_SYSTEM_CLOCK<\/strong> ensures that the system clock is correct before starting NTP. NTP will not adjust the system clock if it is too far from UTC time.<\/p>\n<h2>Turn off GPSD and Use NTP with LoRa 2.1 (MTDCTIP-PHP)<\/h2>\n<p>GPSD does not work with LoRa 2.1, which has GPS built into the LoRa cards.\u00a0Use NTP instead.<\/p>\n<p>1. To do this, edit the\u00a0file <strong>\/etc\/ntp.conf\u00a0<\/strong> by\u00a0adding the <strong>#<\/strong> symbol to comment out lines involving the GPSD server IP address 127.127.28.*<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\"># GPS Serial data reference (NTP0)\r\n# This sets the GPS 50 milliseconds slower than the PPS.\r\n#server 127.127.28.0 true\r\n#fudge 127.127.28.0 time1 0.050 refid GPS\r\n# GPS PPS reference (NTP2)\r\n#server 127.127.28.2 prefer true\r\n#fudge 127.127.28.2 time1 0.000 refid PPS<\/pre>\n<p>2. Configure NTP to use a network server or pool of ntp servers.\u00a0 In this example, we set a single server IP address.\u00a0Add the following new line to the file above the commented section and save:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">server 192.168.2.249 iburst<\/pre>\n<p>3.\u00a0 After these changes are made, restart the ntp server using this command:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">$ sudo \/etc\/init.d\/ntpd restart<\/pre>\n<p>4.\u00a0Verify the ntp server is connected. Enter\u00a0 the command:<\/p>\n<pre class=\"brush:shell\" style=\"padding-left: 30px;\">$ ntpq -p\r\nremote\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 refid\u00a0\u00a0\u00a0\u00a0\u00a0 st t when poll reach\u00a0\u00a0 delay\u00a0\u00a0 offset\u00a0 jitter\r\n==============================================================================\r\n*192.168.2.249\u00a0\u00a0 204.9.54.119\u00a0\u00a0\u00a0\u00a0 2 u\u00a0\u00a0 23\u00a0\u00a0 64\u00a0 377\u00a0\u00a0\u00a0 0.363\u00a0\u00a0\u00a0 3.500\u00a0\u00a0 2.584\r\nLOCAL(0)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 .LOCL.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 14 l\u00a0 494\u00a0\u00a0 32\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0 0.000\u00a0\u00a0\u00a0 0.000\u00a0\u00a0 0.000<\/pre>\n<p style=\"padding-left: 30px;\">The times in the last three columns are in milliseconds.\u00a0 The * indicates the preferred server.<\/p>\n<p>For more information see:<\/p>\n<p><a href=\"http:\/\/doc.ntp.org\/4.2.8p9\/confopt.html\">http:\/\/doc.ntp.org\/4.2.8p9\/confopt.html<\/a><\/p>\n<p>and for systems with Internet access:<\/p>\n<p><a href=\"http:\/\/www.pool.ntp.org\/en\/use.html\">http:\/\/www.pool.ntp.org\/en\/use.html<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NTPD Overview NTPD is a program that provides time as a service.\u00a0 Time is input to NTPD from a GPS, the local system clock, or an external NTP server. \u00a0NTPD adjusts the system clock and hardware clock to match the external time source. \u00a0The program ranks GPS as the top priority source. This program can [&hellip;]<\/p>\n","protected":false},"author":1478,"featured_media":0,"parent":9184,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"class_list":["post-16520","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/16520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/users\/1478"}],"replies":[{"embeddable":true,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/comments?post=16520"}],"version-history":[{"count":49,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/16520\/revisions"}],"predecessor-version":[{"id":32971,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/16520\/revisions\/32971"}],"up":[{"embeddable":true,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/9184"}],"wp:attachment":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/media?parent=16520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}