28

I'm trying to enter an sshfs mount in /etc/fstab with the following line:

sshfs#[email protected]:/media/usb0 /media/ExtHD fuse     defaults,nonempty,allow_other 0 0

So that this volume is mounted at boot. After booting up, nothing happens, but when I use the command sudo mount -a, I am always prompted for the password. I have set up SSH Keys and transferred them over to the computer at 192.168.0.2, and can log in to regular ssh with no pasword. How can I stop fuse from asking for my password so that the volume can be automatically mounted at boot time?

If it helps at all, I am trying to connect to a home server running Debian from a laptop running Arch Linux. Thanks

8 Answers 8

15

Key-based authentication can only work if the ssh process can find your key. You presumably have your key in your home directory; but you've never told sshfs where to look for a key. At boot time, it would be root mounting all filesystems, therefore the key must be either in /root/.ssh or referenced in /root/.ssh/config.

I recommend mounting the filesystem after you've logged in, and as your own user. Put this in a script that's executed when you log in:

ssh-add ~/.ssh/name_of_key.id_rsa
sshfs homeserver:/media/usb0 ~/exthd

Put an alias called homeserver in your ~/.ssh/config:

Host homeserver
HostName 192.168.0.2
User oli
3
  • 1
    Ahh, that is a better way of doing it, I didn't think to put it in my login script. Thanks! Jan 18, 2012 at 17:19
  • 1
    Be careful using ~ in the mount point. If you're using a launcher icon on the panel to run the command instead of a terminal, the ~ may not be evaluated as /home/ and the command will fail.
    – Nick
    Jan 11, 2014 at 18:47
  • @Nick If you're using a GUI, it might have a tool dedicated to automounting. If you follow my answer, you're to put the two commands in a shell script; the launcher icon would run that script. Jan 11, 2014 at 18:51
13

I was able to make sshfs mount via fstab with Ubuntu 14.04 by doing using the following syntax as a standard user (note anything in CAPS is a variable for you to fill in):

USER@HOST:/REMOTE_DIR /LOCAL_DIR fuse.sshfs delay_connect,_netdev,user,idmap=user,transform_symlinks,identityfile=/home/USERNAME/.ssh/id_rsa,allow_other,default_permissions,uid=USER_ID,gid=USER_GID 0 0

This is a combination of the information found here https://superuser.com/questions/669287/automount-sshfs-using-fstab-without-mount-a and adding the delay_connect from here https://askubuntu.com/questions/326977/sshfs-is-not-mounting-automatically-at-boot-despite-etc-fstab-configuration.

I used this tutorial to get my USER_ID and USER_GID: https://kb.iu.edu/data/adwf.html

To find a user's UID or GID in Unix, use the id command. To find a specific user's UID, at the Unix prompt, enter:

id -u username

Replace username with the appropriate user's username. To find a user's GID, at the Unix prompt, enter:

id -g username

Also, if you want a full tutorial, it will be posted at https://www.variux.com/sshfs-automount-at-boot-with-fstab-on-ubuntu-14-04/

3
4

I had the same problem.

I had previously setup ssh keys via the ssh-keygen and then ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh worked fine without asking me for a password so I was confused as to why sshfs still wanted the password...

That is to say,

ssh [email protected] 

worked fine, however sshfs still asked for a password when I typed

sudo sshfs [email protected]:/home/raspberry/Videos /home/pi/LinuxMintMountedFolder/ -o allow_other

As Gilles above explained, sshfs was looking in the root directory for the necessary keys.

Therefore, I solved this by just copying the files id_rsa and id_rsa.pub from my home folder's hidden .ssh folder to the root's .ssh folder i.e.

cp /home/pi/.ssh/id_rsa /root/.ssh/
cp /home/pi/.ssh/id_rsa.pub /root/.ssh/

Then, typing the following worked without bothering me for a password:

sudo sshfs [email protected]:/home/raspberry/Videos /home/pi/LinuxMintMountedFolder/ -o allow_other
1
  • 2
    This is really really bad from a security perspective. Better create a separate set of keys for root and add this as well to authorized_keys. You should never copy private keys around. Sep 27, 2018 at 13:43
4

If you want it to boot without a password, you'll need to create a public/private ssh keys without password. It's not recommendend, but you can at least protect those files with a chmod 400 as root user.

After that, as usual, you'll need to copy the public key on the mount point :

ssh-copy-id -i my_new_key.pub [email protected]

