Skip to content

Instantly share code, notes, and snippets.

@tomslominski
Last active July 6, 2024 07:32
Show Gist options
  • Save tomslominski/9d507acd4036952d65b2364d3750fb36 to your computer and use it in GitHub Desktop.
Save tomslominski/9d507acd4036952d65b2364d3750fb36 to your computer and use it in GitHub Desktop.
Avahi alias systemd service. 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.
#!/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
@J-Siu
Copy link

J-Siu commented Jul 6, 2024

Thank you very much for your solution!! It works perfectly!!

For others using this on Ubuntu 22+, the bash is located at /usr/bin/bash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment