raspberry pi

Name your PIs with mDNS – forget the IPs with ZeroConf

piwhere

Today, you probably do a lot of this:

ssh pi@192.168.1.108

Or even:

ssh pi@raspberrypi.local

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:

ssh pi@wandreceiver.local

(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:

ssh pi@192.168.1.108

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:

/etc/hosts
/etc/hostname

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):

 127.0.1.1          raspberrypi

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:

ssh pi@wandreceiver.local

Troubleshooting…

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:

http://elinux.org/RPi_Advanced_Setup#Setting_up_for_remote_access_.2F_headless_operation

Search for Get Windows to play nice with avahi” on that page.

Advertisements

2 thoughts on “Name your PIs with mDNS – forget the IPs with ZeroConf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s