alruf
Posts: 4
Joined: Thu Dec 05, 2019 3:34 pm

Adding RTC & NTP server to RPI 4 running PiHole & Unbound

Thu Dec 05, 2019 3:56 pm

First post... please have mercy.


Scenario:

I have two Raspberry Pi 4 (4GB) running PiHole and Unbound to provide DNS services to my home network (about 70 clients on Unifi based gear). I recently decided to block all internet access to my IP Cameras due to security concerns therefore I needed to provide an internal time server so the cameras would be able to get their time updates.

Hardware/Software:

Raspberry Pi 4 - 4GB
DS3231SN RTC
Argon One case
POE Splitter to power the system
Raspbian Buster lite
Pi Hole w/ Unbound

What I did and/or tried:

I purchased the smallest RTCs I could find due to the need to fit them inside the Argon One case. I love this case so I was determined to make it work. I will attach pictures of the RTC mod along with my Arduino based mod that automatically powers on my Raspberry Pi after a power outage since this case requires the user to press the power button to do so. Given this system is acting as my DNS, I cannot afford for it to stay off after a power outage, shutdown, switch reboot, etc.

Pi Hole and Unbound were easy to install and everything is working fine. My issues started with the software needed to do the following:

1) Sync the RTC with a remote time server (NA NPS Pool)
2) Sync the RPI clock with the RTC
3) Respond to time requests from devices on my network

At first I got it working using NTPD but then I read that it was no longer the recommended software on Buster therefore I set out to figure out how. One of the posts with this theme is:

https://www.raspberrypi.org/forums/view ... p?t=178763

The issue is I can't get it to work. It would seem that the new method is only for those trying to setup a time client and not a time client AND server. If what I read most recently is true, I should actually stick with NTPD to accomplish what I want.

What I find incredibly frustrating about the Linux world is this constant churn of how things should be done. Instead of the same tool evolving, it appears the tool gets dumped in favour of the next big thing so I have to start from scratch each time.

I would appreciate it if someone can tell me how this should be done, but please keep in mind that I want to adopt the latest and greatest method to do so and not try to make something on its way out work. If NTPD is still the only and best way to do it, then I will stick with that... at least I got that to work once before.

*******************************************************************
STEPS (WILL UPDATE THEM AS I FIGURE THEM OUT)
*******************************************************************

1) Install DS3231 RTC on RPI board

2) Enable I2C using raspi-config

3) Install python-smbus and i2c-tools (I believe this enables you to check whether the RTC is working on the I2C bus)

Code: Select all

 sudo apt-get install python-smbus i2c-tools
To check whether the RTC is detected run:

Code: Select all

sudo i2cdetect -y 1
The 1 is for RPI 4. Other models may require another number. Under ID 68 you should see 68 to indicate the RTC is detected. Once the kernel driver is running, you should see UU instead of 68. You might see other IDs if you are not using a DS3231 or have other I2C devices hooked up.

4) Add kernel driver for DS3231 in /boot/config.txt by adding this line:

Code: Select all

dtoverlay=i2c-rtc,ds3231
After rebooting, you can check whether it works by repeating step 3.

5) Edit /etc/ntp.conf to list your preferred NTP servers

Default Servers commented out:

Code: Select all

# pool 0.debian.pool.ntp.org iburst
# pool 1.debian.pool.ntp.org iburst
# pool 2.debian.pool.ntp.org iburst
# pool 3.debian.pool.ntp.org iburst
New NTP Servers:

Code: Select all

pool 0.north-america.pool.ntp.org iburst
pool 1.north-america.pool.ntp.org iburst
pool 2.north-america.pool.ntp.org iburst
pool 3.north-america.pool.ntp.org iburst
6) Run following command to initiate NTP service:

Code: Select all

sudo systemctl enable ntp.service
Last edited by alruf on Thu Dec 05, 2019 7:35 pm, edited 2 times in total.

epoch1970
Posts: 5022
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Adding RTC & NTP server to RPI 4 running PiHole & Unbound

Thu Dec 05, 2019 6:52 pm

The NTP thing in systemd provides an SNTP (Simple NTP) client. Not a server.

For an NTP server, you can install ntpd or chrony.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

alruf
Posts: 4
Joined: Thu Dec 05, 2019 3:34 pm

Re: Adding RTC & NTP server to RPI 4 running PiHole & Unbound

Thu Dec 05, 2019 7:11 pm

@epoch1970 - Agreed, that is what I came to realize after much pain in trying the zillion "here is how you do it" instructions found on the web.

I am trying to figure out:

1) What software is responsible to update the RTC chip with time from a remote NPD server (x.north-america.pool.ntp.org)?
2) What software writes the RTC time to system time when needed?
3) How do I ensure the RTC chip gets updated when needed? My 2 systems run 24/7 with reboots only when I am updating stuff.
3) How do I enable NTPd to provide time to clients asking for it?

It was working after trying many instructions but some of those were on how to nuke NPD in favor of the systemd method so I think I need to reflash the systems and start over to get them working again. I am hoping to nail down the exact steps so I don't make unnecessary changes which will break something.

markkuk
Posts: 201
Joined: Thu Mar 22, 2018 1:02 pm
Location: Finland

Re: Adding RTC & NTP server to RPI 4 running PiHole & Unbound

Thu Dec 05, 2019 7:27 pm

