macOS Sierra 10.12.2 ssh每次提示输入密码的问题

一直使用key+passphrase的方式ssh登录服务器。早期OSX 10.10和OSX 10.11的时候,第一次使用会提示输入密码,并且是以弹出框的形式,里面可以选择保存密码到keychain。这样以后都不用再输密码了。

但是在升级到macOS Sierra 10.12.2之后,每次都提示要我输入密码,甚是烦人:

1
2
$ ssh foo
Enter passphrase for key '/Users/YONG/.ssh/id_rsa_bar':

谷歌了一下,找到这篇文章 OpenSSH updates in macOS 10.12.2,其中说到:

Prior to macOS Sierra, ssh would present a dialog asking for your passphrase and would offer the option to store it into the keychain. This UI was deprecated some time ago and has been removed.

Instead, a new UseKeychain option was introduced in macOS Sierra allowing users to specify whether they would like for the passphrase to be stored in the keychain. This option was enabled by default on macOS Sierra, which caused all passphrases to be stored in the keychain.

This was not the intended default behavior, so this has been changed in macOS 10.12.2.

里面同时提到了新版的保存密码到keychain的方法:

1
2
3
4
5
6
7
8
9
10
11
12
$ vim ~/.ssh/config
# 针对所有的HOST
Host *
UseKeychain yes

#或者仅针对某个HOST
Host xx
HostName xxx.xxx.xxx.xxx
Port 22
User xxxx
IdentityFile ~/.ssh/id_rsa_xx
UseKeychain yes

问题完美解决。