Table of Contents
Raspberry Pi NTP Server
This article shows how to use a Raspberry Pi on Ubuntu Mate with a real time clock to create a NTP server.
https://thepihut.com/products/adafruit-pirtc-pcf8523-real-time-clock-for-raspberry-pi
Configure Clock
- Power Off Raspberry Pi
- Install PFC8523 on I/O Pins and install the battery
- Power Up Raspberry Pi
- After login run
sudo raspi-configand enable i2c interface - Edit
/boot/config.txtand add the following to the end of the filedtoverlay=i2c-rtc,pcf8523 - After updating the
/boot/config.txt, reboot the Raspberry Pi - Manually set the data/time on the Raspberry Pi, then use
hwclock --systohc
to set the time on the pfc8523
- Confirm the pfc8523 is working correctly by using
hwclock --show
Configure Sync
In order to get the Raspberry Pi to read the RTC and keep the pfc8523 correctly synchronised follow the instructions below
Edit the file /etc/systemd/timesyncd.conf with the following entries:
[Time] NTP=0.europe.pool.ntp.org 1.europe.pool.ntp.org 2.europe.pool.ntp.org 3.europe.pool.ntp.org FallbackNTP=0.europe.pool.ntp.org 1.europe.pool.ntp.org 2.europe.pool.ntp.org 3.europe.pool.ntp.org
Update the file /etc/rc.local by adding the following lines:
[ ! -e /var/lib/systemd/clock -a "`systemctl is-active systemd-timesyncd | grep -i active`" ] && timedatectl set-ntp 1 > /dev/null 2>&1
Create a file /etc/systemd/system/hwclock-sync.service with the following contents:
[Unit] Description=Time Synchronisation from RTC Source After=systemd-modules-load.service RequiresMountsFor=/dev/rtc Conflicts=shutdown.target [Service] Type=oneshot ExecStart=/sbin/hwclock -s TimeoutSec=0 [Install] WantedBy=time-sync.target
Now we need to shutdown and disable services for ntp and fake-hwclock and remove the corresponding packages that come by default with the OS by executing the following set of commands on the “root” shell prompt:
systemctl stop fake-hwclock
systemctl disable fake-hwclock prefer-timesyncd.service systemd-timedated.service
apt-get -y remove fake-hwclock
To finally enable the automatic start-up of the RTC synchonrisation during bootup using the systemd-timesyncd service, execute the following commands on the shell prompt:
systemctl enable hwclock-sync systemd-timesyncd
systemctl start hwclock-sync systemd-timesyncd
Install NTP
apt-get install ntp
Edit /etc/ntp.conf
server 0.europe.pool.ntp.org server 1.europe.pool.ntp.org server 2.europe.pool.ntp.org server 3.europe.pool.ntp.org
Run the following commands to activate ntp
sudo systemctl enable ntp
sudo systemctl start ntp
After five minutes, run ntpq and use the command peers to show the clocks ntp is synchronised with
ntpq> peers
ntpq> exit
Now verify correct operation by using the
timedatectl
command to obtain results similar to below
Local time: Sun 2017-10-15 12:13:55 BST Universal time: Sun 2017-10-15 11:13:55 UTC RTC time: Sun 2017-10-15 11:13:55 Time zone: Europe/London (BST, +0100) Network time on: yes NTP synchronized: yes RTC in local TZ: no
