How to setup mDNS lookups on the Raspberry Pi

I’ve got the new Raspberry Pi 2, and was setting it up the other day. The first thing that annoys me with vanilla Debian installations is that they don’t have mDNS/zeroconf/avahi enabled by default. This technology is very useful, since it helps you to advertise services on you local network, lets you resolve host names without the need for setting up a DNS server and much more.

Especially the convenience of not having to remember IP-addresses for your machines is worth the work to set this up. With DHCP you might not even get the same IP for every machine every time.

For this to work, I asked a question over at the Linux & Unix StackExchange. So parts of this blog entry are taken from there.

First, you might want to install avahi on your RasPi;

sudo apt-get install avahi-daemon

This should help with the Pi being resolvable by name from other machines — which also have to support mDNS. For example Macs will come with mDNS-lookup enabled. So you should be able to ping your Pi just by using its name plus the local-domain:

ping raspberrypi.local

Next, you want to install the client side name service support for mDNS:

sudo apt-get install libnss-mdns

Make sure that the /etc/nsswitch.conf contains this line:

hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4

There is probably already a line starting with “hosts:”, which you can simple comment out with the #-sign.

For added convenience, you may want to add the sshd to the advertised services of avahi. Simply add a file /etc/avahi/services/ssh.service containing the following lines:


<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
<!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>

This should let you use mDNS on you Pi, see the advertised services on you other machines in the local network. If you are using Plex Media Server (see this great post), it will also utilize avahi to advertise its services.