Today, you probably do a lot of this:
At some point you’ll have a pile of Raspberry Pis around your house and you’ll have to start reserving IP addresses in your router to reliably get to them. That, or you’ll have to continuously look them up in the “attached” devices section and try to make a guess as to which you want to play with. Terribly time consuming and no fun. Enter mDNS.
Give each PI their own name!
With mDNS (a.k.a ZeroConf) you’ll be able to do things like this:
(Why wand receiver? Check out: Raspberry Pi – Control by Magic Wand!)
I gave that pi the name wandreceiver. Which makes it easy for me to recollect and ssh into the pi I want to play with today.
mDNS stands for Multicast DNS, or Multicast Domain Name Service. Its designed for small networks to have a way of discovering device IPs. It works by allowing each device to broadcast its name and IP through to every other device on the network. Obviously this wouldn’t be great for the internet at large, but local networks are small enough that the overhead isn’t a problem.
Files to change…
Lets get started. Once more, find the IP address of your raspberry pi on the router. If you use a Netgear router, you’ll likely need to look up something called “Attached Devices” which will show you all devices on your network, find the one you wish to name, and capture its IP.
SSH in using that ip. You know, the old way:
Where 192.168.1.108 should be replaced with your PI’s IP address.
There are two files you’ll need to modify with sudo access:
They both have an entry that contains the hostname. Edit those files and enter the name you’d like to use. Make sure not to include spaces, or odd characters. Only use names, that are possible on a domain name.
Edit the hosts file, and look for the entry labeled(yours might already have a different name):
If you’ve already got a better name in there… Congrats! You probably took advantage of a feature in PiBakery that allows you to update the name. If you did, don’t change anything unless you’ve changed your mind on what it should be called.
Change the raspberrypi part – to the name you’d rather have, in my case its wandreceiver. Now remember that name, you’ll need a copy of it in the /etc/hostname file.
Next edit the the hostname file with sudo permissions. There is only one line in this file. Its the former name of your host. Replace it with the one you placed in the /etc/hosts file.
Getting Avahi… the mDNS service
For me Avahi was already installed. To check, run this:
sudo systemctl status avahi-daemon.service
If you get back a response that starts with:
● avahi-daemon.service - Avahi mDNS/DNS-SD Stack Loaded: loaded (/lib/systemd/system/avahi-daemon.service; enabled) Active: active (running) since Sun 2017-01-08 05:36:05 UTC; 10h ago
You’ve got it, and a simple reboot will change everything over. If not, you have a tiny bit more work. Just, install the Avahi Daemon:
sudo apt-get install avahi-daemon
Reboot your computer and poof! A new domain will resolve on your mDNS friendly computers, and you can now use the name you provided followed by .local like this:
The service normally “just works”, but there are a few things to keep in mind.
Namely, for your computer to know which IP address is should be addressing – it first has to “receive” that entry. Since the broadcasts are happening a lower intervals, wait a few seconds if at first your ping or ssh fails.
Secondarily – your computer might not know how to use mDNS. Macs know by default. Some windows boxes don’t have the “Bonjour” services installed. There is a good article here to help with that:
Search for “Get Windows to play nice with avahi” on that page.
2 thoughts on “Name your PIs with mDNS – forget the IPs with ZeroConf”