0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ssh-agentで登録された秘密鍵は、`.ssh/config`または`ssh -i`オプションで指定された秘密鍵より優先される(`IdentitiesOnly no`の場合)

Last updated at Posted at 2024-12-10

環境

  • 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)
~/.ssh/config
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/configIdentitiesOnly 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.

~/.ssh/config
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
...

参考サイト

  1. 仕事用のアカウントはGitHub Enterpriseによって追加されたアカウントです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?