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

Standard

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/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
  1. make this file executable:
chmod +x ~/.ssh/add_ssh_keys.sh
  1. 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>
  1. now restart your Mac!

 

That’s it! 😎

Leave a Reply

Your email address will not be published. Required fields are marked *