LoginSignup
1
2

More than 3 years have passed since last update.

githubなどにsshを行ったとき、configや -i でidentity_fileを指定したにもかかわらず別のものが使われてしまう場合の対処法

Last updated at Posted at 2020-02-13

結論

ssh-agentが適用されてしまっていると、それが優先度が一番高くなってしまう。

ケース

私の場合SourceTreeで、複数のGithubアカウント(会社用と個人用)のリポジトリを両方使っているとしばしば発生します。pushやpullなどリポジトリアクセスが必要な操作をしたときにエラーになります。

git --no-optional-locks -c color.branch=false -c color.diff=false -c color.status=false -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree push -v --tags origin refs/heads/[branch名]:refs/heads/[branch名]
Pushing to [リポジトリのパス].git
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Completed with errors, see above

原因

ssh-agentのせい

SSH接続の優先度は
1. keys from the agent (ssh-agentの設定)
2. keys from the config file (ssh configの設定)

opensshの該当ソース
https://github.com/openssh/openssh-portable/blob/master/sshconnect2.c#L1622

確認方法

-vをつけてssh

ssh git@github.com -v
OpenSSH_7.9p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
・・・
debug1: Will attempt key: {Key} RSA SHA256:{フィンガープリント} agent <= これ
debug1: Will attempt key: /Users/hogefuga/.ssh/id_rsa
debug1: Will attempt key: /Users/hogefuga/.ssh/id_dsa
debug1: Will attempt key: /Users/hogefuga/.ssh/id_ecdsa
debug1: Will attempt key: /Users/hogefuga/.ssh/id_ed25519
debug1: Will attempt key: /Users/hogefuga/.ssh/id_xmss
・・・
debug1: Exit status 1

Will attempt key の行の末尾に agent が含まれていれば、ssh-agentの設定が適用されています。ログを追っていくと{Key}のidetity_fileが最終的に使われているのがわかります。

ssh-agent

ssh-agentについてはこちら。
https://euske.github.io/openssh-jman/ssh-agent.html

ssh-agentの状況を確認

$ ssh-add -l
2048 SHA256:{フィンガープリント} {Key} (RSA)

やはり設定されていました、いつの間にかSourceTreeを使っているとなります

対処法

ssh-add -D

もう一度sshすると、今度はagentの行がなくなり、configの設定が適用されるはずです。

恒久的対処法

.ssh/configのHost設定中に AddKeysToAgent yes があると、この設定を使うたびにssh-agentに追加されてしまうので注意。削除しましょう。
SourceTreeによって生成されたconfg設定にこの行が存在します。

以上

1
2
1

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
1
2