Quick start NTP on the Raspberry Pi
A guide based on an application note from Uputronics (formerly: HAB Supplies. Much more detailed background notes. Some notes from Rich Schmidt using alternative hardware. The updated Uputronics article is here.
Thomas Kocourek, N4FWD, was kind enough to supply a PDF guide about stand-alone operation of the Raspberry Pi as a time server with no Internet connection. He includes some considerations about configuring NTP for this use. This is for the Broadband Ham Net community.
Utilising the Raspberry Pi+ GPS Expansion Board for a PPS Disciplined NTP Server On the Raspberry Pi
2015-Jan-27 Update for kernel 3.18.3+ #742 and later, thanks to Chris
Stenton for finding the fix.
Note that the Adafruit GPS Hat uses GPIO 4, physical pin 7, so you would need to change the commands given in this document. To check that the module is loaded, you can use the lsmod command, for example:
Output should be similar to:
pps_gpio 2529 1
Verifying PPS is working
Ensure the GPS has a lock and the Green PPS LED on the HAB Supplies Raspberry Pi+ GPS Expansion Board is blinking once a second.
Output should be similar to:
[ 0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2708.boardrev=0x10 bcm2708.serial=0x1a25ea38 smsc95xx.macaddr=B8:27:EB:25:EA:38 bcm2708_fb.fbswap=1 bcm2708.disk_led_gpio=47 bcm2708.disk_led_active_low=0 sdhci-bcm2708.emmc_clock_freq=250000000 vc_mem.mem_base=0x1ec00000 vc_mem.mem_size=0x20000000 dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
Note that with the Raspberry Pi 2, or with more recent versions of Raspbian, you may see:
[ 10.172015] pps pps0: new PPS source pps-gpio.-1
Output should be similar to:
trying PPS source "/dev/pps0"
(Press CTRL+C to quit).
This indicates the PPS Module is loaded (dmesg) and is working (ppstest).
Note that there is no value given for the "clear" time. Gary
E Miller reports that the pps-gpio driver only looks for one edge, the positive
going edge. If you are using a different GPS device from those mentioned
here you may need to a 3.3 volt output inverter in the PPS line from the GPS.
Enabling PPS/ATOM support in NTPD
The supplied version of NTPD on the Raspberry Pi may not support PPS so we need to recompile it (Please note that the configure and compile steps may take up to 30 minutes on older Raspberry Pi models). The latest version is 4.2.8p6, by the way.
Note that for the Raspberry Pi 2 and later, use:
as it's much quicker than just:
Note that for Raspbian Jessie you should add "--enable-linuxcaps" after the "./configure"
Thanks for that discovery to Richard Cartwright. Seems to vary whether you need this or not.
In editing /etc/ntp.conf, add these lines:
Amend this line to add a trailing "prefer":
Note: you must add a preferred server or PPS doesn’t work.
After a few minutes run:
if you get oPPS(0) this indicates source selected, Pulse Per Second (PPS) used and everything is working. To see the version as well, enter:
To prevent NTP being subsequently downgraded when you next update the operating system, you can try:
For more information, please see here.
Thanks to Nick Sayer for that suggestion.
Security for external use
[DJT notes] If your Raspberry Pi is exposed to the wider Intenet, and yet you still want access from the LAN for ntpq commands etc., you may want to adjust the restrict statements in the ntp.conf file to something like the following:
These commands are documented
[DJT notes] If you are operating away from your LAN or the Internet, in addition to the precise edge of the second provided by PPS you will also need to get the nearest second. You can get the time from the serial component of the GPS feed. My own preference is to install gpsd which allows you to see that the GPS is working as well - location, number of satellites etc. There are some notes here but for a different device.
The next steps are to install the gpsd software, and start the gpsd service pointing to the device ttyAMA0:
To allow gpsd to access the serial port, you need to make the following changes. You can then check that you are seeing the GPS device, and that it is functioning:
Raspberry Pi 3 UART & Bluetooth complications
It seems that changes made for the Raspberry Pi 3 currently prevent serial access over pins GPIO14 and GPIO15, which have worked on all other RPi cards. Pain! You find that minicom doesn't show any output, even with the GPS connected. Fortunately, someone has written a DT overlay which resolves the issue, and is now part of the OS distribution. You can read about this here and here. Use these steps:
Then sudo reboot your Raspberry Pi and test with minicom
again as described above. I found (2) worked on 2016-Mar-20, when the OS
version I ended up with (uname -a)
was: Linux 4.1.20-v7+ #862 SMP Sat Mar 19. However, I needed the overlay
in (3) above for Linux 4.4.6-v7+ #874 SMP.
Now install the GPSD software:
From one report I had received, if you get errors with the above step you may need to run an update to apt-get:
Now try and start the gpsd service temporarily.
At this point, you should be able to see a text-mode output from your GPS receiver by running the command "cgps -s", something like the following.
Type "q" to exit the cgps program. Note that you can't use two programs to access the serial stream from the GPS at the same time, so minicom will not longer show the GPS output.
Configuring gpsd to auto-start
Likely with a fresh install of Raspbian Jessie instead of the GPS output showing you may get a "GPS timeout" message. Try editing the startup file and linkage for GPSD as follows. Thanks to A Carver: The gpsd needs to be started with the "-n" option. These options are set in the directory /etc/default, so you need to edit the file /etc/default/gpsd to change the line: GPSD_OPTIONS="" to GPSD_OPTIONS="-n". Method A is to do this through dpkg-reconfigure gpsd (which appears not to work as expected with Raspbian Jessie), method B is to edit the file directly. Set USBAUTO="false". Set DEVICES="/dev/ttyAMA0".
With Raspbian Jessie, gpsd doesn't appear to auto-start properly, but Tomasz Torcz on the comp.sys.raspberry-pi Usenet group suggested trying:
and that appears to do the trick.
You should then find that gpsd restarts after a reboot.
Changes to your NTP configuration
Once GPSD is installed, you can use the shared memory driver, type 28 to point NTP to the nearest second. For example, your ntp.conf file might include:
Note that with the type 28 driver you may want "flag1 1" in the fudge line, as this overrides the 4 hour maximum offset sanity check built into the type 28 ref-clock. This is still under investigation. There is already a "-g" parameter specified in Raspbian for that start of ntpd, which should allow it to step the time as much as is required at startup (which could be several days or weeks if your Raspberry Pi has been powered down for some time), but it seems that the type 28 driver has its own additional sanity check, which by default prevents correct operation after more than four hours down time. Thanks to Whitham D. Reeve from Anchorage, Alaska USA for the testing. The +0.105 aligns the timing of the serial data from the GPS approximately with the actual second edge. It doesn't need to be exact, but may make for faster acquisition of the true second edge. In an earlier version of this Web page I had suggested "stratum 15", but this no longer appears to work, hence "stratum 1".
Earlier notes than the above: To allow gpsd to access the serial port, you need to make some changes.
The easiest way to do this under Raspbian Jessie is to use the Raspi-config
command described here.
Select the Advanced option, Serial, No. Remember to use sudo!
HAB Supplies T/A Nevis Computers Limited
Phone: +44 1274 550910
Revision 1.3-djt 2016-Apr-06