git pullしたら「Bad configuration option usekeychain」というエラーが表示された件

S 20231227 183452

MacBook Airで「git pull」を実行すると以下のようなエラーが表示された。

/Users/sora/.ssh/config: line 8: Bad configuration option: usekeychain
/Users/sora/.ssh/config: terminating, 1 bad configuration options
fatal: Could not read from remote repository.

gitのエラーではなくgitが内部で使っているsshのエラーだと思われる。
macOSのsshはキーチェーンを使ってパスフレーズを管理することができる便利なオプション「UseKeychain」をサポートしており、これまで特に問題なく使えていたので不思議に思った。

なかなかエラーの原因がわからなかったのだが、Homebrewでインストールしていたopensshパッケージのsshコマンドが使われていたのがエラーの理由だった。

Homebrewでインストールしたopenssh版のsshはUseKeychainをサポートしていない。MacOS: Installed openssh via brew, how do I add keys to keychain? – Unix & Linux Stack Exchangeでは次のように説明されている。

Keychain integration is a feature added by Apple that is not in the standard release of OpenSSH. As you have now installed the standard OpenSSH release from HomeBrew, you won’t have this functionality anymore. You have discovered this as the UseKeychain option now gives configuration errors – adding the IgnoreUnknown configuration only makes the error message go away, it won’t actually bring back any functionality.

In order to get Keychain integration, you will want to uninstall the HomeBrew version of OpenSSH and use the version supplied by Apple.

The version in HomeBrew will not for the foreseeable future have Keychain integration – it’s not that they can’t make it work at all, but rather that they have found the implementation that was made to be too big a risk for the project, as it’s a big change that doesn’t come from the OpenSSH project itself. You can read about that discussion here.

キーチェーンの統合は Apple によって追加された機能で、 OpenSSH の標準リリースにはありません。HomeBrewから標準のOpenSSHリリースをインストールしたので、この機能はもうありません。IgnoreUnknown設定を追加しても、エラーメッセージが消えるだけで、実際には何の機能も戻ってきません。

Keychainを統合するには、HomeBrew版のOpenSSHをアンインストールし、Appleが提供するバージョンを使う必要があります。

HomeBrewのバージョンは当分の間Keychainの統合ができないでしょう – まったく動作させることができないということではなく、OpenSSHプロジェクト自体から生まれたものではない大きな変更であるため、作られた実装がプロジェクトにとって大きすぎるリスクであることがわかったからです。その議論についてはこちらをご覧ください。

ようするにApple版のsshでないとUseKeychainが使えないということだ。

以下のような解決策が考えられる

  • 解決策1: PATHの設定で、/opt/homebrew/binの優先度を/usr/binより下げる。
  • 解決策2: PATHは/opt/homebrew/binを優先したまま。.ssh/configにIgnoreUnknownを追加。homebrewのsshを使うようにする(キーチェーンの使用は諦める)。
  • 解決策3: そもそもHomebrewでsshをインストールする必要はない。

今回は解決策3を採用し、「brew uninstall openssh」で単純にアンインストールした。そもそもHomebrew版opensshがどのタイミングでインストールされたのかは不明。