Skip to content

Instantly share code, notes, and snippets.

@Ronmi
Last active September 20, 2025 07:26
Show Gist options
  • Select an option

  • Save Ronmi/5cf35ad5db78ed17fe4d13721132767c to your computer and use it in GitHub Desktop.

Select an option

Save Ronmi/5cf35ad5db78ed17fe4d13721132767c to your computer and use it in GitHub Desktop.
a quick and dirty script to enable systemd.automount + sshfs
#!/usr/bin/bash
# This script assumes you
#
# - use key to authorize
# - have an entry in known hosts file (eg. ssh to remote host once)
#
# You should check the "Options=" line to add or change sshfs/fuse options, especially
# the path to key file (default to "${HOME}/.ssh/id_ed25519") and known_hosts file.
if [[ $# -ne 2 ]]
then
echo "Usage: $0 remote_spec local_absolute_path"
echo
echo "Example: $0 user@host:. /mnt/host/home"
exit 1
fi
remote="$1"
local="$2"
mount_base="$(systemd-escape --suffix mount "$local" | cut -d '-' -f 2-)"
mount_fn="/etc/systemd/system/$mount_base"
automount_base="$(systemd-escape --suffix automount "$local" | cut -d '-' -f 2-)"
automount_fn="/etc/systemd/system/$automount_base"
echo "Creating $mount_fn ..."
cat > "$mount_fn" <<MOUNT
[Unit]
Description=Mount $remote on $local
[Mount]
What=$remote
Where=$local
Type=sshfs
Options=_netdev,default_permissions,allow_other,uid=$(id -u),gid=$(id -g),IdentityFile=${HOME}/.ssh/id_ed25519,UserKnownHostsFile=${HOME}/.ssh/known_hosts,StrictHostKeyChecking=yes
MOUNT
echo "Creating $automount_fn ..."
cat > "$automount_fn" <<AUTOMOUNT
[Unit]
Description=Automount $remote on $local
[Mount]
Where=$local
[Install]
WantedBy=multi-user.target
AUTOMOUNT
systemctl daemon-reload && systemctl enable "$automount_base" && systemctl start "$automount_base" || {
echo "Failed!? You might want to run systemctl enable $automount_base manually to debug."
exit 1
}
echo
echo
echo "You might want to verify options in $mount_fn"
echo
echo "Don't forget to run following commands after you edit it:"
echo " systemctl daemon-reload"
echo " systemctl restart $automount_base"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment