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?
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 :
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.
Use
avahi-utils
:Using
avahi-utils
(Debian), you could have a shell script that scans the network for duplicates viaavahi-browse
command (on network connect on all NICs*), then set the hostname viaavahi-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.