Configure mDNS for Hostname Resolution on Ubuntu

avahi-daemon

Enable your Ubuntu servers to be accessed via hostname.local (e.g., ping yourservername.local or ssh username@yourservername.local) using Avahi for Zero Configuration Networking (Zeroconf). This is perfect for local networks, like my setup with pi5server, pi4server, and pi3server.

Install Avahi

Install the mDNS daemon and tools:

sudo apt update
sudo apt install avahi-daemon avahi-utils libnss-mdns

Verify Avahi Service

Ensure Avahi is running:

sudo systemctl enable avahi-daemon
sudo systemctl start avahi-daemon
sudo systemctl status avahi-daemon

Look for active (running) in the output.

Configure Hostname

Set a unique hostname for each server:

sudo hostnamectl set-hostname yourservername  # Replace with your hostname

Update /etc/hosts to include the hostname:

sudo nano /etc/hosts

Add or verify:

127.0.0.1 localhost
127.0.1.1 yourservername  # Replace with your hostname

Save and exit (Ctrl+O, Enter, Ctrl+X).

Allow SSH discovery via mDNS:

sudo nano /etc/avahi/services/ssh.service

Add:

<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
    <name replace-wildcards="yes">%h</name>
    <service>
        <type>_ssh._tcp</type>
        <port>22</port>
    </service>
</service-group>

Restart Avahi:

sudo systemctl restart avahi-daemon

Configure Client for mDNS

On the client device accessing the server:

sudo apt install libnss-mdns

Edit /etc/nsswitch.conf:

sudo nano /etc/nsswitch.conf

Ensure the hosts line includes:

hosts: files mdns4_minimal [NOTFOUND=return] resolve [!UNAVAIL=return] dns

Test mDNS Resolution

From a client on the same network (e.g., 192.168.x.x):

ping yourservername.local
ssh <username@yourservername.local>

Verify services:

avahi-browse -a

Look for entries like:

+ eth0 IPv4 pi5server [hostname] _ssh._tcp local

Troubleshooting

sudo ufw allow 5353/udp
journalctl -u avahi-daemon

🧠 Notes

Ensure SSH is enabled (sudo systemctl enable ssh; sudo systemctl start ssh).