Configure Zeroconf to broadcast multiple names

I want to broadcast multiple host names on a local network only from a single instance of Ubuntu Server 10 with Zeroconf or something similar. It has to be zeroconf or similar because it's going to be used in a virtual machine configuration and will be copied around so I can't depend on an external DNS server. I've collected a number of resources, but haven't been able to figure it out. Is there a way to do this?

Here are some resources I've gathered: http://bit.ly/l5W4ab

EDIT: It would also be fine to set up VirtualBox to be a DNS server too.


You can configure Avahi to publish arbitrary hostnames using /etc/avahi/hosts, but you need to specify the exact IP address to map the hostname to. There doesn't seem to be a way to publish multiple hostnames to the IP addresses detected by Avahi. You could write a startup script which populates /etc/avahi/hosts after discovering the VM's IP address.


Here's a little systemd service that lets you specify an alias for your current machine like test.local in addition to hostname.local (assuming your machine is called hostname).

Setup

First install avahi-utils, if you haven't already:

sudo apt-get install avahi-utils

Then put the following into /etc/systemd/system/[email protected]

[Unit]
Description=Publish %I as alias for %H.local via mdns

[Service]
Type=simple
ExecStart=/bin/bash -c "/usr/bin/avahi-publish -a -R %I $(avahi-resolve -4 -n %H.local | cut -f 2)"

[Install]
WantedBy=multi-user.target

(The avahi-resolve is used to get the current IP address that is already being published for the hostname)

Use

Then to make the current machine available as test.local in addition to its current hostname.local, you would enable the service with:

sudo systemctl enable --now [email protected]

You can enable multiple aliases by starting multiple services, e.g. [email protected] and [email protected], which makes use of systemd's multi-instance features:

sudo systemctl enable --now [email protected]
sudo systemctl enable --now [email protected]

Naturally you can disable each alias independently, too, with:

sudo systemctl disable --now [email protected]

Enjoy!