How to automount sshfs?

6,045

Rather than using system-wide /etc/fstab, I suggest using afuse. It's mentioned in passing in the Arch wiki you link, but it's also included in Fedora. This runs in your user session and can therefore either use ssh-agent or prompt for a password.

It also will only mount on demand, and can be configured to unmount after a timeout, which is particularly valuable if your network isn't perfectly solid.

afuse -o intr -o timeout=300 \
  -o mount_template='sshfs -o intr -o follow_symlinks -o reconnect <username>@<remote_host_ip_address>:<remote_path>:%r %m' \
  -o unmount_template='fusermount -u -z %m' \
  ~/<localmount>

... making sure to replace the <things in brackets> with your local options. The afuse docs give a few other options that you can use - I like -o populate_root_command, but it's not necessary.

There are a number of different ways to run this automatically on login; it depends on your desktop environment, but basically you'd have to add the afuse line to autostart like any other such command.

Share:
6,045

Related videos on Youtube

Kurtcebe Eroglu
Author by

Kurtcebe Eroglu

A passionate programmer and web developer with a background in front-end and back-end development, which is what he's been doing for over eight years. I had experience in web development using PHP (mostly), MySQL and JavaScript. I follows two major principles everyday work: beauty and simplicity. I believes everyone should learn something new every day. While I'm not working, I spends time coding personal projects, learning, watching screen casts, blogging, etc. Some specific areas of interest for me include cloud computing and anything related to web development among other like system and database administration.

Updated on September 18, 2022

Comments

  • Kurtcebe Eroglu
    Kurtcebe Eroglu almost 2 years

    I am trying to auto mount a remote resource through sshfs but is not working for me. I have read all of this, this, this and this before ask to see if I can get the solution for my issue but it didn't work.

    So here is what I have done so far:

    • Added the following line to /etc/fstab:

      <username>@remote_host_ip_address:remote_path host_path fuse.sshfs delay_connect,_netdev,user,idmap=user,transform_symlinks,identityfile=/home/<username>/.ssh/id_rsa,allow_other,default_permissions,uid=1000,gid=1000 0 0
      
    • I have ssh into the remote host once so it gets added to the /home/<username>/.ssh/know_hosts file. I have checked after and the remote host is there

    • I have run the command sudo mount -a

    When I change directory and check the local path something goes wrong since I can not cd into it and it looks like:

    enter image description here

    What I am doing wrong here? What I am missing?

  • Kurtcebe Eroglu
    Kurtcebe Eroglu almost 8 years
    Ok, but afuse command can not be used on boot, right? If can be used can you add the proper line to your answer?
  • mattdm
    mattdm almost 8 years
    Do you need it to be available on boot? Why?
  • Kurtcebe Eroglu
    Kurtcebe Eroglu almost 8 years
    Well the idea is not to run the command all the time, for example let's said I run the command once, then for some X reason the computer restart and I need to login again, I would like to have the shared folder mount by default but anyway if this is not possible then what's the right command for mount the shared folder using afuse? The example on the Wiki link is not so helpful
  • mattdm
    mattdm almost 8 years
    Afuse will mount the sshfs filesystem automatically whenever you access it. I'll add some details.
  • crypdick
    crypdick over 4 years
    afuse hasn't been updated in 7 years
  • mattdm
    mattdm over 4 years
    @crypdick Concerning, but it still works for me.