Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I understand that since Mac OS X Leopard the Keychain has supported storing SSH keys. Could someone please explain how this feature is supposed to work.

I have some RSA keys that I've generated stored in my ~/.ssh directory for accessing various servers. I don't have passphrases set on those keys. Currently in order to log in to those servers I use the following commands in the Terminal:

eval `ssh-agent`
ssh-add ~/.ssh/some_key_rsa
ssh user@server

(I've written some Bash functions to make this easier.)

Is there a better way to do this using the Keychain?

share|improve this question

6 Answers 6

up vote 9 down vote accepted

For it to work, the $SSH_AUTH_SOCK environment variable should be pointed to /tmp/launch-xxxxxx/Listeners. This is supposed to be done automatically when you log in. The listener on that socket speaks the ssh-agent protocol.

Your bash scripts are starting your own ssh agent (spelled ssh-agent, not ssh_agent) and overriding the existing ssh-agent that is set up for you at login.

Also, the whole point of the keychain is to store the passwords to your ssh keys, but you say that you don't have passphrases set on those keys, so I'm not sure what you are expecting out of the keychain integration.

Finally, when you first log in, you probably won't see a ssh-agent process. That process will be started automatically by launch services the first time something tries to read that socket in /tmp.

share|improve this answer
1  
Thanks. So I'll still have to run ssh-add to add my RSA identities to the default ssh agent started at log in? –  John Topley Dec 28 '09 at 18:01
    
You don't have to run ssh-add; ssh-agent will ask for the key's passphrase the first time you run ssh. –  Rudedog Dec 29 '09 at 5:46
1  
It's prompting me for the password every time I run ssh. The point is that I don't want to have to enter any passwords. –  John Topley Dec 29 '09 at 10:40
    
What is prompting you for the password? I'm beginning to suspect that it's the remote server that's prompting you, which puts your statement that your keys don't have passphrases in better perspective. If you want to bypass the password on the remote server, you need to add your public key to $HOME/.ssh/authorized_keys on that server. Mac OS' ssh-agent+keychain is only used to store the passphrase for your local ssh keys; it is not meant to send remote passwords over existing ssh connections. –  Rudedog Dec 29 '09 at 16:08
1  
use ssh -v to diagnose what ssh is doing. also use sshd -p 8900 -v on the server side and ssh -v remote:8900 to diagnose what sshd is doing. –  Rudedog Dec 30 '09 at 16:54

As of the Leopard release of OS X, ssh-agent is more tightly integrated with Keychain. It is possible to store the passphrases of all of your SSH keys securely in Keychain, from which ssh-agent will read them on startup. The bottom line is that it is simple to secure your keys with passphrases, but never have to type the passphrase to use them! Here is how:

Add the pass phrase to each ssh key to keychain:

ssh-add -K [path/to/private SSH key]

Whenever you reboot your Mac, all the SSH keys in your keychain will be automatically loaded. You should be able to see the keys in the Keychain Access app, as well as from the command line via:

ssh-add -l
share|improve this answer
7  
This should be the accepted answer. –  Kris Jul 24 '12 at 7:29
1  
the accepted answer answers this specific user's question, but this answers the generic user's question –  eqzx Dec 1 '12 at 19:23
    
this is the best answer –  RichVel Mar 17 '13 at 10:36

The answer by Jeff McCarrell is correct, except that the command to add the pass phrase contains an en dash instead of a hyphen, i.e. –K instead of -K, causing a message to the effect of –K: No such file or directory. It should read:

ssh-add -K [path/to/private SSH key]
share|improve this answer

I suspect you aren't using the default ssh command. Do you have ssh installed via ports? Try which ssh to see which ssh command you are using.

Usually it should display a dialog box asking for you password, if it isn't already stored in you keychain.

share|improve this answer
    
I'm not using ports. –  John Topley Dec 28 '09 at 14:56
    
Thanks for the info :) I had problems because i was using OpenSSH from Homebrew. –  ggustafsson Oct 6 '12 at 13:47

See also:

security import priv_key.p12 -k ~/Library/Keychains/login.keychain
security import pub_key.pem -k ~/Library/Keychains/login.keychain

... adding this note as more detail was requested: the "security" command is capable of importing keys (and other things) directly into Keychains. The nice thing is that unlike ssh-add, you are able to specify the keychain. This makes it possible to import directly into the system Keychain ("man security" to learn how)

share|improve this answer
1  
Could you give a bit more detail to this answer please? Thanks. –  Matthew Williams Mar 26 at 10:14

I had a similar problem while trying to login using a client ssh cert. In this specific case it was for accessing a git repository. This was the situation:

  • Key was saved in ~/.ssh/
  • The private key has a passphrase.
  • The passphrase is stored in the OS X login keychain. ~/Library/Keychains/login.keychain
  • The connection was as follows: my mac -> remote mac -> git/ssh server
  • Mac OS X 10.8.5

When I connected to remote mac using remote desktop, I didn't have a problem. However when connecting with SSH to the remote mac, I was asked for the ssh passphrase every time. The following steps solved it for me.

  1. security unlock-keychain The passphrase is stored in the login keychain. This unlocks it and enables ssh-agent to access it.
  2. eval `ssh-agent -s` Starts ssh-agent for shell use. It will get the passphrase from the keychain and use it to unlock the private ssh key.
  3. Establish the ssh/git connection and do my work.
  4. eval `ssh-agent -k` Kill the running ssh-agent.
  5. security lock-keychain Lock the keychain again.
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.