SkillPilotAI

dscacheutil, dns-sd, mDNSResponder + avahi-browse, avahi-resolve

On macOS every app resolves names through the mDNSResponder daemon, which handles both normal DNS and Bonjour (multicast DNS). dscacheutil and dns-sd let you query, flush and browse that resolver; on Linux, Avahi does the same Bonjour job with avahi-browse and avahi-resolve.

91.1 What these tools do

ToolOSJob
mDNSRespondermacOS (built in)The DNS + Bonjour daemon; every app's name lookup goes through it
dscacheutilmacOS (built in)Query and flush the resolver cache
dns-sdmacOS (built in)Bonjour browse, resolve and advertise
avahi-browse, avahi-resolveLinuxBonjour browse/resolve; need avahi-utils + a running avahi-daemon
  • Safari opening example.com does not send DNS itself: it asks mDNSResponder, which sends a unicast query (router -> internet -> 8.8.8.8) and caches the answer.
  • A name ending in .local (OfficePrinter.local) is sent as multicast on the LAN and the device replies with its own IP.
  • dig and nslookup bypass mDNSResponder and its cache, so the app and dig can give different answers.

91.2 How mDNS / DNS-SD discovery works

  • DNS-SD (service discovery) runs on top of mDNS: multicast address 224.0.0.251, UDP port 5353.
  • The client asks a PTR question for a service type, e.g. PTR? _ipp._tcp.local (printers).
  • Devices that do not offer that service (the Raspberry Pi) stay silent; the printer answers by itself, no DNS server needed.
  • PTR gives the instance name, SRV gives host + port (631), TXT gives extra info (ty=HP LaserJet), A/AAAA gives the IP.
  • In dns-sd these steps are -B (browse), -L (resolve/lookup) and -G (get address).
Bonjour: the printer answers by itself
  1. 1.Laptop 192.168.111.5 sends PTR? _ipp._tcp.local to 224.0.0.251 UDP 5353

  2. 2.The LAN switch floods the multicast to every device

  3. 3.raspberrypi (.20, only _ssh._tcp) stays silent

  4. 4.Office Printer (.40) replies: PTR + SRV OfficePrinter.local:631 + TXT + A

  5. 5.Laptop now knows Office Printer = 192.168.111.40:631

dns-sd -B / -L / -G or avahi-browse -r do these same steps

91.3 dscacheutil: flags and query categories

Output
-h                  list the options-q host -a name X   query one entry-q user             all entries of a category-cachedump          cache overview-cachedump -buckets       + hash buckets-cachedump -entries host  + entries (sudo)-configuration      search policy, cache-flushcache         flush all cache (sudo)-statistics         hit / miss statistics
  • -q CATEGORY -a KEY VALUE does a lookup through the cache exactly the way apps do.
  • -q host -a name example.com returns IPv4 and IPv6; -a ip_address 8.8.8.8 is a reverse lookup.
  • Other categories follow the same pattern: group (name/gid), user (name/uid), service (name/port), protocol, rpc, mount.
Output
-q host -a name example.com    name to IP-q host -a ip_address 8.8.8.8  reverse-q group -a name staff         name or gid-q user -a uid 501             name or uid-q service -a port 443         name or port-q protocol -a name tcp        name or number-q rpc -a number 100000        name or number-q mount -a name /Volumes/Data mount entry

91.4 dns-sd: Bonjour operations

  • dns-sd calls keep running until you press Ctrl+C.
  • -B browses instances of a service type; -B _services._dns-sd._udp lists every service type on the LAN.
  • -R advertises your own service; -P advertises a proxy record for another device.
  • -q queries any DNS record through the daemon; -Z prints zone-file format; -E / -F show domains; -V shows the version.
Output
-E                    domains to register-F                    domains to browse-R name type . port   service advertise-P name type "" port host IP   proxy-B _http._tcp         instances browse-B _services._dns-sd._udp   all types-L name type local    host, port, TXT-q www.apple.com      any DNS name-q example.com MX IN  record type + class-Z _http._tcp local   zone-file format-G v4 example.com     IPv4 address-G v6 example.com     IPv6 address-G v4v6 example.com   IPv4 + IPv6-V                    daemon version
Terminal
dns-sd -B _ipp._tcpdns-sd -L "Office Printer" _ipp._tcp localdns-sd -G v4 OfficePrinter.local
Output
$ dns-sd -B _ipp._tcpTimestamp   A/R if Domain Service Type Instance Name10:02:11.3  Add 6  local. _ipp._tcp.  Office Printer
  • This is the documented example output from the macOS docs (the lab has no Mac).
  • A/R: Add = new instance found, Rmv = instance left the network.
  • if 6 = interface index of en0; the last column is the instance name.

91.5 mDNSResponder: daemon control and logging

  • mDNSResponder listens on UDP 5353 but has no CLI: you control it with signals (all need sudo).
  • USR1 toggles extra logging, USR2 toggles packet logging, INFO dumps a state snapshot to the log.
  • Old macOS: syslog -c; modern macOS: log stream --predicate for live logs.
  • plist settings (AlwaysAppendSearchDomains, NoMulticastAdvertisements) apply after a reboot.
  • The most used command is the DNS flush: dscacheutil -flushcache + killall -HUP mDNSResponder.