alruf wrote:
Thu Dec 05, 2019 7:11 pm
1) What software is responsible to update the RTC chip with time from a remote NPD server (x.north-america.pool.ntp.org)?
3) How do I ensure the RTC chip gets updated when needed? My 2 systems run 24/7 with reboots only when I am updating stuff.
The Linux kernel updates the RTC every 11 minutes when the system is synchronized with NTP time.
alruf wrote:
Thu Dec 05, 2019 7:11 pm
3) How do I enable NTPd to provide time to clients asking for it?
Open port UDP/123 in the firewall. Define a pool of servers in /etc/ntp.conf. Run "sudo systemctl enable ntp.service". Don't overcomplicate things, it's really quite simple.

alruf
Posts: 4
Joined: Thu Dec 05, 2019 3:34 pm

Re: Adding RTC & NTP server to RPI 4 running PiHole & Unbound

Thu Dec 05, 2019 7:44 pm

@markkuk - thank you! I had updated the ntp.conf file but I was missing the command to initiate the service.

I updated my first post to include your instructions. So... to recap:

After enabling the RTC, the kernel will keep it updated without me needing to schedule anything. How does it know where to get the reference time from? Does it automatically ask NTPd to do so? Or do I need to have timesyncd configured as well?

I have enbled the ntp.service as you instructed and it appears to be working fine even after a reboot. I forced one of my cameras to pull time from it and it worked.

What about the instructions I found, and followed, to eliminate the 'fake hardware clock'? Should I be leaving it in place or should I be removing it?

Example of such instructions:

https://pimylifeup.com/raspberry-pi-rtc/

The article appears to state that you eliminate the fake clock script and re-enable the "original hardware clock script".

Tried "sudo systemctl status ntp" and got a few errors, but I've seen them before and they somehow went away so maybe it is not something I can control:

Code: Select all

sudo systemctl status ntp

● ntp.service - Network Time Service
   Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-12-05 13:30:24 CST; 16min ago
     Docs: man:ntpd(8)
  Process: 636 ExecStart=/usr/lib/ntp/ntp-systemd-wrapper (code=exited, status=0/SUCCESS)
 Main PID: 651 (ntpd)
    Tasks: 2 (limit: 4915)
   Memory: 2.0M
   CGroup: /system.slice/ntp.service
           └─651 /usr/sbin/ntpd -p /var/run/ntpd.pid -u 109:114

Dec 05 13:30:25 PrimaryPiHoleDNS ntpd[651]: error resolving pool 0.north-america.pool.ntp.org: Temporary failure in name resolution (-3)
Dec 05 13:30:26 PrimaryPiHoleDNS ntpd[651]: error resolving pool 1.north-america.pool.ntp.org: Temporary failure in name resolution (-3)
Dec 05 13:30:27 PrimaryPiHoleDNS ntpd[651]: error resolving pool 2.north-america.pool.ntp.org: Temporary failure in name resolution (-3)
Dec 05 13:30:29 PrimaryPiHoleDNS ntpd[651]: Soliciting pool server 103.105.51.156
Dec 05 13:30:30 PrimaryPiHoleDNS ntpd[651]: Soliciting pool server 69.89.207.99
Dec 05 13:30:31 PrimaryPiHoleDNS ntpd[651]: Soliciting pool server 50.205.244.108
Dec 05 13:30:32 PrimaryPiHoleDNS ntpd[651]: Soliciting pool server 208.75.88.4
Dec 05 13:30:36 PrimaryPiHoleDNS ntpd[651]: receive: Unexpected origin timestamp 0xe193d95d.13d99c61 does not match aorg 0000000000.00000000 from server@208.75.88.4 xmt 0xe193d95c.9f9a03b2
Dec 05 13:31:28 PrimaryPiHoleDNS ntpd[651]: Soliciting pool server 198.50.238.163
Dec 05 13:31:29 PrimaryPiHoleDNS ntpd[651]: Soliciting pool server 50.205.244.21

markkuk
Posts: 201
Joined: Thu Mar 22, 2018 1:02 pm
Location: Finland

Re: Adding RTC & NTP server to RPI 4 running PiHole & Unbound

Thu Dec 05, 2019 10:21 pm

alruf wrote:
Thu Dec 05, 2019 7:44 pm
After enabling the RTC, the kernel will keep it updated without me needing to schedule anything. How does it know where to get the reference time from?
NTPd adjusts the kernel time using the special adjtime and ntp_adjtime system calls. See "man ntp_adjtime" and https://www.eecis.udel.edu/~ntp/ntpfaq/ ... DISCIPLINE

I have removed fake-hwclock after installing a RTC. Instead of changing anything in /lib/udev, I have the following script in /etc/udev/rules.d/85-hwclock.rules

Code: Select all

# On the Raspberry Pi the RTC isn't available when systemd tries,
# set the time from RTC now when it is available.
KERNEL=="rtc0", RUN+="/sbin/hwclock --rtc=$root/$name --hctosys"

aspruil01
Posts: 2
Joined: Tue May 12, 2020 7:30 pm

Re: Adding RTC & NTP server to RPI 4 running PiHole & Unbound

Mon Jun 29, 2020 6:29 pm

Which RTC module did you purchase? I have the argon one case and I am trying to find a reasonable solution...

alruf
Posts: 4
Joined: Thu Dec 05, 2019 3:34 pm

Re: Adding RTC & NTP server to RPI 4 running PiHole & Unbound

Mon Jun 29, 2020 9:08 pm

I used this one:

https://www.amazon.com/gp/product/B07MR7T5D5

But keep in mind that you will have to remove the pin header, solder the PCB to the GPIO pins, and relocate the battery (or else it won't fit).

I would add a picture but I keep getting an error saying the image is too large. I reduced it down to 383KB and I still get the error.

Return to “Beginners”