Skip to content

Instantly share code, notes, and snippets.

@Elandig
Forked from tomslominski/README.md
Created March 30, 2025 11:17
Show Gist options
  • Select an option

  • Save Elandig/a6d48a0fe4694621c8ceda7b1d22afd6 to your computer and use it in GitHub Desktop.

Select an option

Save Elandig/a6d48a0fe4694621c8ceda7b1d22afd6 to your computer and use it in GitHub Desktop.
Avahi alias systemd service

Instructions

The first file is the script to be run, which you can store in your home directory. The second file is the systemd unit file which goes in /etc/systemd/system. Make sure on line 6 it references the location of the first file, and that you run systemctl daemon-reload after editing the unit file.

To run the service automatically on boot, run sudo systemctl enable avahi-alias@[domain], followed by sudo systemctl start avahi-alias@[domain] to start it immediately.

#!/bin/bash
local=$(avahi-resolve -4 -n $(hostname).local | cut -f 2)
hostname=$1
if [ $local != "127.0.0.1" ]
then
echo "Aliasing $local as $hostname."
avahi-publish -a -R $hostname $local
else
echo "Exiting, local address is $local."
exit 1
fi
[Unit]
Description=Publish %I as alias for %H.local via mdns
[Service]
Type=simple
ExecStart=/bin/bash -c "/home/osboxes/avahi-alias.sh %I"
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment