Re: Need help with DHCPv4 client

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



For services/sockets your best bet might be to simply bind them to 0.0.0.0 such that they still work after the address change.

My solution for the DNS record is to use iproute2 tools which provide a monitor command (in the sample below filtered for IPv6 changes):

#!/usr/bin/sh
touch "$CACHE_DIRECTORY"/last-ip
ip monitor address dev "$IF_DEVICE" | grep --line-buffered -E "inet6 .* global" | while read -r update; do
    ip=$(echo "$update" | tr --squeeze-repeats " " | cut --delimiter " " --fields 4 | cut --delimiter / --fields 1 )
    if [ "$ip" != "$(cat "$CACHE_DIRECTORY"/last-ip)" ]; then
        echo "Updating to $ip"
        curl --silent --show-error "https://$LOGIN:$PASSWORD@$SERVER/nic/update?hostname=$DOMAIN&myip=$ip"
        echo "$ip" > "$CACHE_DIRECTORY"/last-ip
    fi
done

The script is simply run as a daemon. You can change to curl line with anything you like, e.g. restarting services or calling other scripts which are able to communicate with your DNS server. There are also dyndns daemons which do this for you.

Cheers, Nils

PS: Ideally I should update that script and move the login data into systemd credentials but I have not gotten around to that yet.


On Wed, Feb 19, 2025, 03:15 Thomas Köller <thomas@xxxxxxxxxxxx> wrote:
My host is on a dynamic IP address (IPv4 only), which it receives via
DHCP from my provider. So far, I have been using ISC dhclient for this,
the network interface being marked as unmanaged in its .network file.
But since dhclient isn't maintained any more, I am exploring ways to
replace it with systemd's DHCP client.

While address updates using systemd's DHCP client seem to work just
fine, there is a problem, however. Whenever the host's IP address
changes, some reconfiguration has to be done: running services listening
on sockets opened before the address change have to be restarted or at
least notified, and a dynamic DNS A record has to be updated. With ISC
dhclient, this used to be unproblematic, as it can be configured to
invoke external scripts when it receives a new address from the DHCP server.

As far as I can tell, systemd's DHCPv4 client does not provide any such
hooks. I looked at the D-Bus interface of systemd-networkd
(org.freedesktop.network1), but was unable to spot anything of that
sort. However, as I am unfamiliar with Dbus in general, it is very
possible that I missed something.

I even tried to write a program processing rtnetlink notifications
resulting from address or route changes, however, this approach turned
out to have it's own problems. At this level it is difficult to detect
the point when all changes have been completed and the system has
reached a new stable state, so that reconfiguration can safely commence.

I'd greatly appreciate any helpful comments!

Thomas


[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux