MichaIng / DietPi

Lightweight justice for your single-board computer!
https://dietpi.com/
GNU General Public License v2.0
4.84k stars 496 forks source link

DietPi-Set_Software | Add ability to allow local time server as time sync mirror #1688

Closed MichaIng closed 6 years ago

MichaIng commented 6 years ago

I just recently recognized, that my router serves as time sync mirror within my local network, while itself syncs regularly with configured external time server, e.g. preconfigured by ISP. In such setups it should be beneficial for DietPi to sync with the local server instead of external as well.

When entering the local network address into dietpi-config ntpd mirror, it is added the wrong way, e.g. (0-3).192.168.178.1 which are of course not available (?). If you want this, just 192.168.178.1 needs to be added.

We could add a separate menu entry/option to add a local IP/hostname, which is then added the correct way, with e.g. local gateway (DHCP server) as default preconfigured entry, which will in most cases be correct. Another possibility would be to just allow it via "custom" entry, by e.g. adding "local" in front of IP/hostname. Then we would not need to add new menu items, just grep for "local" and in case add the following string without (0-3)... to the ntp/timesyncd config file.

Affected code lines: https://github.com/Fourdee/DietPi/blob/testing/dietpi/func/dietpi-set_software#L183-L222

Any ideas/suggestions about this?

k-plan commented 6 years ago

Hi @MichaIng ,

In such setups it should be beneficial for DietPi to sync with the local server instead of external as well.

Richtig. 😃 Und ich mag die Idee, einen lokalen NTP Server zu nutzen und konfigurieren zu können.

Another possibility would be to just allow it via "custom" entry, by e.g. adding "local" in front of IP/hostname.

Puh ... das wird komplex bei der Endnutzer Konfiguration. Weiss nicht, ob das etwas für unbedarfte Quereinstiger ist. Klingt mir mehr nach einer "Entwickler"-Lösung. Vorteil den ich noch sehe, man muss nichts im dietpi.txt Handling ändern und in dietpi-globals anpassen. Ob ich mich damit anfreunden könnte? Jein.

We could add a separate menu entry/option to add a local IP/hostname, which is then added the correct way, ...

Sagt mehr eher zu. Wobei ich für eine Untermenü in Custom Manually enter NTPD mirror plädieren würde.



Das bedarf dann aber einiger Anpassungen in `whiptail menu` , im Handling von `dietpi.txt`  und in `dietpi-globals`.

----------------

> I just recently recognized, that my router serves as time sync mirror within my local network, ....

