109

I've got a question that I've not been able to find an answer for. I have two computers, both of which run Ubuntu Linux 12.04. I have set up my first computer ("home") to be able to SSH into my second computer ("remote") using public/private RSA key authentication.

This is not the first SSH connection that have set up using key authentication on my home computer, so my home computer has several id_rsa private keyfiles (each of which is for a different computer to SSH into). Thus, I am able to successfully SSH only when I specify a keyfile (in ssh, the -i option), using ssh username@ipaddress -i path/to/keyfile/id_rsa.2.

That works great. However, I would also like to use sshfs, which mounts the remote filesystem. While ssh seems to play nice with multiple keys, I can't find a way to get sshfs to use the correct private key ("id_rsa.2").

Is there a way to get sshfs to do this?

1
  • Keychain works well for me in this scenario...
    – jasonwryan
    Commented Jan 17, 2013 at 0:45

5 Answers 5

160

Here's what works for me:

sshfs [email protected]:/remote/path /local/path/ -o IdentityFile=/path/to/key

You can figure this out via man sshfs:

-o SSHOPT=VAL ssh options (see man ssh_config)

Here, SSHOPT is meant to be the actual name of the option (as in the "what works for me" example above) and VAL the value to set it to (if any). Some versions of the man page may describe this differently.

Then from the list of such options in man ssh_config:

IdentityFile

Specifies a file from which the user's DSA, ECDSA or DSA authentication identity is read.

7
  • 2
    This command does not work on macOS Sierra
    – basZero
    Commented Dec 22, 2017 at 10:07
  • 6
    IdentityFile path note. If running sshfs with sudo, using ~ in the IdentityFile path refers to root's home, which may not be where the RSA file is. Use something like /Users/<username>/.ssh instead.
    – RavenMan
    Commented Jun 6, 2018 at 21:41
  • 5
    Caveats: IdentityFile= must be an absolute path not a relative path. I learned this today. Commented Jul 13, 2020 at 1:02
  • 1
    @goldilocks Thanks for describing the way how did you figured it out. Reading man files is sometimes a bit tricky for me - I do not read them I just search them and this particular note was not matched by my search at all ;)
    – ino
    Commented May 21, 2021 at 7:27
  • 1
    @ino You can use regular expressions in the man pager forward slash (/) search (actually the pager is less by default). They seem to be PCRE style too (the style used in most modern programming languages); man less just says "the regular expression library supplied by your system. I use ^\s+-x` a lot, which will find lines starting with some space then -x, so good for searching long lists of options. You can also use man -H to format it as html and send it to $BROWSER if available, sometimes easier to read.
    – goldilocks
    Commented May 21, 2021 at 15:16
34

What you need to do is specify which private key to use in the ~/.ssh/config file. for example:

Host server1.nixcraft.com
    IdentityFile ~/backups/.ssh/id_dsa
Host server2.nixcraft.com
    IdentityFile /backup/home/userName/.ssh/id_rsa
1
  • 1
    Thanks so much! @Steven You, I didn't realize that sshfs reads .ssh/config files (though I do see now that I missed a similar answer from someone's previous post, at stackoverflow.com/a/13638806/1940466). That's excellent to know. For what it's worth, I would like to accept both your and @goldilocks' answers, but I'm not able to. I also don't have enough reputation to vote up your answer. I very much appreciate the answer, though!
    – J L
    Commented Jan 17, 2013 at 4:08
10
sshfs -o ssh_command='ssh -i path/to/keyfile/id_rsa.2' username@ipaddress:/path /local/path
2
  • thanks this answer worked for me on Arch Linux for mounting android file system
    – Alex Jones
    Commented Jul 3, 2019 at 7:53
  • In my case (on macOS), I need to specify full path of key file. Commented May 8 at 7:46
4

Adding to the solution proposed by @Steven You, a simple solution is :

  1. go to ~/.ssh/config
Host <nick name>
    HostName  <ipaddress>
    User <username>
    PubKeyAuthentication yes
    IdentityFile <path/to/keyfile/id_rsa.2>
  1. sshfs -p 22 <nick name>:source/file/on/server local/folder/
1

From a live server.

Type this (with your modifications) in the shell (command line)

sudo sshfs -o allow_other,default_permissions,IdentityFile=/home/osmc/.ssh/id_rsa [email protected]:/home/pi/torrents/rtorrent /mnt/server/

If you want it to come up during a reboot then edit /etc/fstab

sudo nano /etc/fstab [email protected]:/home/pi/torrents/rtorrent /mnt/server fuse.sshfs delay_connect,rw,nosuid,nodev,default_permissions,allow_other,IdentityFile=/home/osmc/.ssh/id_rsa 0 0

Note the added delay_connect option for the /etc/fstab entry.

Also note that the id_rsa file is generated without a password.

Also note that this mount is first mounted manually to get the ssh fingerprint added to /home/osmc/.ssh/known_host

You must log in to answer this question.

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