How to prevent (or at least automatically fix) `avahi-daemon: running [ubuntu_comp_hostname.local]` getting replaced with version with `-2` appended?

I have this problem where this:
#=> avahi-daemon: running [ubuntu_comp_hostname.local]
keeps getting itself replaced with a version with a -2 appended:
#=> avahi-daemon: running [ubuntu_comp_hostname-2.local]

(
I find that from:
> ps -eo cmd | grep avahi
which other than that (and the grep itself) turns up just:
#=> avahi-daemon: chroot helper
)


I've been fixing the problem by just running:
> sudo service avahi-daemon restart
but it's just annoying whenever I try to run some shell function that involves ssh,
and it just hangs for a while before finally going:
#=> ssh: Could not resolve hostname ubuntu_comp_hostname.local: Name or service not known


(This is just a personal laptop, on a home network with one of those standard-issue issue modem-router setups from the local ISP company.)


I'm sadly pretty ignorant (on this subject in particular and in general),
but it seems reasonable to guess that:

  • avahi is crashing or restarting or something

  • and then it tries to start again with plain [ubuntu_comp_hostname.local],
    but that's still hanging around in a defunct state or something
    (as a process on the computer? or in some cache somewhere else on the network? or...?)
    so it automatically appends a -2 to the end of the actual hostname

Something like that, right?


I'm not sure exactly when it happens. All I can say is:

  • it doesn't happen every time the computer wakes up from sleep

  • it may happen at times other than when the computer has just woken up from sleep


So... how can I prevent this from happening?
(Or failing that, get it to automatically fix itself when it does happen??)


Solution 1:

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