最近MacBook系统出了点问题,遂重装了最新的系统macOS Sierra。然鹅,问题来了。以前在终端中使用SSH,若要将key永久保存在本地,只需要执行ssh-add -K "KeyPath"
命令即可。而现在执行此命令虽然依旧能保存成功,但是在“钥匙串”中找不到保存的SSH密钥,也就是说并没有持久化保存到KeyChain中,重启后测试,果然ssh密钥清空了。
研究了一下,原来问题出在系统上,从macOS Sierra 10.12.2以后,SSH的配置选项中多了“UseKeychain”选项,在man ssh_config
页面中有这样的介绍:
UseKeychain
On macOS, specifies whether the system should search for
passphrases in the user's keychain when attempting to use a particular key. When the passphrase is provided by the user, this
option also specifies whether the passphrase should be stored
into the keychain once it has been verified to be correct. The
argument must beyes'' or
no''. The default is ``no''.
要解决这个问题,就要借助于UseKeychain
选项。在~/.ssh/config
文件中添加下面这段话,问题就可以解决了:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa