環境
- Debian GNU/Linux 12 (bookworm)
$ ssh -V
OpenSSH_9.2p1 Debian-2+deb12u3, OpenSSL 3.0.15 3 Sep 2024
やりたいこと
複数のGitHubアカウントを使い分けたいです。
普段使っているGitHubアカウント(yuji38kwmt)に加えて、仕事用のGitHubアカウント(work-account)が追加されました。1
ハマったこと
ssh-agentに普段用GitHubアカウントの秘密鍵が登録されている状態で、仕事用GitHubaアカウントの秘密鍵を.ssh/configに設定しました。
$ ls ~/.ssh -1
id_rsa_github # 普段用のGitHubアカウントの秘密鍵。公開鍵は登録済。
id_rsa_github_work # 仕事用のGitHubアカウントの秘密鍵。公開鍵は登録済。
$ ssh-add -l
4096 SHA256:XXXXXXXXXXXXXXXXXX /home/yuji/.ssh/id_rsa_github (RSA)
Host github-work
HostName github.com
IdentityFile ~/.ssh/id_rsa_github_work
User git
~/.ssh/configや-iオプションで指定した秘密鍵よりも、ssh-agentで登録された秘密鍵が優先されました。
$ ssh -T git@github.com
Hi yuji38kwmt! You've successfully authenticated, but GitHub does not provide shell access.
# `.ssh/config`で指定した秘密鍵は使われない
$ ssh -T git@github-work
Hi yuji38kwmt! You've successfully authenticated, but GitHub does not provide shell access.
# `-i`で指定した秘密鍵は使われない
$ ssh -T git@github-work -i ~/.ssh/id_rsa_github_work
Hi yuji38kwmt! You've successfully authenticated, but GitHub does not provide shell access.
~/.ssh/configを設定した時点では、ssh-agentの存在が頭になかったため、なぜ秘密鍵id_rsa_github_workが使われないのかが分からず、原因調査に時間がかかってしまいました。
なお、-vオプションでデバッグログを出力すれば、「id_rsa_githubはagentに登録されていて、それを採用している」ことが分かりました。
$ ssh -T git@github-work -v
...
debug1: Will attempt key: /home/yuji/.ssh/id_rsa_github RSA SHA256:xxxxxx agent
debug1: Will attempt key: /home/yuji/.ssh/id_rsa_github_work RSA SHA256:xxxxxx explicit
...
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/yuji/.ssh/id_rsa_github RSA SHA256:XXXXXX agent
debug1: Server accepts key: /home/yuji/.ssh/id_rsa_github RSA SHA256:XXXXXX agent
解決策
.ssh/configにIdentitiesOnly yesを指定すれば、ssh-agentに普段用GitHubアカウントの秘密鍵が登録されていても、仕事用GitHubアカウントの秘密鍵が使われました。
IdentitiesOnly Specifies that ssh(1) should only use the configured authentication identity and certificate files (either the default files, or those explicitly configured in the ssh_config files or passed on the ssh(1) command-line), even if ssh-agent(1) or a PKCS11Provider or SecurityKeyProvider offers more identities. The argument to this keyword must be yes or no (the default). This option is intended for situations where ssh-agent offers many different identities.
Host github-work
HostName github.com
IdentityFile ~/.ssh/id_rsa_github_work
User git
IdentitiesOnly yes
$ ssh-add -l
4096 SHA256:XXXXXX .ssh/id_rsa_github (RSA)
$ ssh -T git@github-work
Hi work-account! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@github-work -v
...
debug1: Will attempt key: /home/yuji/.ssh/id_rsa_github_work RSA SHA256:XXXXXX explicit
debug1: SSH2_MSG_EXT_INFO received
...
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/yuji/.ssh/id_rsa_github_woven RSA SHA256:XXXXXX explicit
debug1: Server accepts key: /home/yuji/.ssh/id_rsa_github_woven RSA SHA256:XXXXXX explicit
...
参考サイト
-
仕事用のアカウントはGitHub Enterpriseによって追加されたアカウントです。 ↩