How to auto mount an ssh file system on demand with an RSA key

I am trying to auto mount a folder from my raspberry pi (/home/pi/server_folder), to a local folder (/home/my_name/raspberrypi). I can do this with sshfs (auto mount in fstab) when I set up a blank rsa key, but when I try to use an actual key, like 123, the raspberry pi filesystem wont mount. This is pretty obvious, since I have to supply the passphrase, but is there a way to have it ask for the passphrase when I first try to access /home/my_name/raspberrypi, or do something similar to that? Because if someone gets my laptop, they dont need to put a password or anything in to get access to my raspberry pi, if I leave the rsa key blank. I have looked into autofs, and autosshfs, but autosshfs won’t download, and autofs doesn’t explain how to mount with an actual rsa key (well, I haven’t found a guide on how to). I’m using arch Linux, latest version. Here is the fstab entry:

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="79091039484957495749574c49">[email protected]</a>:/home/pi/server_folder/ /home/my_name/raspberrypi  fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,IdentityFile=/home/my_name/.ssh/id_rsa,allow_other,reconnect 0 0

which is what arch wiki says to do (but doesn’t explain very well).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Since SSHFS is based on FUSE, it’s easier to use a non-root automounter. Use afuse, that’s prety much what it was designed for. One-time setup:

mkdir ~/.afuse
ln -s .afuse/raspberrypi/server_folder ~/raspberrypi

To start the automounter:

afuse -o mount_template="sshfs %r:/ %m" -o unmount_template="fusermount -u -z %m" ~/.afuse

Make sure that the SSH_AUTH_SOCK variable is set when you start afuse, i.e. it must be started after ssh-agent. Run ssh-add to load the key into the SSH agent, and then you’ll be able to access the SSHFS directories.

Method 2

To expand on Gilles’ correct answer, and to address some further thoughts addressed in comments to his answer regarding ‘Integration of into (profile) startup (scripts)’:

You can enable on-demand mounting by adding a script invocation to your e.g. ~/.profile or create a user level systemd service unit.

I favor the answer: ~/.config/systemd/user/afuse.service

[Unit]
Description="SSHFS via Afuse automounter"
AssertPathExists=%h/scp/
AssertFileIsExecutable=/usr/bin/afuse
AssertFileIsExecutable=/usr/bin/sshfs

[Service]
Type=forking
WorkingDirectory=%h/scp
ExecStart=/usr/bin/afuse 
    -o mount_template="sshfs -o ServerAliveInterval=10 -o reconnect %%r:/ %%m" 
    -o unmount_template="fusermount -u -z %%m" .
Restart=always
PrivateTmp=true
#NoNewPrivileges=true
#Environment=

[Install]
WantedBy=default.target


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x