How to fix ssh-add -K issue with Mac OS Sierra?


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.

  1. create a bash file containing these codes (~/.ssh/
count=`ssh-add -l |grep -v "The agent" |wc -l|awk '{print $1}'`
 if [ "0" == "${count}" ]
 arr=`cd ~/.ssh && find id_rsa* -not -path '*.pub'`
 for key in ${arr[@]}
 ssh-add -k ~/.ssh/${key}
  1. make this file executable:
chmod +x ~/.ssh/
  1. add this file to your Mac launch (~/Library/LaunchAgents/ssh_fixer.plist):
<plist version="1.0">
 <string>/Users/{YOUR USERNAME}/.ssh/</string>
  1. now restart your Mac!


That’s it! 😎