SSH Agent loses identity while restart machine

It's normal. The purpose of a key agent is just to hold decrypted keys in memory, but it will never write them to disk. (That would defeat the purpose – why not just unprotect the main key instead?)

So the keys must be unlocked on each login, and you need to automate this – on Linux using pam_ssh is one option; it automatically uses your OS password to unlock the agent. Another similar module is pam_envoy, which is slightly more reliable afaik (but requires systemd).

Both modules will start the agent itself and load keys automatically.


Try to this to your ~/.bashrc:

if [ ! -S ~/.ssh/id_rsa ]; then
  eval `ssh-agent`
  ln -sf "$SSH_AUTH_SOCK" ~/.ssh/id_rsa
  ssh-add
fi
export SSH_AUTH_SOCK=~/.ssh/id_rsa

This should only prompt for the password once you are login.


On OS X, ssh-add has a special flag to connect to Keychain if you decide to store your private key in there.

Just run ssh-add -K ~/.ssh/id_rsa.

I believe this answers your question more fully. This OS X specific flag is hard to find documentation for but it's been working since at least OS X Leopard.