Output
# all of these need sudokillall -USR1 mDNSResponder  extra loggingkillall -USR2 mDNSResponder  packet loggingkillall -INFO mDNSResponder  state snapshotsyslog -c mDNSResponder -d   all log levelslog stream --predicate \  'process == "mDNSResponder"'  live logsdefaults write /Library/Preferences/\  com.apple.mDNSResponder.plist \  AlwaysAppendSearchDomains -bool true  NoMulticastAdvertisements -bool truedscacheutil -flushcachekillall -HUP mDNSResponder   DNS flush

91.6 avahi-browse and avahi-resolve (Linux)

OptionMeaning
_http._tcp / -aBrowse one service type / all types
-tDump the current list and exit
-cShow only the cache, then exit
-lIgnore local services (on this machine)
-rResolve each service: host, IP, port, TXT
-p / -kParsable (semicolon-separated) / raw type names, for scripts
-D, -d, -v, -f, -bBrowse domains, domain, verbose, wait for daemon, dump type database
avahi-resolve -n / -aName to IP / IP to name (short forms avahi-resolve-host-name / -address)
avahi-resolve -4 / -6Only IPv4 / only IPv6 answers

91.7 Practical: discovering the printer + mDNS packets

  • Isolated Docker lab n091-lan: laptop 192.168.111.5, printer 192.168.111.40 (_ipp._tcp 631), raspberrypi 192.168.111.20 (_ssh._tcp 22).
  • In avahi-browse output, + means a service was found and = means it was resolved.
Terminal
avahi-browse -rt _ipp._tcptcpdump -ni eth0 -c 8 udp port 5353
Output
$ avahi-browse -rt _ipp._tcp+   eth0 IPv4 Office Printer                                Internet Printer     local=   eth0 IPv4 Office Printer                                Internet Printer     local   hostname = [OfficePrinter.local]   address = [192.168.111.40]   port = [631]   txt = ["rp=ipp/print" "ty=HP LaserJet"]$ tcpdump -ni eth0 -c 8 udp port 535322:52:18.300958 IP 192.168.111.5.5353 > 224.0.0.251.5353: 0 PTR (QM)? _ipp._tcp.local. (33)22:52:18.301158 IP 192.168.111.5.5353 > 224.0.0.251.5353: 0 PTR (QM)? _ipp._tcp.local. (33)22:52:18.363709 IP 192.168.111.40.5353 > 224.0.0.251.5353: 0*- [0q] 4/0/0 PTR Office Printer._ipp._tcp.local., (Cache flush) TXT "ty=HP LaserJet" "rp=ipp/print", (Cache flush) SRV OfficePrinter.local.:631 0 0, (Cache flush) A 192.168.111.40 (146)3 packets captured
  • A Test Web service was also published on the laptop itself; -l hides it, leaving only the printer and the Pi.
  • -aptk | cut keeps name and type; a space in a name is shown as \032.
Terminal
avahi-browse -a -t -lavahi-browse -aptk | cut -d';' -f4,5
Output
$ avahi-browse -a -t -l+   eth0 IPv4 raspberrypi                                   SSH Remote Terminal  local+   eth0 IPv4 Office Printer                                Internet Printer     local$ avahi-browse -aptk | cut -d';' -f4,5raspberrypi;_ssh._tcpOffice\032Printer;_ipp._tcpTest\032Web;_http._tcpTest\032Web;_http._tcp
Terminal
avahi-resolve -n raspberrypi.localavahi-resolve -a 192.168.111.40avahi-resolve -n nobody.localpkill avahi-daemonavahi-browse -a -t
Output
$ avahi-resolve -n raspberrypi.localraspberrypi.local	192.168.111.20$ avahi-resolve -a 192.168.111.40192.168.111.40	OfficePrinter.local$ avahi-resolve -n nobody.localFailed to resolve host name 'nobody.local': Timeout reached$ pkill avahi-daemon$ avahi-browse -a -tFailed to create client object: Daemon not running
  • A name nobody on the LAN owns gets no reply, so the result is a timeout, not NXDOMAIN.
  • Avahi tools send no packets themselves: they ask avahi-daemon over D-Bus, so without the daemon they fail.

91.8 Common mistakes

  • Thinking Bonjour searches the whole office: the router does not forward multicast 224.0.0.251, so a printer on another subnet never shows up.
  • Fix for other subnets: an mDNS reflector or unicast DNS-SD; the firewall must also allow UDP 5353.
  • Testing a Mac with dig only: dig skips the mDNSResponder cache that Safari uses.
  • Running avahi-browse without avahi-daemon: 'Daemon not running'.
Why a printer on another subnet is not found
  1. 1.Laptop 192.168.1.10 sends an mDNS query

  2. 2.Switch (LAN 1) carries it to every device in LAN 1

  3. 3.Router: multicast 224.0.0.251 is not forwarded

  4. 4.Printer 192.168.2.40 in LAN 2 never sees the query

  5. 5.Fix: mDNS reflector or unicast DNS-SD + allow UDP 5353

91.9 Real-world example

  1. A server's IP changed in the company DNS; dig on the Mac shows the new IP but Safari still opens the old one.
  2. Reason: dig bypasses the mDNSResponder cache, Safari uses it.
  3. Fix: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder.

91.10 Interview tips

  • Printer discovery: the device announces with mDNS on 224.0.0.251, UDP 5353; clients browse instances with a PTR query.
  • Then SRV gives host + port, TXT gives details, A/AAAA gives the IP.
  • macOS: mDNSResponder, tested with dns-sd -B/-L/-G; Linux: avahi-daemon, tested with avahi-browse -rt.
  • mDNS is link-local: same LAN only, unless you add a reflector.
  • Mac DNS flush = dscacheutil -flushcache + killall -HUP mDNSResponder.