How to use mDNS to access a device without knowing the IP address

From RidgeRun Developer Wiki

Overview

This wiki explains how to access a board connected to a LAN without explicitly knowing its IP adress. There are several techniques to do this:

  1. Assigning a fixed IP address to your device from the DHCP server in your switch.
  2. Having a local name server in your network.
  3. Having your device send mDNS messages.

This wiki focuses on 3. since it's the simplest of the three. Specifically, this wiki shows how to enable mDNS in Linux devices using Avahi.

What is mDNS?

Multicast DNS or mDNS is a protocol that helps resolve hostnames on local networks that do not have a name server. The protocol specifies the resolution of hostnames ending in .local. Simply put, mDNS allows you to perform a hostname/IP mapping. For example the hostname nano.local maps to a device with address 192.168.0.3:

$ ping nano.local
PING nano.local (192.168.0.3): 56 data bytes
64 bytes from 192.168.0.3: icmp_seq=0 ttl=64 time=1.257 ms
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.992 ms

To do so (and without going into the full protocol specification), when a mDNS client (your PC) wants to resolve a .local hostname it sends a multicast query message. The device which identifies with this hostname then multicasts back its IP address. This address is stored in the mDNS caches of the different devices in the network.

What is Avahi?

Avahi is an open source implementation of the mDNS and the DNS-SD protocols. See more in their website.

Enabling mDNS in my device

Start by installing the Avahi Daemon on the device:

$ sudo apt install avahi-daemon
$ sudo systemctl restart avahi-daemon

Now that you have Avahi on the device, identify its hostname by running (again on the device):

$ hostnamectl
   Static hostname: jetson-nano
         Icon name: computer
        Machine ID: a3d9197b765643568af09eb2bd3e5ce7
           Boot ID: e8dd6897fedd4e5cbbe255ecd86a5d47
  Operating System: Ubuntu 18.04.5 LTS
            Kernel: Linux 4.9.140-tegra
      Architecture: arm64

In this case, the hostname is jetson-nano (without the .local suffix). If you wish to change the host name, you may do so by running the following:

$ sudo hostnamectl set-hostname nano
$ hostnamectl
   Static hostname: nano
         Icon name: computer
        Machine ID: a3d9197b765643568af09eb2bd3e5ce7
           Boot ID: e8dd6897fedd4e5cbbe255ecd86a5d47
  Operating System: Ubuntu 18.04.5 LTS
            Kernel: Linux 4.9.140-tegra
      Architecture: arm64

$ sudo systemctl restart avahi-daemon

In line 1 we change the hostname to nano, in line 3 we verify that the name has been successfully changed and in line 11 we restart the mDNS server so that the change takes effect.

Finally verify that you can ping your device from your PC:

$ ping nano.local
PING nano.local (192.168.0.3): 56 data bytes
64 bytes from 192.168.0.3: icmp_seq=0 ttl=64 time=0.638 ms
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=1.187 ms


For direct inquiries, please refer to the contact information available on our Contact page. Alternatively, you may complete and submit the form provided at the same link. We will respond to your request at our earliest opportunity.


Links to RidgeRun Resources and RidgeRun Artificial Intelligence Solutions can be found in the footer below.