John Siu Blog

Tech - Business Tool, Personal Toys

Avahi and Multiple Alias

☰ Table of Content

Configure Avahi to broadcast multiple names.

Ref:

Configuration Files

Create the two files below.

/etc/systemd/system/avahi-alias4@.service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[Unit]
Description=Publish %I as alias for %H via mdns (IPv4)

[Service]
Type=simple
ExecStart=/bin/bash -c "/home/js/.script/avahi-alias.sh 4 %I"
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

/etc/systemd/system/avahi-alias6@.service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[Unit]
Description=Publish %I as alias for %H via mdns (IPv6)

[Service]
Type=simple
ExecStart=/bin/bash -c "/home/js/.script/avahi-alias.sh 6 %I"
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

/etc/systemd/system/avahi-alias.sh:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/bash

ALIAS=$2
IP_VER=$1

MDNS_DOMAIN="vms.local"
HOSTNAME=$(hostname -s).$MDNS_DOMAIN

avahi_alias() {
	IP=$(avahi-resolve -$IP_VER -n $HOSTNAME | cut -f2)
	echo "IPv$IP_VER=$IP"
	if [ $IP != "127.0.0.1" ] && [ $IP != "::1" ]; then
		echo "Aliasing $ALIAS -> $IP."
		avahi-publish -a -R $ALIAS.$MDNS_DOMAIN $IP
	else
		echo "Exiting, local address is $IP."
		exit 1
	fi
}

avahi_alias

Enable Alias

Enable Alias for IPv4

1
systemctl enable --now avahi-alias4@<alias>.service

Enable Alias for IPv6

1
systemctl enable --now avahi-alias6@<alias>.service

John Siu

Update: 2025-08-02
Tag
comments powered by Disqus