Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5009

Raspberry Pi OS • Re: dhcpcd not learning NTP servers from DHCP

$
0
0
A script for updating timesyncd ntp sources via dhcp under NetworkManager. Tested on Debian 12 bookworm for raspberry pi and amd64. I have not tested DHCP6 as my router only supports SLACC.

Installation
============
# sudo mkdir /usr/local/etc/network

# Open a new file with nano
sudo nano /usr/local/etc/network/timesync.sh

# Paste this code block 'timesync,sh" into the file, save, exit and make executable
sudo chmod 755 /usr/local/etc/network/timesync.sh

Note: I could put this file in /etc/network/ or maybe /etc/network/hooks/ but I not sure if that is considered good practice.

# Check timesysyncd for manual ntp servers
sudo nano /etc/systemd/timesyncd.conf

# Remove manual servers by commenting out NTP=
[Time]
#NTP=

# If does not exist create the .conf.d directory
sudo mkdir /etc/systemd/timesyncd.conf.d

# Create symbolic links
sudo ln -s /usr/local/etc/network/timesync.sh /etc/network/if-up.d/timesync
sudo ln -s /usr/local/etc/network/timesync.sh /etc/network/if-post-down.d/timesync
sudo ln -s /usr/local/etc/network/timesync.sh /etc/network/if-pre-up.d/timesync
sudo ln -s /usr/local/etc/network/timesync.sh /etc/network/if-down.d/timesync

# Enable and refresh the interface. The new script should restart timesyncd.
sudo timedatectl set-ntp true
sudo nmcli connection down 'Wired connection 1'
sudo nmcli connection up 'Wired connection 1'

# Check status
timedatectl show-timesync

# For debug info:
cat /tmp/nmtimesync.log



timesync.sh

Code:

#!/bin/sh# Keith Chapple 7/9/2024## IPV6 has not been tested as my environment uses SLAAC.## A script for for applying ntp server information, derived from# dhcp managed by NetworkManager, to Timesyncd## Based on a script for dhcpclient from Tim at#       https://codingtim.github.io/raspberry-pi-ntp-dhcp/## Tim's script works on debian 11 "Bullseye" and, I think,# debian 10 "Buster" which both use dhcp client.# However debian 12 "Bookworm" uses NetworkManager## This script works on debian 12 for amd64 and Raspberry Pi OS# variant of debian 12## Installation# ============#   sudo mkdir /usr/local/etc/network## Open a new file with nano#   sudo nano /usr/local/etc/network/timesync.sh## Paste this code block ino the file, save, exit and make executable#   sudo chmod 755 /usr/local/etc/network/timesync.sh## Note: I could put this file in /etc/network/ or maybe# /etc/network/hooks/ but I not sure if that is considered good# practice## Check timesysyncd for manual ntp servers#   sudo nano /etc/systemd/timesyncd.conf## Remove manual servers by commenting out NTP=#  [Time]#  #NTP=## If does not exist create the .conf.d directory#   sudo mkdir /etc/systemd/timesyncd.conf.d## Create symbolic links#   sudo ln -s /usr/local/etc/network/timesync.sh /etc/network/if-up.d/timesync#   sudo ln -s /usr/local/etc/network/timesync.sh /etc/network/if-post-down.d/timesync#   sudo ln -s /usr/local/etc/network/timesync.sh /etc/network/if-pre-up.d/timesync#   sudo ln -s /usr/local/etc/network/timesync.sh /etc/network/if-down.d/timesync## Enable and refresh the interface.  The new script should restart timesyncd.#   sudo timedatectl set-ntp true#   sudo nmcli connection down 'Wired connection 1'#   sudo nmcli connection up 'Wired connection 1'## Check status# timedatectl show-timesync## For debug info: cat /tmp/nmtimesync.logdebug="yes"# If DHCP client is not running or does not contain a ntp server# this script will delete the current interface file and restart timesycd# for one iterartion only.  If you want to overide the dncp server or# for piece of mind it can be disabled.disable="no"# DEBUGif [ "$debug" = "yes" ]; then    debugfile="/tmp/nmtimesync.log"    echo "----"$(date)"-----------------------------------------" >> "$debugfile"    # date >> "$debugfile"    echo "$0" >> "$debugfile"    env >> "$debugfile"else    debugfile="/dev/null"fiif [ "$disable" = "yes" ]; then    echo "exiting - disable = yes" >> "$debugfile"    exitfi# The remaining code is enclosed in brackets# The entire block is run in a subshell.  All status and error# messages are are captured with a single redirection# statement at the end.(    configdir="/etc/systemd/timesyncd.conf.d/"    start_timesync () {        echo "restarting timesyncd"        systemctl restart systemd-timesyncd    }    remove_servers () {        if [ -e "$configfile" ]; then            echo "removing existing interface.conf file - $configfile"            rm "$configfile"            start_timesync        fi    }    update_server_list () {        if [ -z "$servers" ]; then            echo "no servers - Address family = \"$addrfamily\""            remove_servers        else            # Create interface.conf file            echo "creating interface.conf file - $configfile"            echo "# Generated by NetworkManager" > "$configfile"            echo "[Time]" >> "$configfile"            echo "NTP=$servers" >> "$configfile"            start_timesync        fi    }    # MODE=stop|start    # PHASE=post-down|post-up    # ADDRFAM=inet|inet6    mode="$MODE"    phase="$PHASE"    addrfamily="$ADDRFAM"    interface="$IFACE"    case "$interface" in        lo|--all)            echo "exiting - invalid interface \"$interface\""            exit            ;;        "")            echo "exiting - no interface"            exit    esac    case "$addrfamily" ininet)           servers="$DHCP4_NTP_SERVERS"           ;;        inet6)          servers="$DHCP6_NTP_SERVERS"          ;;        *)         echo "exiting - unknown address family \"$addrfamily\""         exit    esac    configfile="$configdir$interface-$addrfamily.conf"    if [ "$mode $phase" = "start post-up" ]; then        update_server_list    fi    if [ "$mode $phase" = "stop post-down" ]; then        remove_servers    fi    # The closing bracket and redirection statement) >> "$debugfile" 2>&1

Statistics: Posted by kmacdchap — Sat Sep 07, 2024 4:35 am



Viewing all articles
Browse latest Browse all 5009

Trending Articles