在终端上切换 Github 帐户时遇到问题 [英] Having Trouble Switching Github accounts on terminal

查看:24
本文介绍了在终端上切换 Github 帐户时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一段时间没有向 GitHub 推送任何内容了.我最初在我的电脑上设置了我的帐户,一切都很好.然后我将我的帐户更改为客户的帐户(因此我可以将代码推送到他们的私人存储库).

已经有一段时间了,现在我正在改回旧帐户,但遇到了问题.我生成了一个新的 rsa_key 并且几乎按照 此处的说明 到 T.

然而,当我输入:ssh -T git@github.com 我得到:

您好 oldincorrectusername!您已成功通过身份验证,但 GitHub 不提供 shell 访问权限.

我也无法推送到我的存储库,因为这个旧的客户端用户名未经授权.我已经在我的计算机和 GitHub 上的帐户设置中仔细检查了我的 ssh 密钥.

我还设置了我的全局帐户变量:

git config --global user.name "Firstname Lastname"git config --global user.email "your_email@youremail.com"git config --global github.user 用户名git config --global github.token 0123456789yourf0123456789token

它仍然给我旧的用户名.

有什么建议吗?

谢谢,

解决方案

问题是您本地的 ssh 仍在向 GitHub 提供您的旧"SSH 密钥.当您在 ssh-agent 中加载了一个 GitHub 识别的密钥(即您的旧"密钥)但想要使用另一个 GitHub 识别的密钥(即您的新"密钥)时,通常会出现这种情况).

ssh 按以下顺序提供密钥:

  1. 已加载到代理中的指定密钥
  2. 已加载到代理中的其他密钥
  3. 尚未加载到代理中的指定密钥

指定的键"是指由 -i 命令行选项或 IdentityFile 配置选项(可以通过 ~/.ssh/config-o 命令行选项).

如果您的旧"密钥已加载到代理中,但您的新"密钥未加载,那么 ssh 将始终在之前提供您的旧"密钥(来自第一或第二类)您的新"键(仅在最后一个类别中,因为它未加载),即使您使用 -i/IdentitiesOnly 指定新"键也是如此.><小时>

您可以使用 ssh-add -l 检查您的 ssh-agent 中加载了哪些密钥.如果列出了您的旧"密钥,那么您可以通过从代理卸载它来解决问题(请务必同时卸载任何其他 GitHub 识别的密钥,也许您的新"密钥除外):

ssh-add -d ~/.ssh/old_key_file

如果您使用的是 Mac OS X,系统可能会在提示输入密码时选中记住我的钥匙串中的密码",系统可能会自动加载您的旧"密钥;您可以通过使用命令删除密钥的钥匙串条目来禁用此自动加载
/usr/bin/ssh-add -K -d ~/.ssh/old_key_file.其他系统可能会做类似的事情,但告诉它们停止"的命令会有所不同.

<小时>

您可以将 IdentitiesOnly 配置选项设置为 yes,而不是从您的代理卸载旧"密钥,以告诉 ssh跳过第二类键(非指定代理加载的键).您的 ~/.ssh/config 可能包含这样的部分:

托管 github.com用户 gitIdentityFile ~/.ssh/id_rsa # 无论你的新"密钥在哪里身份只有是

这样,任何其他 GitHub 识别的密钥是否加载到您的代理中都无关紧要;ssh 将始终仅提供您的新"密钥.

如果您预计需要访问两个 GitHub 帐户的存储库,并且不想在想要在 GitHub 帐户之间切换时编辑配置文件,那么您可以设置您的 ~/.ssh/config 像这样:

Host clientname.github.com主机名 github.comIdentityFile ~/.ssh/client_id_rsa # 或旧"客户端密钥所在的任何位置托管 github.comIdentityFile ~/.ssh/id_rsa # 或者你的新"密钥所在的任何地方主机 github.com *.github.com用户 git主机名 github.com身份只有是

然后使用像 github.com:GitHubAccount/repository 这样的 URL 作为您的存储库,使用像 clientname.github.com:GitHubAccount/repository 这样的 URL 作为您客户的存储库(您可以如果您愿意,可以将 git@ 前缀放回原处,但这不是必需的,因为上述条目设置了 User 配置变量).

It's been a while since I pushed anything to GitHub. I had initially set up my account on my computer, and everything worked great. Then I changed my account to a client's account (so I could push code to their private repository).

It's been a while and now I am changing back to my old account, and I am having trouble. I generated a new rsa_key and pretty much followed the instructions here to a T.

However, when I type: ssh -T git@github.com I get:

Hi oldincorrectusername! You've successfully authenticated, but GitHub does not provide shell access.

I can't push to my repos either, because this old client username isn't authorized. I've doublechecked my ssh keys both on my computer and on my account setting on GitHub.

I've also set my global account variables:

git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@youremail.com"
git config --global github.user username
git config --global github.token 0123456789yourf0123456789token

And still it is giving me the old username.

Any suggestions?

Thanks,

解决方案

The problem is that your local ssh is still offering your "old" SSH key to GitHub. This often comes up when you have one GitHub-recognized key (i.e. your "old" key) loaded in an ssh-agent but want to use a different GitHub-recognized key (i.e. your "new" key).

ssh offers keys in this order:

  1. specified keys that have been loaded into the agent
  2. other keys that have been loaded into the agent
  3. specified keys that have not been loaded into the agent

By "specified keys" I mean those keys specified by the -i command line option or the IdentityFile configuration option (which can be given through ~/.ssh/config or the -o command line option).

If your "old" key is loaded into the agent, but your "new" key is not, then ssh will always offer your "old" key (from the first or second categories) before your "new" key (only ever in the last category since it is not loaded), even when you specify your "new" key with -i/IdentitiesOnly.


You can check which keys are loaded in your ssh-agent with ssh-add -l. If your "old" key is listed, then you can fix the problem by unloading it from your agent (be sure to also unload any other GitHub-recognized keys, except perhaps your "new" key):

ssh-add -d ~/.ssh/old_key_file

If you are using Mac OS X, the system may be automatically loading your "old" key if you checked "Remember password in my keychain" when prompted for the password at one point; you can disable this automatic loading by deleting the Keychain entry for the key with the command
/usr/bin/ssh-add -K -d ~/.ssh/old_key_file. Other systems may do something similar, but the commands to tell them to "stop that" will be different.


Instead of unloading the "old" key from your agent, you can set the IdentitiesOnly configuration option to yes, to tell ssh to skip the second category of keys (non-specified agent-loaded keys). Your ~/.ssh/config might include a section like this:

Host github.com
    User           git
    IdentityFile   ~/.ssh/id_rsa # wherever your "new" key lives
    IdentitiesOnly yes

This way, it will not matter whether any other GitHub-recognized keys are loaded into your agent; ssh will always offer only your "new" key.

If you anticipate needing to access the repositories of both GitHub accounts and you do not want to have to edit the configuration file whenever you want to switch between GitHub accounts, then you might setup your ~/.ssh/config like this:

Host clientname.github.com
    HostName      github.com
    IdentityFile ~/.ssh/client_id_rsa  # or wherever your "old" client key lives

Host github.com
    IdentityFile   ~/.ssh/id_rsa        # or wherever your "new" key lives

Host github.com *.github.com
    User           git
    Hostname       github.com
    IdentitiesOnly yes

Then use URLs like github.com:GitHubAccount/repository for your repositories and URLs like clientname.github.com:GitHubAccount/repository for your client’s repositories (you can put the git@ prefix back in if you like, but it is not necessary since the above entries set the User configuration variable).

这篇关于在终端上切换 Github 帐户时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