4

I want to use different private key files to connect to different SSH servers (ssh://git-server:port/repository). How to manage that? SSH seems to assume the private key file in ~/.ssh/id_rsa.

2 Answers 2

6

That's why there's the -i option:

-i identity_file

Selects a file from which the identity (private key) for RSA or DSA authentication is read. The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for protocol version 2.

Identity files may also be specified on a per-host basis in the configuration file. It is possible to have multiple -i options (and multiple identities specified in configuration files).

If you want to make this permanent, you need to setup your SSH config file and set the according IdentityFile option.

IdentityFile

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

If you see a dialog similar to this:

enter image description here

You need to enter your user password for your OS X account. Not your key passphrase, or the remote SSH password. This is because they identity hasn't been stored in the keychain.

According to this tutorial, the following command should add them appropriately:

ssh-add -K
9
  • OK, I've done that now, but when trying to log in, OS X asks in a separate dialog for the password instead of passphrase. I'm entering the correct passphrase for the private key files, but it still can't log in. Any further ideas?
    – Mike L.
    Nov 22, 2011 at 15:29
  • If it asks you for the password, that's the SSH password on the remote machine, not the passphrase for the key.
    – slhck
    Nov 22, 2011 at 15:32
  • No, no, it asks me for the password for my private key file - at least that's what the dialog is saying. BTW, without the passphrase SSH could not do anything on the remote machine.
    – Mike L.
    Nov 22, 2011 at 15:49
  • Hm. I'm not an expert in SSH, I have to admit. What is that dialog saying exactly?
    – slhck
    Nov 22, 2011 at 15:59
  • I've configured IdentityFile = ~/secret/pk-file in ~/.ssh/config and invoke ssh myserver. The occurring dialog says Enter your password for the SSH key "pk-file"., shows a Password input field and two checkboxes (Show password and Remember passwod in my keychain). I'm entering the correct passphrase, but I'm asked 2 more times and then SSH aborts with RSA host key for IP address '1.2.3.4' not in list of known hosts. Permission denied (publickey). (1.2.3.4 is dummy).
    – Mike L.
    Nov 23, 2011 at 8:34
3

The easiest way to accomplish this is with a ssh config file.

cat ~/.ssh/config

HOST *  
     USER root

You can also specify certain subdomains use certain users. Useful if your laptop travels between networks.

HOST 192.168.*.*
     USER homeuser

HOST 10.2.*.*
     USER workuser

You could even configure by domains, and use different ssh keys for different domains.

HOST *.microsoft.com
     USER bill
     IdentityFile ~/.ssh/microsoft/id_rsa

HOST *.apple.com
     USER steve
     IdentityFile ~/.ssh/apple/id_rsa

You must log in to answer this question.

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