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?


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
    ssh-add ~/.ssh/id_rsa
sshfs -o idmap=user example.com:/remote/path /mount/point
|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 '14 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. /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.

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

For this use-case, I comfortably use afuse, a FUSE-based sshfs auto-mounter[1].

On distros with you can easily use an afuseuser service unit file like the one posted in another question here.

You can also (re-)spawn by a few simple lines of shell-script[2] sourced from ~/.profile.

[1]: afuse is available in the package manager for most distros of Linux.

[2]: This linked script is from my question about turning this setup into a systemd user service.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.