【问题标题】:SSH using old possibly cached user for connecting to GitHubSSH 使用可能缓存的旧用户连接到 GitHub
【发布时间】:2021-04-26 04:46:32
【问题描述】:

Mac OS (High Sierra) 在这里。我有一个 SSH 密钥 example-gh-user,当我运行 pbcopy < ~/.ssh/example-gh-user.pub 并将其粘贴到编辑器中时,我看到了:

ssh-ed25519 <SOME_REALLY_LONG_ALPHANUM> me@example.org

显然出于安全原因,我将&lt;SOME_REALLY_LONG_ALPHANUM&gt;me@example.org实际 值替换为上面的虚拟值。但一切看起来都不错。

在 GitHub 上,我有一个用户 example-gh-user,正在使用 me@example.org 电子邮件,我可以使用该用户登录并查看我的所有存储库。一切看起来都很好。

我已关注GitHub guide on adding an SSH key 到我的example-gh-user GitHub 帐户。因此,此 SSH 密钥应与我的 GH 帐户所关联的同一电子邮件地址相关联。

在我的~/.ssh/config 文件中,我有以下条目:

Host example-dev
  Hostname   github2.com
  AddKeysToAgent yes
  IdentityFile ~/.ssh/example-gh-user

Host github.com
  User git
  Hostname   github.com
  AddKeysToAgent yes
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/example-gh-user

到目前为止,我认为还不错。

但是,当我从命令行运行以下命令时:

ssh -Tv git@github.com

我得到了一个巨大的输出转储,有趣的是:

debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi my-old-gh-user! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3452, received 2724 bytes, in 0.1 seconds
Bytes per second: sent 28403.1, received 22413.1
debug1: Exit status 1

看那个! my-old-gh-user!那是我没用过的GH用户!

SSH 和/或 git/GitHub 从哪里获取 my-old-gh-user?! SSH 是否以某种方式缓存旧数据?如果我了解一切如何正常工作,~/.ssh/config 明确指示 SSH 在以git 用户身份连接到github.com 时使用~/.ssh/example-gh-user,对吗?我哪里出错了?

【问题讨论】:

  • 感谢@JimB (+1) 的建议,我会试试的。听起来你想让我尝试从回购根目录运行git config --global url.git@github.com:.insteadOf https://github.com/?只是好奇那会在引擎盖下做什么?
  • 您可以在链接的答案中看到它“在幕后”做了什么,它在 git 配置中添加了一个 insteadOf 条目。 insteadOf 的完整描述可以在文档中找到,但基本上是;使用 ssh 而不是 https。
  • 好的,我在 repo 的根目录中运行了git config --global url.git@github.com:.insteadOf https://github.com/,然后尝试了go build,但得到了完全相同的错误。知道接下来我可以解决什么问题吗?
  • 它告诉我这与 Go 无关,你需要修复你的 git+ssh 配置。

标签: git macos github ssh ssh-config


【解决方案1】:

如何知道当前最新的 some-service/client 版本是什么,以及如何更新 go.mod 以使用它?

只需go get github.com/SomeExampleOrg/some-service/client(假设原行还在go.mod

这会将go.mod 更新为该存储库的最新版本,关于客户端的 go.mod 行将显示可用的最新版本。

如果该存储库是公共存储库,Go 将使用正常的 HTTPS URL,并且不需要您的 SSH 配置。


TheOP 在讨论中提到

ssh -Tv git@github.com` generates the following output:

Hi my-old-gh-user! You've successfully authenticated, but GitHub does not provide shell access. 

my-old-gh-user 是我多年未使用的老 GH 用户,必须为其配置 SSH。

SSH/GitHub 是从哪里获取 my-old-gh-user 的?

默认情况下,ssh 会查找~/.ssh/id_rsa(.pub)。如果旧的 public kay 注册到旧的 GitHub 用户帐户 SSH 配置文件设置(如 documented by GitHub),这将解释为什么 SSH 默认使用旧的 GitHub 帐户识别您。

在您的新帐户中注册一个新的公钥,并确保用您的新密钥(或use an ~/.ssh/config file)替换默认公钥,SSH 将使用新帐户验证您的身份。

我生成了~/.ssh/example-gh-user

然后你需要添加到你的~/.ssh/config文件:

Host newgh
    HostName github.com
    User git
    IdentityFile ~/.ssh/example-gh-user

然后

ssh -Tv newgh

cd /path/to/repo
git remote set-url origin newgh:<me>/<repo>

【讨论】:

  • 感谢@VonC (+1) 的精彩回答,不幸的是,我错误地加粗了一个问题(你在回答中引用的那个)。我已经编辑了我的问题以正确识别此处提出的 real 问题;如果你有任何想法,我很想听听他们的意见!再次感谢!
  • @hotmeatballsoup 我了解,但是您是否按照我的建议先尝试更新依赖项?也许这样可以避免您在问题的第二部分中遇到的问题。
  • @hotmeatballsoup 如果没有,请查看stackoverflow.com/a/65850333/6309
  • 再次感谢@VonC(两者都+1)。当我运行go get github.com/SomeExampleOrg/some-service/client 时,我得到go: github.com/SomeExampleOrg/some-service/client@v0.8.0: reading github.com/SomeExampleOrg/some-service/client/client/go.mod at revision client/v0.8.0: unknown revision client/v0.8.0
  • 看你上面贴的链接,我相信相关信息是在我的~/.bash_profile中设置GO111MODULE=onGOPRIVATE=github.com/SomeExampleOrg,我已经做到了,但问题仍然存在。
【解决方案2】:

这个有趣场景的答案在于 ssh-agent 的工作方式以及在 MacOS 的钥匙串上缓存身份(SSH 密钥)的方式。

您可以通过ssh-add -L 获取所有当前加载的身份,其中您应该看到旧的 git ssh 用户的身份。

您可以使用ssh-add -Dssh-add -d 刷新身份或使用以下方法重新加载ssh-agent

eval "$(ssh-agent -s)"
ssh-add

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2014-01-05
    • 2020-10-30
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多