Skip to content

Instantly share code, notes, and snippets.

@lordtangent
Last active June 5, 2024 08:01
Show Gist options
  • Save lordtangent/0092643428b42c796415df2a20bc3861 to your computer and use it in GitHub Desktop.
Save lordtangent/0092643428b42c796415df2a20bc3861 to your computer and use it in GitHub Desktop.
universal sshfs Automount

Universal sshfs automount setup

This gist requires autofs and sshfs be installed on your system. You must have ssh access to each host you would like to automount via a passwordless ssh key.

The script currently expects a the key to be named $HOME/.ssh/id_ed25519 but you can change it if you like.

/etc/auto.master

Make sure the following line is in your /etc/auto.master it will source the files in /etc/auto.master.d/

# /etc/auto.master
+dir:/etc/auto.master.d

You will also need to install the following files. The auto.sshfs script must be executable! (it executes and then feeds it's output to the automount command)

trigger to the automount of the remote host by going to /net/sshfs/username@hostname

#!/bin/bash
# /etc/auto.sshfs automount script
# This file must be executable to work! ( chmod 755 )
key="${1/%:/}"
user="${key/@*/}"
t="${key/*@/}"
server="${t/:*/}"
tp="${t/*:/}"
port="${tp/\/*/}"
if [ $port = $server ]; then
port=22
fi
if [[ $(find /etc/sshfs/$server/uidfile -mtime +7 -print 2>&1 ) ]]; then
mkdir -p /etc/sshfs/$server
ssh -p $port -i /home/${user}/.ssh/id_ed25519 ${user}@${server} 'cat /etc/passwd | cut -d ":" -f1,3' > /etc/sshfs/$server/uidfile
ssh -p $port -i /home/${user}/.ssh/id_ed25519 ${user}@${server} 'cat /etc/group | cut -d ":" -f1,3' > /etc/sshfs/$server/gidfile
fi
mountopts="-fstype=fuse,rw,nodev,nonempty,noatime,allow_other,max_read=65536,port=${port},\
idmap=file,uidfile=/etc/sshfs/$server/uidfile,gidfile=/etc/sshfs/$server/gidfile,\
StrictHostKeyChecking=no,cache_timeout=3600,IdentityFile=\$HOME/.ssh/id_ed25519"
echo "$mountopts :sshfs#${user}@${server}:/"
# /etc/auto.master.d/sshfs.autofs
# /net/sshfs needs to exist to trigger this rule!
/net/sshfs /etc/auto.sshfs --timeout=60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment