Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Everytime I boot, I use sshfs to mount remote directories. I am thinking to put these short commands in my .bashrc, but that does not seem to be The Right Way™.

What is the proper way to run sshfs on login?

share|improve this question

3 Answers 3

Indeed, .bashrc isn't right. That file is read when you open a terminal running an interactive shell (if your shell is bash).

You can perform sshfs mounts at boot time, but that only works if you don't need any interactive authentication: in most setups this means a passwordless key. (There are other ways, but they only apply inside trusted networks where you would typically already have something like NFS.)

  • If you're root, you can add an entry in /etc/fstab. Note that this will use root's ~/.ssh to look up keys and known hosts.

    example.com:/remote/path /mount/point fuse.sshfs idmap=user,uid=1234
    
  • You can put an @reboot crontab entry.

    @reboot sshfs -o idmap=user example.com:/remote/path /mount/point
    

If you need to type a password to send to the remote server, you need to log in and have a user interface available first. Same thing if you need to type a password to unlock a key.

Most window managers and desktop environments allow you to run custom commands when you log in. You can run a snippet like the following (assuming your environment already starts an SSH agent — most do these days):

SSH_ASKPASS=ssh-askpass ssh-add ~/.ssh/id_rsa
mount | grep ' /mount/point ' ||
sshfs -o idmap=user example.com:/remote/path /mount/point

mount | grep ' /mount/point ' || checks whether the filesystem appears to be mounted already, in case you log in through multiple means. Alternatively, without using an agent:

mount | grep ' /mount/point ' ||
SSH_ASKPASS=ssh-askpass sshfs -o idmap=user example.com:/remote/path /mount/point

If you don't have the ssh-askpass program, another method is to open a terminal just to do the mounting:

xterm -e 'ssh-add ~/.ssh/id_rsa'
mount | grep ' /mount/point ' ||
sshfs -o idmap=user example.com:/remote/path /mount/point

For a text mode log in, put ssh-add in your ~/.profile. Run it only if the shell is interactive.

case $- in
  *i*)
    ssh-add ~/.ssh/id_rsa
    ;;
esac
sshfs -o idmap=user example.com:/remote/path /mount/point
share|improve this answer
    
We have some issues where this mount (from .bash_profile) will not work if the user logs in shortly after boot - the network (wifi) isn't up yet. A possible solution might be running a script on network up. –  Bob Oct 6 at 13:17

If you want to mount file systems at boot, you need to put them into your /etc/fstab. Here is an example from OpenBSD documentation. I do this regularly for NFS storage on remote servers.

10.0.0.1:/work /mnt nfs rw 0 0

That format will work well. Your IP then the directory you want to mount. And then where you want to mount it on the remote server. fstab is the best way to do it at mount. Or you can have a little script you can execute when you boot up.

share|improve this answer
1  
This wouldn't work as written for per-user fuse mounts like sshfs. –  Jim Paris Nov 5 '12 at 17:55

This might be an easier way to do it, but I can't speak on the security of this method:

Create a file at /home/user/sshfs with your password in it.

Edit /etc/rc.local and before the exit 0 add the following code:

sshfs user@ipor.com:/Remote/Path/To/Mount /local/path/to/mount -o workaround=rename -o gid=xx,uid=xxxx -o allow_other -o password_stdin < /home/user/sshfs

Make sure to add in your proper user name, ip, group id, and user id.

Final note: execute the above command once before expecting it to auto-mount on reboot.

share|improve this answer

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.