Is it possible to auto-mount sshfs

Solution 1:

Find the UUID of your connection using

$ nmcli con

Note that this lists not just physical connections but also the Wireless connections defined (SSIDs).

Put some simple script like this in your /etc/NetworkManager/dispatcher.d/ directory:

#!/bin/bash

# Specify your connection UUID you like to trigger on below.
MYCON_UUID=397bdb70-2a89-415e-b3e9-09ca0b704fc1

if [ "$CONNECTION_UUID" == "$MYCON_UUID" ]
then
    # do your scripting you need to do here:
    mount -t sshfs ...
fi

Don't forget to set the right permissions to make it excutable (i.e. chmod +x trigger-sshfs-on-vpn.sh). It can be any type of script, a Bash script is probably sufficient for your purpose.

NetworkManager just executes all the scripts in this directory providing some environment variables you can use for scripting. In this case you probably just need CONNECTION_UUID.

Solution 2:

Use autofs.

Autofs will automatically mount a folder which is configured as a mount point when someone or something is accessing it on your system.

The mount point can be a remote host through sshfs as well as an arbitrary other mount point such as:

  • samba
  • nfs
  • NTFS

here is a nice howto