16

Often (maybe 80% of the time), when I reboot a machine running Avahi, it starts using machinename-2.local as it's hostname. All the machines on my network have unique hostnames, so there is no collision.

How do I prevent Avahi from appending -2 to hostnames?

(This turns out to be an incredibly difficult thing to Google for...)

dty
  • 313

6 Answers6

3

I have the same problem on a Raspberry Pi. Until a better solution comes along, Ive added a crontab entry to run this script every day.

#!/bin/bash

writelog() { echo $(date) >> /home/pi/avahirestart.log } aname=$(avahi-resolve -a $(hostname -I|cut -d' ' -f1)|cut -f2) if [[ "$aname" = "-2" ]];then sudo service avahi-daemon restart writelog fi if [[ "$aname" = "-3" ]];then sudo service avahi-daemon restart writelog fi

Ken H
  • 131
  • 4
3

This is also the case with OS X machines.

If the hostname is already in use on the network it bumps the counter to make the names unique again. I would suspect that the machine in question may have two netcards to the same network (cabled and wireless) or that you have a bonjour proxy running (which may be unknown to you) which helps sleeping machines by holding their hostname active. To be more certain you may want to describe your network better.

2

In my case, this behavior is caused by a Plex Media Server inside a docker container which runs its own avahi-daemon. I'm using this docker image: https://hub.docker.com/r/linuxserver/plex/

The number was increased very often, without need to restart.

After stopping the docker container and a restart of avahi-daemon.service, the numbers disappeared.

1

Here's an event-based dbus listener that looks for avahi hostname changes, and tries to remove the -2 suffix. It runs on startup as a systemd service (the gist includes an example service unitfile). I'm running it on a Debian server, although any distro that has dbus should be fine.

The benefit with this approach is that the script doesn't rely on polling, so when the hostname changes, it will quickly be changed back.

import dbus, avahi
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
import time

class Runner(): def init(self, avahi_server): self.avahi_server = avahi_server

def on_state_change(self, state, error):
    if state == avahi.SERVER_COLLISION:
        print(f"[state: collision] collided, waiting 5s...")
        time.sleep(5)

    elif state == avahi.SERVER_RUNNING:
        hostname = self.avahi_server.GetHostName()
        print(f"[state: running] hostname changed: {hostname}")
        if hostname.endswith("-2"):
            hostname = str(hostname)[0:-2]
            print(f"removing `-2` suffix: {hostname}")
            self.avahi_server.SetHostName(hostname)

def main(): DBusGMainLoop(set_as_default=True) bus = dbus.SystemBus() object = bus.get_object("org.freedesktop.Avahi","/") avahi_server = dbus.Interface(object, 'org.freedesktop.Avahi.Server') runner = Runner(avahi_server) avahi_server.connect_to_signal('StateChanged', runner.on_state_change) print("Starting main loop") loop = GLib.MainLoop() loop.run()

if name == 'main': main()

https://gist.github.com/dymk/d06425d932c96916f27eed9bfc5bb7cc

dymk
  • 111
  • 2
1

See section 4.7 "Hostname changes with appending incrementing numbers" in Avahi. Here it states it is a known bug in Avahi. One workaround is to turn off IPv6 on your local router. I tried and it worked in my network. I have struggled with a Raspberry pi 4 for some time. I don't know why I haven't seen the issue on other Pis.

JL-EU
  • 11
  • 1
1

On an RPi with console autologin, I added

sudo service avahi-daemon restart

at the end of .bashrc.