mDNS with Avahi
Instead the default mDNS repeater avahi is used to reflect mDNS between multiple interfaces on OPNSense. This allows devices on different interfaces to discover each other via mDNS, which is useful for services like AirPlay, Chromecast, and other local network services. The script below installs and configures Avahi to reflect mDNS between interfaces with descriptions matching the specified names. It also enables and starts the necessary services.
#!/bin/shset -e
# Space-separated list of interface description names to reflect mDNS betweenINTERFACES="lan iot mgmt wghomelab wgmgmt"
# ---- resolve description names to actual interface names ----resolve_iface() { name="$1" ifconfig -a | awk -v name="$name" ' /^[a-z]/ { iface = $1; gsub(/:$/, "", iface) } /description:/ { if (index($0, name) > 0) print iface } ' | head -1}
echo "==> Resolving interface descriptions..."RESOLVED=""for desc in $INTERFACES; do iface=$(resolve_iface "$desc") if [ -z "$iface" ]; then echo "ERROR: No interface found with description containing '${desc}'" >&2 exit 1 fi echo " ${desc} -> ${iface}" RESOLVED="${RESOLVED} ${iface}"done
ALLOW_INTERFACES=$(echo "$RESOLVED" | xargs | tr ' ' ',')echo "==> Resolved interfaces: ${ALLOW_INTERFACES}"
# ---- install ----echo "==> Installing avahi-app..."pkg install -y avahi-app
# ---- config ----echo "==> Writing avahi-daemon.conf..."mkdir -p /usr/local/etc/avahicat > /usr/local/etc/avahi/avahi-daemon.conf << EOF[server]use-ipv4=yesuse-ipv6=yescheck-response-ttl=nouse-iff-running=noallow-interfaces=${ALLOW_INTERFACES}
[publish]publish-addresses=yespublish-hinfo=yespublish-workstation=nopublish-domain=yes
[reflector]enable-reflector=yesreflect-ipv=no
[rlimits]rlimit-core=0rlimit-data=4194304rlimit-fsize=0rlimit-nofile=30rlimit-stack=4194304rlimit-nproc=10EOF
sed -i '' 's/service avahi-daemon restart || true/service avahi-daemon stop; sleep 1; service avahi-daemon start || true/' /usr/local/etc/avahi/avahi-daemon.conf
# ---- services ----echo "==> Enabling and starting dbus..."sysrc dbus_enable="YES"service dbus start || service dbus restart
echo "==> Enabling and starting avahi-daemon..."sysrc avahi_daemon_enable="YES"# avahi exits non-zero on first start when cleaning up a stale PID file# the subsequent start succeeds — verify with pgrep insteadif pgrep -q avahi-daemon; then service avahi-daemon restart || trueelse service avahi-daemon start || truefisleep 1if ! pgrep -q avahi-daemon; then echo "ERROR: avahi-daemon failed to start" >&2 exit 1fi
echo ""echo "Done. Avahi is reflecting mDNS on: ${ALLOW_INTERFACES}"echo "Remember to disable the OPNsense mDNS Repeater under Services -> mDNS Repeater."