0
0

Raspberry PI OS (bookworm) で dhcp の ntp-servers オプションを拾う

Posted at

Raspberry PI OS (bookworm) は dhcp の ntp-serves を既定(NetworkManager)では拾ってくれないので拾うようにしてみた。

Arch Linux のウェブドキュメントにある

systemd-timesyncd で DHCP 経由で受信した NTP サーバを動的に設定する

をそのまま使用してもいいのですが、少し弄って

  • ディレクトリを
    • /etc/systemd/timesyncd.conf.d から
    • /run/systemd/timesyncd.conf.d に変更
  • ディレクトリや .conf ファイル名をシェル変数化
  • ファイル名を簡略化(後の OS アップデートで衝突するかもしれませんが…)

としてみた。

/etc/NetworkManager/dispatcher.d/02-timesyncd
#!/bin/sh

[ -z "$CONNECTION_UUID" ] && exit 0

INTERFACE="$1"
ACTION="$2"

CONF_DIR=/run/systemd/timesyncd.conf.d
CONF_FILE="$CONF_DIR/${CONNECTION_UUID}.conf"

case $ACTION in
up | dhcp4-change | dhcp6-change)
  [ -n "$DHCP4_NTP_SERVERS" ] || exit 0
  mkdir -p "$CONF_DIR"
  echo "[Time]" > "$CONF_FILE"
  echo "NTP=$DHCP4_NTP_SERVERS" >> "$CONF_FILE"
  systemctl restart systemd-timesyncd.service
  ;;
down)
  rm -f "$CONF_FILE"
  systemctl restart systemd-timesyncd.service
  ;;
esac
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0