3

Let me explain what I'm trying to do:

  • I'm creating a Linux Distro Image that will be installed on a SD Card
  • Setting the default hostname to myHostname.local
  • This is all on my local network

This is fine if I only have 1 server, but what about if I run an additional server. How does the hostname work as both hosts would now be the same name.

  • myHostname.local <-- server 1
  • myHostname.local <-- server 2

Does Avahi offer a way to dynamically change the hostnames? Something like this:

  • myHostname1.local <-- server 1
  • myHostname2.local <-- server 2

any suggestions?

  • I am surprised that the many tutorials I read did not point this out. This is exactly what I need to happen! Thank you! – TamusJRoyce Aug 15 '19 at 01:10

2 Answers2

4

Avahi, which is an implementation of the Zeroconf protocol described in RFC6762, changes the hostname of the machine it is running on to a different one by adding a number at the end.

This anti-duplicate processus does not changes the hostname on the machine (so the others applications running on the machines will see and announce the same hostname) and if the machines are started in a different order, it is likely they will get a different hostname.

If you are looking for consistency and stability of the hostnames, this solution will not work.

There are different solutions to give each machine a unique hostname :

  • modify your deployement process to give each installation a unique hostname (it can be a manual intervention),
  • use a configuration management system to modify each installation after the initial deployement,
  • make each machine generate random name (with a password generator like pwgen for example) at the first boot (or at each boot if your storage on the machines is read-only).

The most simple solution should be to change your deployement process, but you may need a configuration management system at some point.

For the last solution, I wouldn't recommend it in a small, personal deployement, it is likely that you will spend more time to make it work than you would have spend modifying each machine by hand. Also note that it is almost what you already have with avahi, except for the name persistence.

pseudorandom
  • 151
  • 4
  • 1
    hmm my testing found different results. I have setup two servers with the same hostname and avahi appends a -2 to one of them. so myHostname.local and myHostname-2.local so when this happens I'd also like avahi to add the -1 to the original server, thoughts? – Phill Pafford Jul 30 '14 at 14:42
  • You are right. I just checked the [RFC6762](http://tools.ietf.org/html/rfc6762#section-9) describing the protocol and this behaviour is precisely described. – pseudorandom Jul 30 '14 at 15:21
  • In the 9th paragraph on name conflict resolution, it is suggested that if the hostname is ending with a number, this number should be incremented : have you tried to end the default hostname of your machines with a number like `myHostname1` or `myHostname-1` ? – pseudorandom Jul 30 '14 at 15:23
  • yes it just appends to it like this myHostname-1 myHostname-1-2, I'm just looking for consistency with naming – Phill Pafford Jul 30 '14 at 17:40
  • Ok, so I think if you want this kind of control the only working solution is to give the different hostnames yourself. I will modify my answer to correct the errors and add something about the norm. – pseudorandom Jul 30 '14 at 18:41
0

Use avahi-utils:

Using avahi-utils (Debian), you could have a shell script that scans the network for duplicates via avahi-browse command (on network connect on all NICs*), then set the hostname via avahi-set-host-name.

*: Doing this on a single network interface one time will not suffice. Several devices implement multi NIC systems where a device can be connected to multiple networks at once. These NICs can also disconnect/reconnect independently of each other, and you should note that they can do so independently of the startup event. So, a startup/logon script won't solve your problem. You need a script that is triggered on network connection:

Here are several methods for triggering a script on network connection for you to use

Additionally, you may want to set your script on a timer to scan the network for changes. Also consider making a web service between your devices to notify them that another one has connected.

DaMaxContent
  • 101
  • 2