Linux dhcp ntp server

Motivation

In order to set the time of multiple hosts (Linux) in the isolated local network, we decided to run the NTP server on one of them and synchronize the time with the NTP client on the other. For each client host, we decided to set the NTP server address delivered by DHCP.

Setting up the client was more difficult.

The settings for the NTP server and the settings for distributing NTP server information with the DHCP server are described in various places. On the other hand, the client settings are highly environment-dependent and it took time to find appropriate information. It seems that the processing performed by the client side (Linux) at startup generally follows the following procedure.

  1. Launch a script (hook) when the DHCP client exits.
  2. The script started by the DHCP client updates the NTP client configuration file.
  3. Read the updated configuration file and start the NTP client.

Environment dependencies occur here ** DHCP clients ( dhclient (ISC-DHCP), dhcpcd , NetworkManager ‘s internal dhcp client, . ) and NTP clients ( ntpd , Due to the large number of combinations ** of openntpd , chrony , systemd-timesyncd , . ). Depending on the Linux distribution, it seems that the DHCP and NTP packages adopted as defaults when upgrading the version may change, and I am addicted if I do not notice it. If a non-default (= not used) DHCP client or NTP client package is also installed, ** you may be worried about rewriting a configuration file that is not used and not doing what you want.

For Raspberry Pi OS

In Raspberry Pi OS (64bit, Debian buster based), the default DHCP client = dhcpcd , NTP client = systemd-timesyncd . The hooks called by DHCP are like /lib/dhcpcd/dhcpcd-hooks/50-ntp.conf . This file has a description for ntpd , ʻopenntpd , chrony , but no description for systemd-timesyncd . Based on [Official website forum information](https://www.raspberrypi.org/forums/viewtopic.php?t=217832#p1340336), I added the following, and the NTP server dynamically started when the system started. It has been set and the time is set. ** There is a file called /etc/dhcp/dhclient-exit-hooks.d/timesyncd` that contains an implementation that seems to exist, but be aware that this is not used. ** **

Читайте также:  Linux set path file

/tmp/dhcpcd_ntp_conf(Correction part;diff output)

 --- /etc/dhcpcd.conf.orig 2019-11-13 23:44:50.000000000 +0900 +++ /etc/dhcpcd.conf 2020-09-16 08:28:53.595999273 +0900 @@ -30,7 +30,7 @@ option classless_static_routes option interface_mtu # Most distributions have NTP support. -#option ntp_servers +option ntp_servers # A ServerID is required by RFC2131. require dhcp_server_identifier 

/lib/dhcpcd/dhcpcd-hooks/50-ntp.conf(Postscript)

 # Set NTP servers for systemd-timesyncd confd=/run/systemd/timesyncd.conf.d if [ -n "$new_ntp_servers" ] ; then 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 fi 

By the way, in the systemd-timesyncd default configuration file ( /lib/systemd/system/systemd-timesyncd.service.d/disable-with-time-daemon.conf ), other NTP clients and VirtualBox services If the executable file of is present, nothing is done, so it is safer to add 50-ntp.conf instead of replacing it.

For Ubuntu MATE 20.04 LTS

For the image for Raspberry Pi (ʻubuntu-mate-20.04.1-beta2-desktop-arm64 + raspi.img ), the internal dhcp client of NetworkManager and systemd-timesyncd are used. It seems to be a combination of . By default, dhclient is also installed, and /etc/dhcp/dhclient-exit-hooks.d/timesyncd is also provided, so if you manually execute dhclient on the command line, the time will be synchronized. The default. So, I thought it would be better to add dhcp = dhclient to the [main] section of /etc/NetworkManager/NetworkManager.conf so that dhclient is used instead of internal dhcp. Looking at the log, dhclient` is throwing an error, and the hook is not called at the desired timing.

I gave up using dhclient because I had no choice, and decided to prepare a Dispather script for NetworkManager by referring to /etc/dhcp/dhclient-exit-hooks.d/timesyncd . Modify the environment variables and judgment strings according to the Official Manual, and /etc/NetworkManager/dispatcher.d/90-dhcp- Create timesyncd .

/etc/NetworkManager/dispatcher.d/90-dhcp-timesyncd

 #!/bin/sh TIMESYNCD_CONF=/run/systemd/timesyncd.conf.d/01-dhclient.conf timesyncd_servers_setup_remove() < if [ -e $TIMESYNCD_CONF ]; then rm -f $TIMESYNCD_CONF systemctl try-restart systemd-timesyncd.service || true fi >timesyncd_servers_setup_add() < if [ ! -d /run/systemd/system ]; then return fi old_ntp_servers=$(sed -ne 's/^NTP=//gp') if [ -e $TIMESYNCD_CONF ] && [ "x$DHCP4_NTP_SERVERS" = "x$old_ntp_servers" ]; then return fi if [ -z "$" ]; then timesyncd_servers_setup_remove return fi mkdir -p $(dirname $) cat $.new # NTP server entries received from DHCP server [Time] NTP=$DHCP4_NTP_SERVERS EOF mv $.new $ systemctl try-restart systemd-timesyncd.service || true > logger -i -t "$0" "action=$:NTP=$" case $NM_DISPATCHER_ACTION in up|dhcp4-change) timesyncd_servers_setup_add ;; down) timesyncd_servers_setup_remove ;; *) : ;; esac 

This file must be owned by root and given execute permissions.

% ls -l /etc/NetworkManager/dispatcher.d/90-dhcp-timesyncd -rwxr-xr-x 1 root root 1140 Apr 2 02:55 /etc/NetworkManager/dispatcher.d/90-dhcp-timesyncd 

By putting this script, the time is synchronized.

Читайте также:  Редактирование файлов через консоль linux

For JetPack 4.4 (Ubuntu 18.04 LTS based)

It seems that the previous version of Ubuntu also uses a combination of NetworkManager and systemd-timesyncd . Therefore, I thought that it would work with the same script as the previous section (Ubuntu MATE 20.04 LTS), but it did not work as expected as it was. It seems that the environment variable $ NM_DISPATCHER_ACTION is not set, probably because of the difference in the version of networkmanager-dispatcher . Therefore, instead, the expected behavior can be obtained by modifying the following so that the second argument at runtime is evaluated.

NetworkManager-Supports different versions of dispatcher

 --- Ubuntu_MATE-20.04LTS/etc/NetworkManager/dispatcher.d/90-dhcp-timesyncd 2020-09-20 21:52:17.950941231 +0900 +++ JetPack-4.4/etc/NetworkManager/dispatcher.d/90-dhcp-timesyncd 2020-09-22 01:18:46.160691469 +0900 @@ -34,9 +34,9 @@ EOF systemctl try-restart systemd-timesyncd.service || true > -logger -i -t "$0" "action=$:NTP=$" +logger -i -t "$0" "action=$>:NTP=$ $@" -case $NM_DISPATCHER_ACTION in +case $ in up|dhcp4-change) timesyncd_servers_setup_add ;; 

As an aside, Ubuntu 18.04 LTS’s NetworkManager seems to use dhclient instead of the internal dhcp client.

For Ubuntu Server 20.04 LTS

It seems that it is a combination of internal dhcp clinet of systemd-networkd and systemd-timesyncd . The DHCP NTP server is set up without any special action. I couldn’t find much information about systemd-networkd-dispatcher , so it’s unclear how the DHCP-acquired address is passed to systemd-timesyncd .

For CentOS 7.8 (20.03), CentOS 8.2 (20.04)

It is a combination of NetworkManager and chrony , and the DHCP NTP server is set up without doing anything. As an aside, it seems that CentOS 7 uses dhclient and CentOS 8 uses NetworkManager ‘s internal dhcp client.

Источник

how do you set up a linux client to use ntp information provided through dhcp?

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

serverfault.com/questions/329596/… may give you some clues. You probably want to look at /etc/dhcp/dhclient-exit-hooks.d/ntp to find the actual filename being used. Make sure that your dhclient.conf file is set up to request ntp-servers as well.

Читайте также:  Linux ntfs partition recovery

Источник

Оцените статью
Adblock
detector