420. Avahi and mDNS
This page is for finding things on the local network, and being found, without a DNS server: printers, scanners and file servers that announce themselves, and other machines that reach this one as <hostname>.local. It covers the daemon that does this, its configuration, the commands that browse, resolve and publish, and what to check when nothing is found.
The daemon is Avahi. It speaks multicast DNS (mDNS), which resolves .local names, and DNS service discovery (DNS-SD), which lists the services on the network. On most Linux distributions a program resolves a .local name through the C library’s name-service switch and the nss-mdns plug-in. KDOS’s C library, musl, has no such switch, so on KDOS the lookup goes through DNS instead, by way of kdos-mdnsd; this page explains that path too.
Installation
The avahi port provides the daemon, avahi-daemon, and the command-line tools (avahi-browse, avahi-resolve-host-name, avahi-publish and the rest). Every image carries it. Its postinstall.sh creates the system accounts the daemon and avahi-autoipd drop to:
root="${PKG_ROOT:-/}"
grep -q '^avahi:' "$root/etc/group" 2>/dev/null || groupadd -R "$root" -r avahi
grep -q '^avahi:' "$root/etc/passwd" 2>/dev/null || \
useradd -R "$root" -r -g avahi -d /run/avahi-daemon -s /sbin/nologin avahi
grep -q '^avahi-autoipd:' "$root/etc/group" 2>/dev/null || \
groupadd -R "$root" -r avahi-autoipd
grep -q '^avahi-autoipd:' "$root/etc/passwd" 2>/dev/null || \
useradd -R "$root" -r -g avahi-autoipd -d /var/lib/avahi-autoipd \
-s /sbin/nologin avahi-autoipd
ports/core/net-libs/avahi/postinstall.sh, lines 24–32
It then gives /var/lib/avahi-autoipd, which the package carries, to avahi-autoipd: the directory is that account’s home and the root avahi-autoipd chroots into.
The service script is part of the base filesystem, fs/etc/init.d/45_avahi.sh, so it runs at every boot on the live medium and on an installed system alike; the installer offers no choice for it. It runs after 40_dbus, because the daemon is a system-bus service:
. /etc/init.d/service_helper
NAME="avahi-daemon"
DAEMON="/usr/sbin/avahi-daemon"
case "$1" in
start)
if [ ! -x "$DAEMON" ]; then
echo "[SKIP] $NAME: $DAEMON not found"
exit 0
fi
# After 40_dbus: avahi is a system-bus service and exits immediately
# if it cannot claim org.freedesktop.Avahi.
echo "[KDOS] Starting $NAME..."
mkdir -p /run/avahi-daemon
chown avahi:avahi /run/avahi-daemon 2>/dev/null
# No flag: avahi-daemon stays in the foreground unless given -D, and
# ksvc must watch the daemon itself. -f takes a configuration FILE,
# so a bare -f is a usage error and a respawn loop.
supervise "$NAME" "$DAEMON"
;;
stop) stop_service "$NAME" ;;
status) check_status "$NAME" ;;
*) echo "Usage: $0 {start|stop|status}"; exit 1 ;;
esac
The service is named avahi (from the file name) and its supervised process avahi-daemon (from NAME), which is what service status avahi reports: [ OK ] avahi-daemon (pid <n>). avahi-autoipd and avahi-dnsconfd are installed, and no script starts either.
Avahi is a library as well as a daemon. The ports that discover things through it include cups and cups-browsed (printers), ipp-usb (a USB printer presented as a network one), sane-backends and sane-airscan (scanners), samba (which advertises its shares), gvfs (the GTK file managers’ network view), kio-extras (Dolphin’s network:/ and smb:/ views) and pipewire (network audio sinks). The port’s --enable-compat-libdns_sd also builds Avahi’s libdns_sd, for programs written against Apple’s DNS-SD interface.
Configuration
/etc/avahi/avahi-daemon.conf
The daemon’s configuration is upstream’s file as the avahi package installs it; KDOS changes nothing in it. The keys are described in avahi-daemon.conf(5). After an edit:
sudo service restart avahi
/etc/avahi/services/
Each .service file here is a service the daemon publishes for as long as it runs. Upstream ships two, for SSH and SFTP; the port deletes them, so this machine advertises no SSH server, whether or not one is running:
rm -f "$PKG/etc/avahi/services/ssh.service" "$PKG/etc/avahi/services/sftp-ssh.service"
ports/core/net-libs/avahi/build.sh, lines 52–52
The firewall
mDNS is UDP port 5353, and the shipped /etc/nftables.conf accepts it in its base rules, before any firewall name is turned on. Beside it are the rules for NetBIOS name replies and WS-Discovery answers, which Dolphin’s network browsing asks for alongside its mDNS query:
# mDNS, because avahi is installed and started by
# fs/etc/init.d/45_avahi.sh — a machine that runs a responder
# and firewalls its own port is advertising into a wall.
udp dport 5353 accept
# A NETBIOS NAME QUERY IS A BROADCAST AND ITS ANSWERS ARE NOT
# THE CONNECTION COMING BACK. Dolphin's network browsing, and the
# workgroup-name resolution behind a share mount, send to the
# subnet's broadcast address; every machine answers from its
# OWN, so conntrack holds no entry for any of them and the
# first rule in this chain does not match. Without this a
# browse finds nothing on a network full of file servers.
#
# SCOPED TO THE PORT PAIR A REPLY CARRIES, 137 to 137, rather
# than opening the NetBIOS service: nothing on this image
# listens there, and for the second or so `nmblookup` does, it
# is the thing that asked.
udp sport 137 udp dport 137 accept
# A WS-DISCOVERY PROBE IS MULTICAST, SO ITS ANSWERS ARE NOT THE
# CONNECTION COMING BACK EITHER. Dolphin's smb:/ lists Windows
# machines, which no longer answer NetBIOS, by sending a probe
# to 239.255.255.250 or ff02::c, port 3702; each host answers
# from its own address and port 3702 to the probe's source
# port, so conntrack holds no entry for the answer. Without this
# the network view lists only the hosts NetBIOS finds.
#
# SCOPED TO AN ANSWER FROM THE LOCAL NETWORK INTO THE KERNEL'S
# EPHEMERAL RANGE (net.ipv4.ip_local_port_range): a private or
# link-local source, source port 3702, and the kind of port a
# probe's socket is given. A service listening below 32768 is
# not reachable through it.
ip saddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 169.254.0.0/16 } udp sport 3702 udp dport 32768-60999 accept
ip6 saddr { fc00::/7, fe80::/10 } udp sport 3702 udp dport 32768-60999 accept
fs/etc/nftables.conf, lines 67–100
/etc/nsswitch.conf
On a glibc system, nss-mdns in this file is what lets every program resolve .local names. KDOS’s C library, musl, does not read the file at all, and the file says so:
# musl ignores this file; it exists for glibc tooling inside distrobox
# containers and for host-integration helpers that consult it.
passwd: files
group: files
shadow: files
hosts: files dns
networks: files
protocols: files
services: files
ethers: files
rpc: files
netgroup: files
.local names through DNS
musl sends every host lookup to the resolver /etc/resolv.conf names, 127.0.0.1, which is NetworkManager’s dnsmasq. One line in dnsmasq’s configuration directory forwards the local domain to kdos-mdnsd on UDP port 5380:
# KDOS: the `local` domain is multicast DNS. NetworkManager's dnsmasq, which
# /etc/resolv.conf names, forwards it to kdos-mdnsd (45_mdns.sh), which asks
# avahi-daemon and answers with a ten-second TTL that dnsmasq caches. With
# this line gone, a `.local` name reaches no resolver on musl.
server=/local/127.0.0.1#5380
fs/etc/NetworkManager/dnsmasq.d/kdos-mdns.conf
kdos-mdnsd asks this daemon for each A or AAAA question, over Avahi’s simple-protocol socket /run/avahi-daemon/socket, and answers with the address at a ten-second TTL, or with an empty answer when Avahi has none within three seconds; dnsmasq caches both. It runs as nobody, started by its own service script after this one:
. /etc/init.d/service_helper
NAME="mdns"
DAEMON="/usr/sbin/kdos-mdnsd"
case "$1" in
start)
if [ ! -x "$DAEMON" ]; then
echo "[SKIP] $NAME: $DAEMON not found"
exit 0
fi
# Every answer is avahi-daemon's, so without it the daemon could
# only say SERVFAIL. 45_avahi.sh sorts before this script.
if [ ! -x /usr/sbin/avahi-daemon ]; then
echo "[SKIP] $NAME: avahi-daemon not installed"
exit 0
fi
if [ -e /etc/service.disabled/avahi ]; then
echo "[SKIP] $NAME: avahi is disabled (/etc/service.disabled/avahi)"
exit 0
fi
echo "[KDOS] Starting $NAME..."
# Binds 127.0.0.1:5380 as root, then runs as `nobody`. dnsmasq
# reaches it through /etc/NetworkManager/dnsmasq.d/kdos-mdns.conf.
# --restart-with avahi-daemon: restarted with Avahi, and held while
# Avahi is between two runs.
supervise --restart-with avahi-daemon "$NAME" "$DAEMON"
;;
stop) stop_service "$NAME" ;;
status) check_status "$NAME" ;;
*) echo "Usage: $0 {start|stop|status}"; exit 1 ;;
esac
Its answers, and why a miss is never NXDOMAIN, are in kdos-mdnsd.
Who may change the daemon’s host name
Any user may browse, resolve and publish through the daemon. Its D-Bus policy, /usr/share/dbus-1/system.d/avahi-dbus.conf, keeps SetHostName, which renames the machine on the local network, to root and the members of wheel, the group the avahi port’s --with-avahi-priv-access-group=wheel names. A member of wheel may run avahi-set-host-name <name>; the name lasts until the daemon restarts.
Usage
Seeing what is on the network
List every service on the network, resolved to host and port, and exit when the list is complete:
avahi-browse -artNarrow it to one service type, for example printers or SMB file servers:
avahi-browse -rt _ipp._tcp avahi-browse -rt _smb._tcp
Resolving a .local name
Ask Avahi for the address:
avahi-resolve-host-name -4 printer.localIt prints the name and one address, separated by a tab. For a machine with several interfaces the address may be one the asker cannot reach: asked for its own name, a KDOS machine with libvirt’s
defaultnetwork started answers with192.168.122.1, the address of itsvirbr0bridge.Or give the name to any program:
ping printer.local,ssh kdos@pi.localandhttp://printer.localin a browser resolve through the C library, dnsmasq andkdos-mdnsd(see .local names through DNS). Check the path with:getent hosts printer.local service status mdnsEvery program that connects through libcups —
cupsd,lpadmin,lpstat, cups-browsed and System Settings’ Printers page — also asks Avahi itself when the C library cannot answer, because of the cups port’slocal-names-through-avahi.patch, so a printer is added by its.localname on a machine without NetworkManager too. There, for anything else, use the addressavahi-resolve-host-nameprints, or put it in/etc/hosts.
Publishing a service
For as long as a command runs,
avahi-publishadvertises a service, here a web server on port 8080:avahi-publish -s "My web page" _http._tcp 8080For a permanent one, write a file in
/etc/avahi/services/. This advertises the SSH server that the port leaves unadvertised:<?xml version="1.0" standalone="no"?> <!DOCTYPE service-group SYSTEM "avahi-service.dtd"> <service-group> <name replace-wildcards="yes">%h</name> <service> <type>_ssh._tcp</type> <port>22</port> </service> </service-group>Save it as
/etc/avahi/services/ssh.service, then runsudo service restart avahi.Advertising a service does not open the firewall for it. Open the service’s firewall name too; see Opening a service.
Turning it off
sudo service disable avahi
sudo service stop avahi
Printers and scanners on the network are then not discovered, Dolphin’s network view finds only the machines that answer NetBIOS or WS-Discovery, 45_mdns is skipped at the next boot, and no program resolves a .local name. UDP 5353 stays accepted by the base firewall rules until you edit /etc/nftables.conf.
Tips
- The name other machines find this one by is the hostname set at boot from
/etc/hostname, followed by.local.hostnameprints it, andpgrep -a avahishows the name the daemon holds in its process title, as inavahi-daemon: running [kdos.local]. - The daemon announces on every interface with an address, among them
loand, while libvirt’s network is started, itsvirbr0bridge, soavahi-browse -rtlists a service of this machine once per interface. avahi-browse -pprints one record per line, separated by semicolons, for scripts;-kshows service types by their DNS names rather than translated ones.- Dolphin’s
smb:/lists file servers fromavahi-browse’s_smb._tcprecords, a NetBIOS query and a WS-Discovery probe together.
Troubleshooting
avahi-browse says Failed to create client object: Daemon not running. Check the service:
service status avahi
cat /run/kdos-init.avahi.log
[DOWN] avahi-daemon with no log at all is a service turned off with service disable, which the boot skips; service list shows it as [DISABLED], and sudo service enable avahi followed by sudo service start avahi brings it back.
The daemon is restarted every five seconds. service status avahi then reports [FAIL] avahi-daemon with the daemon’s exit code and the number of failed starts in a row. Read the daemon’s own messages in the supervisor’s log, which only root can read:
sudo tail -n 40 /run/kdos-svc.avahi-daemon.log
The daemon exits at once when it cannot claim org.freedesktop.Avahi on the system bus, so check service status dbus first.
Another machine finds this one as <hostname>-2.local. Another machine on the network already has that name. The log above records the conflict; give one of the two machines a different hostname.
A printer or file server is missing from every list. mDNS does not cross a router, so a machine on another subnet is never listed; reach it by its address. A machine that is asleep, or whose own firewall drops UDP 5353, does not answer either.
ping name.local fails. It prints ping: name.local: Name does not resolve, and getent hosts name.local prints nothing after about three seconds. Check each step of the path:
avahi-resolve-host-name -4 name.localanswers: if it does not, the machine is not on this network or not announcing, and nothing below can help.service status mdnsreports[ OK ] mdns; its log is/run/kdos-svc.mdns.log.drill -p 5380 name.local @127.0.0.1askskdos-mdnsddirectly, anddrill name.localasks dnsmasq. When the first answers and the second does not, dnsmasq has not read/etc/NetworkManager/dnsmasq.d/kdos-mdns.conf: NetworkManager is not running withdns=dnsmasq, or the network is run by30_network’s dhcpcd, whose resolver knows nothing of.local.
A host whose only IPv6 address is link-local resolves to its IPv4 address alone, because a DNS record cannot say which interface a link-local address belongs to.
Known gaps
- The list of Windows shares on the network is whatever answered a broadcast: a Windows machine announces itself with WS-Discovery, not mDNS. Dolphin’s
smb:/sends a WS-Discovery probe of its own, but GTK programs’network://view lists only what Avahi finds, and a machine that is asleep or on another subnet is missing from both .localnames resolve only through NetworkManager’s dnsmasq: on the dhcpcd fallbackping,sshand the rest take an address
See also
- Services:
45_avahiand45_mdnsin the boot order - kdos-mdnsd: the DNS bridge to this daemon
- The firewall: the base rules, which accept UDP 5353
- File sharing, Printing and scanning, Firewall, SSH server, Services