colinhogben
Posts: 7
Joined: Fri Jun 08, 2012 7:20 am

dhcpcd not passing NTP servers to systemd-timesyncd

Tue Jul 10, 2018 2:43 pm

I have a Compute Module 3 running Stretch (2018-04-18 version with latest update/upgrade applied) and trying to get time synchronised with our local NTP servers.

By default, systemd-timesyncd is trying various servers *.debian.pool.ntp.org, but these all time out due to our site firewall configuration.

The local NTP server addresses are supplied by dhcp, and I can see these written to the file /run/ntp.conf.dhcp . However, since Raspbian is using systemd-timesyncd rather than ntpd, that doesn't appear to have any effect. It looks like some glue is missing.

Googling and manpage-reading found a temporary workaround - I added a file /etc/systemd/timesyncd.conf.d/site.conf containing approximately

Code: Select all

[Time]
NTP=ntp-0.localdomain ntp-1.localdomain
However, this is only good until system administrators decide to move things or we deploy the module on a different network - which is why we need to use the server addresses from DHCP.

Does anyone know how this should be done properly? Or have I missed something?

donbrew
Posts: 87
Joined: Sun Sep 04, 2016 2:32 pm

Re: dhcpcd not passing NTP servers to systemd-timesyncd

Wed Jul 11, 2018 2:48 pm

Is there some reason it can't get the time from the network, that is the default?

colinhogben
Posts: 7
Joined: Fri Jun 08, 2012 7:20 am

Re: dhcpcd not passing NTP servers to systemd-timesyncd

Wed Jul 11, 2018 9:45 pm

donbrew wrote:
Wed Jul 11, 2018 2:48 pm
Is there some reason it can't get the time from the network, that is the default?
Yes. The Pi is on a network which is isolated from the outside world by a firewall which does not allow NTP traffic through. However, there are local NTP servers which are dual-hosted i.e. one interface on the internal network (to act as server to internal hosts) and another on the external network (to allow synchronisation with the rest of the world).

User avatar
DougieLawson
Posts: 42772
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: dhcpcd not passing NTP servers to systemd-timesyncd

Thu Jul 12, 2018 7:10 am

Have you enabled ntp with sudo timedatectl set-ntp 1?
Have you installed ntpd with a /etc/ntp.conf update to point to your LAN server?
Change this stuff

Code: Select all

server 0.ubuntu.pool.ntp.org burst iburst maxpoll 11
server 1.ubuntu.pool.ntp.org burst iburst maxpoll 11
server 2.ubuntu.pool.ntp.org burst iburst maxpoll 11
server 3.ubuntu.pool.ntp.org burst iburst maxpoll 11
to

Code: Select all

server 192.168.xxx.yyy
where that's the address of the server on your LAN.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

ejolson
Posts: 11768
Joined: Tue Mar 18, 2014 11:47 am

Re: dhcpcd not passing NTP servers to systemd-timesyncd

Thu Jul 12, 2018 7:32 pm

colinhogben wrote:
Tue Jul 10, 2018 2:43 pm
Does anyone know how this should be done properly?
I think the idea behind including simple versions of ntpd, dhcpcd, udev and others in systemd is to speedup how fast the system reboots in the desktop usage case. For servers and situations not related to desktop use the trade-off between reliability, flexibility and reboot speed favours a different init philosophy using the original daemons to provide full featured services. If you install ntpd using

# apt-get install ntpd ntpdate

systemd should be graceful enough to allow ntpd handle the time synchronization. You may also need to remove the

timesyncd.conf.d/site.conf

file you created or manually disable the systemd time synchronization daemon. After this ntpd should work normally.

Do you usually use an includefile directive in ntp.conf to source the file with the names of the time servers provided by dhcp?

I have a similar setup with a cluster of Pi Zero computers that can't reach the Internet. In that case, however, there is no need to automatically configure ntpd using dhcp.

colinhogben
Posts: 7
Joined: Fri Jun 08, 2012 7:20 am

Re: dhcpcd not passing NTP servers to systemd-timesyncd

Fri Jul 13, 2018 7:32 am

DougieLawson wrote:
Thu Jul 12, 2018 7:10 am
Have you enabled ntp with sudo timedatectl set-ntp 1?
Yes, "timedatectl status" shows that ntp is enabled. The issue is that it's not using the servers obtained from DHCP.
ejolson wrote:
Thu Jul 12, 2018 7:32 pm
I think the idea behind including simple versions of ntpd, dhcpcd, udev and others in systemd is to speedup how fast the system reboots in the desktop usage case. For servers and situations not related to desktop use the trade-off between reliability, flexibility and reboot speed favours a different init philosophy using the original daemons to provide full featured services. If you install ntpd ...
Well, yes, I could go the route of switching from systemd-timesyncd to traditional ntpd and then the dhcp hook machinery would work as it used to.

Or, perhaps, go in the other direction and use systemd-networkd instead of dhcpcd. But, as you allude to, that may not give me the flexibility I need (e.g. to deal with some other DHCP options).

I was hoping/expecting that the default combination of packages and services chosen by the distro (Raspbian Lite Stretch) would be fully integrated together and work out-of-the-box - hence my original comment about some missing "glue". Maybe I should raise an issue against Raspbian.

colinhogben
Posts: 7
Joined: Fri Jun 08, 2012 7:20 am

Re: dhcpcd not passing NTP servers to systemd-timesyncd

Fri Jul 13, 2018 8:41 am

I've created /lib/dhcpcd/dhcpcd-hooks/50-timesyncd.conf containing the following:

Code: Select all

# Set NTP servers for systemd-timesyncd

confd=/run/systemd/timesyncd.conf.d

set_servers() {
    mkdir -p "$confd"
    (
        echo "# Created by dhcpcd hook"
        echo "[Time]"
        echo "NTP=$new_ntp_servers"
    ) > "$confd/dhcp-ntp.conf"

    # Tell timesyncd it has an updated configuration
    systemctl try-reload-or-restart systemd-timesyncd
}

if $if_up; then
    set_servers
fi
which seems to work.

ejolson
Posts: 11768
Joined: Tue Mar 18, 2014 11:47 am

Re: dhcpcd not passing NTP servers to systemd-timesyncd

Fri Jul 13, 2018 6:14 pm

colinhogben wrote:
Fri Jul 13, 2018 8:41 am
I've created /lib/dhcpcd/dhcpcd-hooks/50-timesyncd.conf
That script looks reasonable. Thanks for posting. Do you need to check whether $new_ntp_servers is non-null before updating?

Return to “Raspberry Pi OS”