Save identities added by ssh-add so they persist

What is ssh-agent for and how does it work?

The ssh-agent keeps your decrypted keys securely in memory and in your session. There is no reasonable and safe way to preserve the decrypted keys among reboots/re-logins.

OK, how can I automate it?

Automate ssh-agent startup

Add

[ -z "$SSH_AUTH_SOCK" ] && eval "$(ssh-agent -s)"

to your ~/.bashrc or other startup script (~/.zshrc).

Automate adding the keys

The keys can be automatically added upon the first usage, when you add

AddKeysToAgent yes

to your ~/.ssh/config.

For more information on ~/.ssh/config see man ssh_config.


Add this to ~/.bashrc

This means ssh-agent will be started automatically when you open another session no your terminal

if [ -z "$SSH_AUTH_SOCK" ] ; then
 eval `ssh-agent -s`
fi

if you need a key to be added to the agent also add this

if [ -z "$SSH_AUTH_SOCK" ] ; then
 eval `ssh-agent -s`
 ssh-add ~/.ssh/<your private ssh key>
fi