Before Mac OS Sierra, using this line of code save your time adding ssh keys once in your keychain:
ssh-add -K ~/.ssh/{your key}
but recently there is some issue with Mac OS new version and all your keys added to keychain reset on startup.
to fix this issue I found a solution with might be helpful.
- create a bash file containing these codes (~/.ssh/add_ssh_keys.sh):
count=`ssh-add -l |grep -v "The agent" |wc -l|awk '{print $1}'` if [ "0" == "${count}" ] then arr=`cd ~/.ssh && find id_rsa* -not -path '*.pub'` for key in ${arr[@]} do ssh-add -k ~/.ssh/${key} done fi
- make this file executable:
chmod +x ~/.ssh/add_ssh_keys.sh
- add this file to your Mac launch (~/Library/LaunchAgents/ssh_fixer.plist):
<plist version="1.0"> <dict> <key>Label</key> <string>add_ssh_keys</string> <key>ProgramArguments</key> <array> <string>/Users/{YOUR USERNAME}/.ssh/add_ssh_keys.sh</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
- now restart your Mac!
That’s it! 😎