c4n1
Posts: 10
Joined: Wed Mar 07, 2018 6:39 am

Buster DHCP configured NTP servers

Mon Jul 22, 2019 8:03 am

I'm wondering what I need to do to enable either NTP or timesyncd to get their NTP servers from DHCP handout. I have a working DHCP server which is handing out the correct options to an older version of Raspbian but in a new Buster install I cannot seem to get it working.

I've tried adding "UseNTP=true" to /etc/systemd/networkd.conf
I've tried installing ntp in case it was a timesyncd issue. /etc/ntp.conf doesn't get populated with DHCP servers and
I ran "dhcpdump -i eth0" and saw the DHCP request come through but didn't see the NTP option, I double checked and ran the command on an older OS (Wheezy) on the same network and confirmed it's getting the option so no issue on the servers end.
I have seen it suggested that the dhclient.conf request line needs to have ntp configured but it already is.

The fact that I don't see the ntp option when running "dhcpdump -i eth0" on the Buster based Pi makes me thing that the DHCP client itself is at fault here. Can anyone suggest what I need to configure to enable dhclient requesting the ntp server?

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

Re: Buster DHCP configured NTP servers

Mon Jul 22, 2019 9:15 am

The default dhcp client in Raspbian is dhcpcd. In dhcpcd.conf there is a list of options the client reacts to, your config file probably has "option ntp_servers" set by default.
Local configuration happens via the "run-hook" scripts included with dhcpcd, in "/lib/dhcpcd/dhcpcd-hooks/"

If you don't see the option via tcpdump or dhcpdump (I don't know this one) I would suspect the server, not the client.
Since other clients seem to receive the option, I suppose dhcpdump is somewhat confused and I would instead put dhcpcd in verbose mode or add some trace commands in the various scripts related to ntp and see which one, if any, gets activated.

(You can also use the broadcast address to send NTP time over the network. It less efficient than unicast communication but assuming clients and server are properly configured, using broadcast would remove DHCP from the equation.)
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

c4n1
Posts: 10
Joined: Wed Mar 07, 2018 6:39 am

Re: Buster DHCP configured NTP servers

Tue Jul 23, 2019 2:33 am

After doing some more research it would appear that network manager was causing issues As described here https://askubuntu.com/questions/138373/ ... ot-invoked in the answer posted by Alvar (Annoyingly I have no rep there so can't upvote).

It appears that the dhclient hooks aren't always run. I noticed this by adding some touch and echo commands into the /lib/dhcpcd/dhcpcd-hooks/50-ntp.conf file along with enabling debugging in /etc/dhcp/dhclient-enter-hooks.d/debug after doing this when I ran "sudo dhclient -r -v" the lease renewed but did not run any of the scripts it was supposed to.

The fix was to add some info into /etc/network/interfaces

Code: Select all

# interfaces(5) file used by ifup(8) and ifdown(8)
# these two lines are here by default
auto lo
iface lo inet loopback

# this was missing in my case and while the address was retrieved via DHCP, additional 
# options like ntp-servers were never retrieved:
auto eth0
iface eth0 inet dhcp
After making that change and rebooting I could see info in /tmp/dhclient-script.debug and also my NTP servers configured in /run/ntp.conf.dhcp

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

Re: Buster DHCP configured NTP servers

Thu Jul 25, 2019 7:25 am

c4n1 wrote:
Tue Jul 23, 2019 2:33 am
I ran "sudo dhclient -r -v" the lease renewed but did not run any of the scripts it was supposed to.[/code]
dhclient does not run dhcpcd scripts. Only dhcpcd does.
By activating ifupdown you now have dhclient manage eth0. Possibly in addition to dhcpcd, which could lead to issues.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

c4n1
Posts: 10
Joined: Wed Mar 07, 2018 6:39 am

Re: Buster DHCP configured NTP servers

Wed Oct 02, 2019 5:54 am

Bumping this as I've had some more time to work out how to get this issue fixed the proper way. Initially I couldn't get epoch1970's suggestion of adding the option ntp_servers to /etc/dhcpcd.conf working but now it's all sorted.


Firstly (Thanks epoch1970)
We need to get dhcpcd requesting the ntp-servers option, I'm unsure why this would be disabled by default as it seems like the kind of thing that should be working out the box?

Code: Select all

/etc/dhcpcd.conf
option domain_name_servers, domain_name, domain_search, host_name
#becomes
option domain_name_servers, domain_name, domain_search, host_name ntp_servers
Secondly (Thanks colinhogben in this thread) we setup systemd-timesyncd to use the dhcp provided ntp servers using a hook in /lib/dhcpcd/dhcpcd-hooks/ I modified to only re-write the file and restart if the ntp servers change.

Code: Select all

/lib/dhcpcd/dhcpcd-hooks/60-timesyncd.conf

confd=/run/systemd/timesyncd.conf.d
conf_file=$confd/dhcp-ntp.conf

set_servers() {
    mkdir -p "$confd"
    (
        echo "# Created by dhcpcd hook"
        echo "[Time]"
        echo "NTP=$new_ntp_servers"
    ) > "$conf_file"

    systemctl try-reload-or-restart systemd-timesyncd
}

check_difference() {
    if [ -f "$conf_file" ]; then
        old_val=$(grep "NTP=" $conf_file |cut -d'=' -f2)
        if ! [ "$old_val" = "$new_ntp_servers" ]; then
            set_servers
        fi
    elif ! [ -z "$new_ntp_servers" ]; then
        set_servers
    fi
}

if $if_up; then
    check_difference
fi

Return to “Troubleshooting”