Das machen aber nicht alle Router. Bei einigen "Billig-Plastik-Router" geht das gar nicht, bei einigen muss man es erst Konfigurieren (z.B. Mikrotik) und einige machen es einfach ohne Konfigurationsmöglichkeiten (z.B. Fritz!Box) wenn sie einen NTP Server via DHCP oder TR-069 auf der WAN Schnittstelle beziehen. Einige haben auch einfach ein paar NTP Server Adressen fix ins Firmware einkompiliert.
![180408-0002](https://user-images.githubusercontent.com/17237601/38469205-2f36aaf6-3b51-11e8-9be1-baab41ee4f25.gif)
Aber lassen das vorerst einmal unberücksichtigt. 

**DHCP in Verbindung mit NTP:**

Hier kann man sehen, wie der DHCP Server konfiguriert ist:

![180408-0001](https://user-images.githubusercontent.com/17237601/38469219-69ba57b8-3b51-11e8-8b4a-be11469da1ba.gif)

Das ist auch so ein Ding.
Im Moment läuft das mit den damit verbundenen DHCP Clients  noch nicht so richtig "standardkonform" bei einigen Debian based Images.

Ist auf einem Gerät der IP Adressbezug mittels dhcp client aktiviert und propagiert der [DHCP Server einen NTP Server](http://www.iana.org/assignments/bootp-dhcp-parameters/bootp-dhcp-parameters.xhtml) via `dhcp option 42` sollte der Client diesen auch benutzen. Das macht der Client im Moment aber nicht.

Bezieht der Client die IP-Adresse via DHCP , dann sollte er die Konfiguration des Zeitservers nicht aus der Datei `/etc/ntp.conf`, sondern aus `/var/lib/ntp/ntp.conf.dhcp`einlesen. Diese Datei wir aber nicht angelegt.
Konfiguriert man den NTP Server per Hand, muss `/var/lib/ntp/ntp.conf.dhcp` gelöscht werden, Netzwerk und Zeitdienste müssen neu gestartet werden. Dann wird `/etc/ntp.conf` eingelesen und verwendet.

Soweit ich das bisher verstanden habe, hängt das mit `/etc/dhcp/dhclient.conf` , `/etc/dhcp/dhclient-exit-hooks.d/ntp` , `/var/lib/ntp/ntp.conf.dhcp` zusammen.
Da hapert es irgendwo. Ob bei allen Images, kann ich nicht sagen.
Wenn deine bridged Dietpi VM via DHCP eine Adresse von der fritz.box bezieht, müsste diese auch der primäre NTP Server deiner VM werden.

https://serverfault.com/questions/329596/how-to-override-the-ntp-information-sent-by-dhcp-in-debian

http://www.raspberry-pi-geek.de/Magazin/2014/03/RasPi-als-DHCP-und-DNS-Server/(offset)/2

https://forum.ubuntuusers.de/topic/ntpd-ignoriert-serverliste-in-etc-ntp-conf/

Sorry, das ist auch wieder so eine Mammut Ding. Hätte ich gerne ausgespart. Aber wenn man es anfasst, sollte man es auch gemäß den Standard tun.
Fourdee commented 6 years ago

@MichaIng @k-plan

Maybe a simple solution, could we not just add the current IP gateway (eg: router) to the server list automatically?

server 192.168.0.1
server 0.europe.pool.ntp.org iburst
server 1.europe.pool.ntp.org iburst
server 2.europe.pool.ntp.org iburst
server 3.europe.pool.ntp.org iburst

My router doesnt support NTP, however, it still passed via external pool server.

EDIT:

We could also have a dietpi.txt entry with:

#Change the local NTP mirror, gateway=default, eg: router, or use a different ip address etc
CONFIG_NTP_LOCAL_MIRROR=gateway

Then additional dietpi-config option to change it?

k-plan commented 6 years ago

@Fourdee

Maybe a simple solution, could we not just add the current IP gateway (eg: router) to the server list automatically?

😃 - yes it's simple. But I think, @MichaIng want to avoid traffic and to pester public ntp server with his LAN devices ...

But I have here more when one SNTP server for highly available in local area network. What should I do? And if I want to ask my local ISP for NTP, because he response quicker when pool.ntp.org server, what should I do?

My router doesnt support NTP, ...

Most network devices only support SNTP server, not completely implement NTP server ...

...however, it still passed via external pool server.

Yes, cheap-plastic-router from Shenzhen market ... 😄 Should I sent you a real one?

We could also have a dietpi.txt entry with:

Nice! 👍 Can we have more than one entry, eg. three or four? And can we have the option to disable the NTP pool server, if CONFIG_NTP_LOCAL_MIRROR is set?

MichaIng commented 6 years ago

@k-plan @Fourdee Jep, the idea was to avoid traffic and perhaps reduce sync time by allowing to just sync with local NTP server. I am not sure how it is handled, if the local IP/host is just added to the servers list.

As dietpi.txt design currently always has all settings set, it would need three settings all together then:

But hmm to keep it simple... Isn't actually this 0. / 1. only available for the ntp.org pool servers, thus we could just grep for pool.ntp.org within mirror string to decide if we add it as single server or 4 times with prefix? On the other hand, who knows if here and there it is adopted by local NTP servers as well...

MichaIng commented 6 years ago

https://manpages.debian.org/testing/systemd/timesyncd.conf.5.en.html

Idea: Always set local gateway as as NTP server and pool servers as FallbackNTP to reduce unnecessary NTP pool server load?

MichaIng commented 6 years ago

Okay, following solution, I would go for:

@Fourdee @k-plan What do you think?

Fourdee commented 6 years ago

@MichaIng

Apologies for the delay, been a bit manic recently lol.

By default use local gateway as NTP server and debian.pool.ntp.orgas FallbackNTP

Yes, love this idea and the additional two options. Worth implementing 👍

MichaIng commented 6 years ago

@Fourdee Okay great, leave this to me, will create PR later.

MichaIng commented 6 years ago

🈺 if NTP= configured local server fails, the daemon does not fallback to the FallbackNTP= servers automatically. FallbackNTP= is only used, if no NTP= is defined and no per-network NTP server within /etc/systemd/network/* configuration: https://manpages.debian.org/stretch/systemd/timesyncd.conf.5.en.html

Thus, we need to let user choose between local NTP server, optional automated gateway test, and ntp.pool.org servers.

MichaIng commented 6 years ago

Testing

Jessie

Stretch

Buster

Implementation

MichaIng commented 6 years ago

Yeyyy, finally done: https://github.com/Fourdee/DietPi/pull/1974

This even allows to enter multiple custom mirrors (space-separated), and *.pool.ntp.org entries will be added with leading [0-3]. automatically, the others will be left without. Automated local gateway detection, theoretically allowing multiple default gateways as well (if this can be even the case?), reworked explanations and recommendation.

Fourdee commented 6 years ago

🈯️ https://github.com/Fourdee/DietPi/commit/7b5af9ee300534a774b2089c912b6b765e8493fb We need to force check ntp sync during dietpi-config, eg: gateway

root@DietPi:~# systemctl status systemd-timesyncd -l
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; disabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Tue 2018-07-31 22:55:40 BST; 14s ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 14182 (systemd-timesyn)
   Status: "Idle."
   CGroup: /system.slice/systemd-timesyncd.service
           └─14182 /lib/systemd/systemd-timesyncd

Jul 31 22:55:40 DietPi systemd[1]: Starting Network Time Synchronization...
Jul 31 22:55:40 DietPi systemd[1]: Started Network Time Synchronization.
Jul 31 22:55:50 DietPi systemd-timesyncd[14182]: Timed out waiting for reply from 192.168.0.1:123 (192.168.0.1).

🈯️ Fixed below commit | I may of broke dietpi-config menu for this, working on it now.

Fourdee commented 6 years ago

@MichaIng

Great work, seems to work a treat 👍

Although I couldnt get gateway to work on my network (https://www.tp-link.com/uk/products/details/cat-9_Archer-C2.html), lacks local NTP server by looks of it.

MichaIng commented 6 years ago

Finished, will mark this as closed.