Skip to content

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/sh
set -e
# Space-separated list of interface description names to reflect mDNS between
INTERFACES="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/avahi
cat > /usr/local/etc/avahi/avahi-daemon.conf << EOF
[server]
use-ipv4=yes
use-ipv6=yes
check-response-ttl=no
use-iff-running=no
allow-interfaces=${ALLOW_INTERFACES}
[publish]
publish-addresses=yes
publish-hinfo=yes
publish-workstation=no
publish-domain=yes
[reflector]
enable-reflector=yes
reflect-ipv=no
[rlimits]
rlimit-core=0
rlimit-data=4194304
rlimit-fsize=0
rlimit-nofile=30
rlimit-stack=4194304
rlimit-nproc=10
EOF
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 instead
if pgrep -q avahi-daemon; then
service avahi-daemon restart || true
else
service avahi-daemon start || true
fi
sleep 1
if ! pgrep -q avahi-daemon; then
echo "ERROR: avahi-daemon failed to start" >&2
exit 1
fi
echo ""
echo "Done. Avahi is reflecting mDNS on: ${ALLOW_INTERFACES}"
echo "Remember to disable the OPNsense mDNS Repeater under Services -> mDNS Repeater."