Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is it possible to auto-mount a remote FS using sshfs upon instantiating a proper VPN connection?

Allow me to explain the scenario, I'm working remotely, to do that it helps if I can mount my home dir from a server in the office. To do that I need to vpn in. So within network manager I select the relevant VPN and connect. It connects but now I have to drop to the command line and mount my home dir on several machines.

If I forget to do one machine my local dev environment isn't as efficient. I suppose I could write a quick bash script to do this but I'd rather get it running automatically when I connect.

share|improve this question
    
possible duplicate of Doing something if connected to particular SSID in Ubuntu –  Seth Jan 6 at 2:43
    
@Seth That question is duplicate of this ;) –  Radu Rădeanu Jan 6 at 15:14
    
@RaduRădeanu That question is more general, so it will be more helpful to people in the future. –  Seth Jan 6 at 16:10
    
Actually, I've changed my mind, they're totally different questions. –  Seth Jan 6 at 16:26
add comment

2 Answers 2

up vote 11 down vote accepted

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.

share|improve this answer
    
This seems like the best way to do this. However I can't get mount -t sshfs user@host:/dir /home/myuser/dir to work, so I've tried sshfs hostname:?directory /home/myuser/dir but that doesn't seem to work either. Any ideas? –  Mark D Sep 19 '12 at 4:07
    
@MarkD Does your issue only arise in a NetworkManager dispatcher script? If not, then you may want to ask another question for this. And please be more specific about "does not work". –  gertvdijk Sep 19 '12 at 8:48
    
@gertvdijk What If I use ssh prv key for connecting and I want to have same permissions as user that run sshfs? meaning, will permissions work too? –  confiq Dec 9 '13 at 8:57
1  
@confiq No, NetworkManager will run this script as root. As with any other invocation from root as user, use su. Eg. su -l username -c 'mycommand' –  gertvdijk Dec 9 '13 at 12:53
add comment

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

share|improve this answer
    
While autofs would work if the network were reachable some of the time. It is only reachable when I'm connected to the vpn. –  Mark D Sep 19 '12 at 4:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.