Multicast DNS is part of Zero-configuration networking (zeroconf) set of technologies designed to enable devices to work on networks without manual setup.
Multicast DNS is used to locate a device or service by name on a small local network without using a preconfigured name sever i.e DNS.
Originally developed by Apple it goes under the name of Bonjour. It is an Internet standard Multicast DNS RFC 6762.
Multicast DNS uses the same packet structure and commands as DNS, but doesn’t rely on a user configured DNS server.
Instead computers on a network create their own local DNS records and store them locally in cache. (computer memory).
In this tutorial you will learn how mDNS works and how to perform mDNS queries using the avahi utility tools and the dns-sd tool from the Bonjour sdk.
- How mDNS works
- .local Domain name
- mDNS records
- mDNS Announcements
- Service Discovery Using mDNS
- Finding Hosts Using mDNS
- Common Questions
mDNs on Linux and Windows
On Linux including the Raspberry Pi it is usually installed automatically and uses Avahi packages .
You can also download the utilities which can be useful for troubleshooting using:
sudo apt-get avahi-utils.
On windows and Apple the mDNS services are provided by the Bonjour packages.
You can download the Bonjour SDK here which will allow you to use the dns-sd command line tool.
You will need to create a developer account to download the SDK.
How mDNS Works
It is useful to compare mDNS with the traditional DNS service especially if you are familiar with DNS.
When a host requires to know the IP address of another host is send a DNS query to a DNS server using a unicast UDP message on port 53.
The address of this DNS server is part of the network configuration of the host.
With mDNS no mDNS server is preconfigured, and the host uses a multicast query on IPv4 address 220.127.116.11 or IPv6 address ff02::fb and UDP port 5353.
All mDNS hosts see this query and the host that has this network name responds to the query using a multicast message that contains it’s IP address.
Because messages were exchanged using multicast all other mDNS hosts see this exchange and can make a note of the network name and IP address, and store this in their local cache.
All mDNS hosts now know the name and IP address of the requested host.
So the process is:
- Host A sends query for IP address of Host Z.
- All mDNS hosts see this query.
- Host Z responds with it’s IP address
- All mDNS hosts see this response.
- All mDNS hosts update their local cache with the name host Z and it’s IP address.
The .local Domain Name
All DNS entries have a domain name and for devices and services on local networks that aren’t part of the global DNS namespace the .local domain name was reserved.
This means that all hosts on a local network will have a name of the form.
The mDNS resolver on the client registers the host with the .local prefix.
So when you do a ping on a local network you would use:
for a computer called ws6 on your local network. The local name resolver will automatically choose mDNS as a first choice to resolve the name because of the presence of the .local suffix.
mDNS supports both host name records (A and AAAA) record types and SRV records as in standard DNS.
Because of this mDNS can be used for locating hosts and services on a local network.
The service record structure can appear confusing at first glance for any one new to DNS service records.
The structure (taken fro the wiki) is shown below along with an example record:
_service._proto.name. TTL class SRV priority weight port target. _sip._tcp.example.com. 86400 IN SRV 0 5 5060 sipserver.example.com.
the service is _sip
the proto name is tcp //protocol service uses
the name is example.com. // domain name for which it is valid.
You should notice the use of the underscores in the structure.
At the end of the record we have
The port is the port the service runs on (5060) and the target is the host that runs this service ( sipserver.example.com. )
As part of the mDNS protocol mDNS devices will make announcements containing their mDNS records on start and in response to network changes on the host machine.
These announcements will be received by all mDNS clients on the local network and used to update their own records.
Service Discovery Using mDNS
mDNS can be used to discover services like MQTT on your local network.
An mDNS client issues a query for that service as shown by using the avahi-browse utility below:
You can see that this service is available on machine called pi2 and on both IPv4 and IPv6.
On Windows use the dns-sd -B service name command
Finding Hosts Using mDNS
This can be demonstrated using the avahi-resolve utility as shown below
On Windows use the dns-sd -Q hostname command
Common Questions and Answers
Q- Can I still use a local domain name server like DNSMasq if my clients use mDNS.
A- Yes they co-exist on the same network.
Q- Can you use mDNS across VLANs?
A- Not without extra configuration.
Q- Can I use a host announce services that are available on other machines on the network?
A- Yes on Linux you will need to create and add a avahi services file. See Broadcast mDNS for another Device.
- Name Resolution and DNS on Home Networks
- Create a Home Network DNS Server Using DNSMasq
- Home Network Addressing
- Dynamic DNS (DDNS)? -How it Works and Why Use It?