Resolve Hostname with mDNS in Ubuntu 22.04 to Access Local Network Devices
In a local network, it is often useful to be able to identify devices by their hostnames instead of their IP addresses. This is especially true when setting up services that need to communicate with other devices on the network. One way to achieve this is by using Multicast DNS (mDNS), a protocol that allows devices to discover and communicate with each other using their hostnames, even if they are on different subnets.
In Ubuntu 22.04, mDNS is implemented through the Avahi daemon, which provides mDNS and DNS-SD (Domain Name System-Service Discovery) services. With Avahi, it is possible to resolve hostnames of devices on the local network and publish services running on the local machine.
Prerequisites
To follow this guide, you will need:
- A machine running Ubuntu 22.04.
- Access to the terminal.
Checking if mDNS is Already Installed
Before installing mDNS, you can check if it is already installed on your machine by running the following command:
avahi-daemon --version
If mDNS is installed, you should see an output similar to the following:
avahi-daemon 0.8-5 (Cyrillic)
Installing mDNS
If mDNS is not installed on your machine, you can install it by running the following command:
sudo apt-get install avahi-daemon
Enabling mDNS
Once mDNS is installed, you can start and enable the Avahi daemon by running the following commands:
sudo systemctl start avahi-daemon
sudo systemctl enable avahi-daemon
Testing mDNS
To test if mDNS is working on your machine, you can run the following command:
avahi-browse --all
This will display a list of all mDNS-enabled devices on the local network, along with the services they are providing. You should see your machine listed in the output, along with any other devices that have mDNS enabled.
Resolving Hostnames with mDNS
With mDNS enabled, you can resolve the hostname of your machine by running the following command:
avahi-resolve -n <hostname>
Replace <hostname> with the hostname of your machine. The output will display the IP address of the machine.
You can also use the "ping" command to check if you can reach your machine using its hostname:
ping <hostname>
Replace <hostname> with the hostname of your machine. You should see a response from your machine, indicating that it is reachable using its hostname.Publishing Services with mDNS
With mDNS, you can also publish services running on your machine, so that other devices on the local network can discover and connect to them. For example, if you have a web server running on your machine, you can publish it using mDNS, so that other devices can access it using the hostname of your machine.
To publish a service with mDNS, you can use the Avahi service file, which describes the service. A typical Avahi service file looks like this:
_http._tcp
80
My Web Server
local
```
This file describes an HTTP service running on port 80, with the name "My Web Server" and the domain "local". You can save this file as "mywebserver.service" in the "/etc/avahi/services" directory.
- In Ubuntu 22.04, mDNS can be used to resolve hostnames of devices on the local network and publish services running on the local machine.
- mDNS is implemented through the Avahi daemon, which can be installed, started, and enabled using the terminal.
- Once mDNS is enabled, you can resolve hostnames using the "avahi-resolve" command and test mDNS using the "avahi-browse" command.
- To publish services with mDNS, you can create an Avahi service file, which describes the service and saves it in the "/etc/avahi/services" directory.