あまり知られていない~/.ssh/configのIdentityFileの挙動
あまり知られてなさそうなのと、自分が完全に忘れていたのでメモっておきます。
IdentityFileの挙動
man ssh_configでみてみると
It is possible to have multiple identity files specified in configuration files; all these identities will be tried in sequence. Multiple IdentityFile directives will add to the list of identities tried (this behaviour differs from that of other configuration directives).
すなわち
IdentityFileは複数書ける- 書いた順に試行される
とのこと。
Host github.com HostName github.com User git IdentityFile ~/.ssh/github-foo.pem IdentityFile ~/.ssh/github-bar.pem
と書くと、github-foo.pem, github-bar.pemの順でログインできるか試行する挙動になるよう。
どういう場面で使える?
追記あり:Jenkinsにdeploy keysをリポジトリ毎に設定するのが面倒くさい - なっく日報 の方がいいかも
JenkinsでGithubと連携させるためにプロジェクト毎にdeploy keyを設定するときとか。
↑の回答にあるようにJenkinsサーバの~/.ssh/configにリポジトリ毎にエイリアスを設定して
Host a.github.com HostName github.com User git IdentityFile ~/.ssh/project-a-id_rsa Host b.github.com HostName github.com User git IdentityFile ~/.ssh/project-b-id_rsa
git clone git@a.github.com:yukidarake/project-a.git git clone git@b.github.com:yukidarake/project-b.git
みたいに書けます!という例をよく見かけますが、これは
Host github.com HostName github.com User git IdentityFile ~/.ssh/project-a-id_rsa IdentityFile ~/.ssh/project-b-id_rsa
こう書けると。
プロジェクト毎に秘密鍵を作って、~/.ssh/configに追加して、deploy keyに設定するという手順自体は変わりませんが、
git clone時にa.github.comみたいなエイリアスを使うという気持ち悪さからは開放されます。
まとめ
自分はこの書き方を使っていこうかと思ってますが、
何か指摘等あれば、コメントいただければ幸いです。