7

How can a computer running avahi ascertain and display its OWN hostname in the event that it is dynamically changed to foo-2, foo-3, etc due to hostname conflicts with other devices on the network?

When two computers (both with hostname = foo) that are running avahi-daemon and are on the same networks, as expected they can be accessed via ssh as foo.local and foo-2.local. Eg, the hostname collision is being handled correctly by avahi.

However, on both machines the command hostname returns foo. So that is not dynamically updated when avahi does it's hostname renaming.

What command will show the correct (dynamical) hostname to access a computer?

These are mobile devices, and I want to display "my hostname is XXXX.local' on each device so when more than one device is present the user know WHICH hostname to enter to go to "their' device.

1 Answer 1

3

Run:

avahi-resolve -a <IP> | cut -f 2

This will return a list of hostnames (one per-line) registered on mDNS for the IP address you passed in. If you pass in your own local IP, it will return what you have registered. Under normal circumstances, it should return exactly one line with your local host name (or whatever incremental hostname if there were collisions).

If you remove the cut command at the end, you can just parse the lines yourself in your own code by splitting at the first tab character and taking the second part of each line.

Also, there might be some call you can make on DBus to get this info, but if there is, I've not found any info about it.

1
  • Note you'll need to install avahi-utils (e.g. sudo apt install avahi-utils) for avahi-resolve to work.
    – CletusW
    Oct 7 at 1:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .