mDNS on Raspberry Pi
Setting up mDNS (ZeroConf)
If you have a single Raspberry Pi (rPi) on your network, you can connect to it via its IP address (192.168.1.128 for example) or via a local DNS name of raspberrypi.local
ssh pi@192.168.1.128
ssh pi@raspberrypi.local
If you have multiple rPi on the network, you can either have to remember several IP addresses, or you can use the magic of mDNS (part of a suite of network technologies called ZeroConf) to give them all useful, memorable names. You’ll need to set the wanted name of the Pi in two files (the content of my files are shown), basically replace any occurrence of ‘raspberrypi’ with the name you want. I’m using weather here, because this rPi will be used to collect and store weather sensor data.
1. /etc/hostname
pi@weather:~ $ cat /etc/hostname
weather
2. /etc/hosts
pi@weather:~ $ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1 weather
The names can’t include spaces, odd characters or fullstops.
When you’ve got the files set correctly, you need to check if you have the mDNS server installed
pi@weather:~ $ dpkg -l |grep avahi
ii avahi-daemon 0.8-5+deb11u1 arm64 Avahi mDNS/DNS-SD daemon
ii libavahi-client3:arm64 0.8-5+deb11u1 arm64 Avahi client library
ii libavahi-common-data:arm64 0.8-5+deb11u1 arm64 Avahi common data files
ii libavahi-common3:arm64 0.8-5+deb11u1 arm64 Avahi common library
ii libavahi-core7:arm64 0.8-5+deb11u1 arm64 Avahi's embeddable mDNS/DNS-SD library
ii libavahi-glib1:arm64 0.8-5+deb11u1 arm64 Avahi GLib integration library
If you see something like the above, then you’ve got the software installed. If you see nothing printed out, then you can install the software with
pi@weather:~ $ sudo apt install avahi-daemon
Either way, once you’ve got the software installed, you can start it with
pi@weather:~ $ sudo systemctl status avahi-daemon.service
Loaded: loaded (/lib/systemd/system/avahi-daemon.service; enabled; vendor preset: enabled)
Active: active (running)
It’s probably a good idea to reboot the rPi at this point, it should work without a reboot, but starting from a clean slate is a good idea.
Once the rPi has booted, you should be able to access it on the network as (in my case)
ssh pi@weather.local