17

Is it possible to automatically mount a sshfs filesystem whenever the connection goes up?

Olivier Lalonde
  • 57,431
  • 62
  • 125
  • 146

4 Answers4

5

I found the easiest way to achieve this is to create a small script for the process and add it to the start up applications list. Add these two lines to a text file and save it somewhere you'll remember as something like sshfs.sh than add it to start up.

#! /bin/bash
sshfs <host>@<ip>: ~/<mountpoint>

Be sure to create the folder before launching the script or it will have no where to mount the file system you are connecting to. The mount point can be wherever you want I just create it in my home folder.

shaneo
  • 697
  • 1
  • 7
  • 12
  • 1
    how do you get around the need for a password to the host machine? – Joshua Robison Apr 26 '13 at 01:47
  • Please explain how, or link to, the way to "add it to the start up applications list." This may be trivial for you, but not for many (I don't know how to do it). – Gabriel Staples Aug 31 '16 at 22:34
  • @JoshuaRobison You can create `[ssh-keys](https://www.cyberciti.biz/faq/ubuntu-18-04-setup-ssh-public-key-authentication/)` to avoid password. – Muhammad bin Yusrat Apr 16 '20 at 07:57
  • 1
    @GabrielStaples Press the `super` key (windows key) on the keyboard and type Startup, you will find the application that responsible for running programs on start up. You can add a script to that list to have it execute automatically whenever you login. – Muhammad bin Yusrat Apr 16 '20 at 07:58
  • 1
    Holy smokes that was 4 years ago! I've since advanced in my knowledge and usage of Linux. See this, for example: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles. Nevertheless, this will help the next person for sure. Thanks for the comment. I've upvoted it. – Gabriel Staples Apr 16 '20 at 09:06
  • @GabrielStaples Thnx!! – Muhammad bin Yusrat Apr 16 '20 at 09:16
  • This is not just one of the best answers I found, but IMO the BEST one. Period. All other advice I've seen lead to frustration (the feeling, not the name of a mountpoint). – Rahav Aug 25 '23 at 16:37
2

Does Upstart in Ubuntu work with network events? For sure you can place scripts in /etc/network/if-up.d and /etc/network/if-down.d. There is a guide on Ubuntu Forums.

Does this help enough?

  • I believe in newer releases, you should use the network manager scripts for this task, see e.g. [this guide](http://www.thelupine.com/content/ubuntu-networkmanager-scripts). Christopher's link however also covers questions for password and `allow_others` etc. – Carsten Thiel Jan 11 '11 at 08:36
2

So if sshfs can be listed in /etc/fstab (I believe it can be), then mountall will mount it any time a network device is brought up. The issue is that when the net device goes down, you need to umount it. This would be doable with an upstart job like /etc/init/sshfs-down.conf:

# sshfs-down

start on net-device-down IFACE!=lo
task

exec umount /path/to/sshfs/mount

One problem will probably arise that sshfs may try to flush buffers on umount, and at the point that the net device is already down, you'll have issues.

SpamapS
  • 19,570
  • 4
  • 31
  • 49
  • another problem is sshfs asks for the password of the user's account folder that you are mounting on the host machine... that would make it hard to mount on startup – Joshua Robison Apr 26 '13 at 01:46
1

I think the more useful thing to do is automatically mount an sshfs file system on access. autofs can be configured to auto mount sshfs filesystems when you access the mount point and unmount after a timeout.

Have a look at autosshfs, which allows mounting an sshfs (fuse) filesystem with a user's ssh-agent. That solves the usual authentication issue with autofs: since it runs as root it is tricky to set up to authenticate as a user without having to type a passphrase every time.

kynan
  • 2,207
  • 1
  • 22
  • 24