And you'll need to tell sshfs to use this key.

[email protected]:/media/usb0 /media/ExtHD fuse     defaults,nonempty,allow_other,'IdentityFile /path/to/private/key' 0 0
3
  • 2
    SSHFS version 2.8 fuse: unknown option 'SSHOPT=IdentityFile /home/ravi/.ssh/id_boxy_rsa'
    – Tom Hale
    Apr 3, 2017 at 5:19
  • 1
    @TomHale instead of ,SSHOPT='IdentityFile /path/to/private/key' it is simply ,IdentityFile /path/to/private/key Jul 17, 2017 at 6:31
  • At least in Debian Buster you still need the equal sign: user@server:/share /mnt/share fuse.sshfs defaults,nonempty,allow_other,IdentityFile=/home/user/.ssh/id_rsa 0 0
    – Dokbua
    May 28, 2020 at 18:27
3

You actually can mount SSHFS without public key on startup by using the password_stdin option, but not with fstab.

What you need is a little script file and cron.

Please be aware that public key always is a better choice. But sometimes public keys don't work (I have had that issue a few times myself). Please note that the script file will contain the password in plain. Therefor you should make sure that only root has any permission to this file!

The script file can look like this (/root/automount.sh):

#!/bin/sh
if [ $(mount | grep 'user@server:/folder' | wc -l) -ne 1 ]
then
    echo 'PASSWORD' | sshfs user@server:/folder /mnt/mountpoint -o password_stdin,allow_other
    echo 'SSHFS has been mounted.'
else
    echo 'SSHFS is already mounted.'
fi

To run this on startup you can use crontab as root and enter this line:

@reboot /root/automount.sh

As stated above, you should make sure that absolutely nobody can read the file as it contains plain text password!

-rwx------ 1 root root 526 Nov 21 12:57 automount.sh

By now, after every reboot your system will automatically mount the SSHFS by using credentials.

2
  • Fantastic, works great for me. Some things for other people not to forget (which I totally didn't neglect to think about): 1. Make the automount.sh script executable (perhaps as illustrated). 2. Make the mountpoint directory in advance. 3. Also in advance, SSH to your target server as root to get that server added to root's known_hosts. Feb 20, 2020 at 19:40
  • While the script works and I'm still using it, calling it out of cron is a crapshoot because sometimes networking is ready and sometimes it isn't. I'm on Ubuntu 18.04 and so systemd was the way to go for this: askubuntu.com/a/1184762/870100 Feb 21, 2020 at 20:44
2

Works for me perfectly on Debian 8.2 adding the following line to fstab:

user@hostname:/path/to/localmountpoint /path/to/remotemountpoint fuse.sshfs _netdev,user,port=(if_not_22),idmap=user,transform_symlinks,identityfile=/home/user/.ssh/id_rsa,allow_other,default_permissions,uid=user_id,gid=group_id 0 0

Hostname indicates the remote server (folder to mount from) user is the same locally and remotely. (easier)

Make sure you have created locally the ssh key for the user using ssh-keygen and use ssh-copy-id to copy the key to remote server (hostname).

You can find user_id and group_id for the user in local server /etc/passwd.

Also make sure that the user has read/write permissions in the folder path/to/localmountpoint.

UPDATE FOR DEBIAN 10

user@server:/share /mnt/share fuse.sshfs defaults,nonempty,allow_other,IdentityFile=/home/user/.ssh/id_rsa 0 0

1

If you're trying to connect to a server that doesn't support keys and you still want it to be automatic, you can use sshpass to type in your password for you even with sshfs and even in fstab.

For example, if you put something similar to this in /etc/fstab:

sshfs#username@host:/  /mnt/here   fuse   auto,ssh_command=sshpass\040-f\040/root/.ssh/host.password\040ssh  0  0

Then put the password in the filename specified in /etc/fstab:

echo 'secret' > /root/.ssh/host.password

Make sure you have installed sshpass:

pacman -S sshpass  # if you're using Arch Linux

Then it should work:

mount /mnt/here

You might need to ssh to the server once first to confirm its key, but after that this should work with no manual interaction, and with the auto flag in fstab it should mount at boot time with no need to manually enter the password.

0

Had the same problem. In the end it helped either setting type to simple fuse, or (which is more probably) truncating options to:

fuse reconnect,idmap=user,allow_other,default_permissions

which worked for me in the end

You must log in to answer this question.

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