18

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

4 Answers 4

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.

7
  • 1
    how do you get around the need for a password to the host machine? Apr 26, 2013 at 1:47
  • 1
    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). Aug 31, 2016 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. Apr 16, 2020 at 7: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. Apr 16, 2020 at 7:58
  • 1
    Holy smokes that was 4 years ago! I've since advanced in my knowledge and usage of Linux. See this, for example: github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles. Nevertheless, this will help the next person for sure. Thanks for the comment. I've upvoted it. Apr 16, 2020 at 9:06
3

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.

1
  • 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 Apr 26, 2013 at 1:46
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?

1
  • I believe in newer releases, you should use the network manager scripts for this task, see e.g. this guide. Christopher's link however also covers questions for password and allow_others etc. Jan 11, 2011 at 8:36
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.

1
  • 1
    autosshfs has been taken down on GitHub and autofs' website has been taken over.
    – sean
    Mar 2, 2020 at 17:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .