configuration dhcp ntp ntpd dhclient – How to Set Up a Linux Client to Use NTP Information Provided Through DHCP

configurationdhclientdhcpntpntpd

there are so many tutorials out there explaining how to setup dhcpd server, in relation to providing ntp suggestions to dhcp clients, that I had always thought that ntp configuration was carried out automatically. Recently I started seeing clock drifts in my local network, so I assume this was a wrong assumption. So I set out to see how can one minimize the ntp client configuration, provided one has carried out the effort to set up ntp-server suggestions through dhcpd.

I have not been able to find much apart from this Ubuntu specific help tutorial https://help.ubuntu.com/community/UbuntuTime . Even here (see paragraph under "Troubleshooting -> Which configuration file is it using?") the information is scarce but it says that if an /etc/ntp.conf.dhcp file is found it will be used instead. First of all the actual location that the writer meant here is /var/lib/ntp/ntp.conf.dhcp as observed in /etc/init.d/ntp , but regardless of that the presence of this file does not guarantee that the ntp will request servers from dhclient. As a result, I have to explicitly add the server clause in ntp.conf.dhcp for my local ntp server. But in that case, why do I even setup ntp settings on the dhcpd server?

This seems to go against intuition, ie setup ntp settings once (ie on the server) and let dhcpd server delegate the information to the clients. How can I minimize (if not avoid altogether), client configuration for the ntp. Alternatively, how can I get ntp information through dhclient.

Is there a cli solution that fits all linux distros?

I assume every client should have the executables of ntpd, but I do not know how to proceed from there.

Thank you

EDIT:
ubuntu client verbose output when running manually dhclient:

sudo dhclient -1 -d -pf /run/dhclient.eth0.pid -lf /var/lib/dhcp/dhclient.eth0.leases eth0
Internet Systems Consortium DHCP Client 4.2.4
Copyright 2004-2012 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth0/20:cf:30:0e:6c:12
Sending on   LPF/eth0/20:cf:30:0e:6c:12
Sending on   Socket/fallback
DHCPREQUEST of 192.168.112.150 on eth0 to 255.255.255.255 port 67 (xid=0x2e844b8f)
DHCPACK of 192.168.112.150 from 192.168.112.112
reload: Unknown instance: 
invoke-rc.d: initscript smbd, action "reload" failed.
RTNETLINK answers: File exists
 * Stopping NTP server ntpd
   ...done.
 * Starting NTP server ntpd
   ...done.
bound to 192.168.112.150 -- renewal in 41963 seconds.

The ntpd service is restarted, yet running ntpq -cpe -cas afterwards I still do not see my local ntp server in the list of ntp servers.

Of course my dhcpd server does have option ntp-servers

subnet 192.168.112.0 netmask 255.255.255.0 {
        max-lease-time 604800;
        default-lease-time 86400;
        authoritative;
        ignore client-updates;

        option ntp-servers 192.168.112.112; #self

        ... (many other options)
}

Best Answer

If the dhcp server you are using is configured to provide the ntp-servers option, you can configure your dhclient to request ntp-servers by adding ntp-servers to the default request line in dhclient.conf, as shown at the end of this example from Ubuntu Linux (as of 19.04, but present since at least 12.04):

request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, domain-search, host-name,
        dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
        netbios-name-servers, netbios-scope, interface-mtu,
        rfc3442-classless-static-routes, ntp-servers;

/etc/ntp.conf and the information from DHCP will be used to create /etc/ntp.conf.dhcp.

Your ntpd must be told to use /etc/ntp.conf.dhcp if it exists. On the version of Ubuntu that I'm using, this is done via /etc/dhcp/dhclient-exit-hooks.d/ntp. <-- this is the file that tells NTPd to use /etc/ntp.conf.dhcp if it exists, and to just use /etc/ntp.conf if it doesn't.

Related Question