启用 2FA 后 Git 身份验证失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25550481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:23:35  来源:igfitidea点击:

Git authentication fails after enabling 2FA

gitgithubtwo-factor-authentication

提问by Max Bileschi

I just enabled 2FA (I can't think of any other changes I made) and git asked for my username and password. I provided both, but they were "wrong". I tried many of the solutions here: Git push requires username and passwordbut that didn't work. In particular, when switching from https to ssh, the ssh key gives

我刚刚启用了 2FA(我想不出我所做的任何其他更改)并且 git 要求我提供用户名和密码。我提供了两者,但它们是“错误的”。我在这里尝试了许多解决方案:Git push 需要用户名和密码,但这没有用。特别是,当从 https 切换到 ssh 时,ssh 密钥给出

Permission denied (publickey). fatal: Could not read from remote repository.

权限被拒绝(公钥)。致命:无法从远程存储库读取。

$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'https://github.com': **********
Password for 'https://[email protected]': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/mlbileschi/scala.git/'

Any tips?

有小费吗?

回答by Gergo Erdosi

You need to generate an access token. You can create one by going to your settings page.

您需要生成访问令牌。您可以通过转到您的设置页面来创建一个。

enter image description here

在此处输入图片说明

Use this access token as your password in the command line.

在命令行中使用此访问令牌作为密码。

回答by RayLuo

An end-to-end solution takes 3 steps.

端到端解决方案需要 3 个步骤。

  1. Kudos to Gergo Erdosi. His answer is largely right, it is just that Github changes that setting page. As of late 2016, you need to generate an access tokenfrom your Personal access tokens page.

    enter image description here

    Use this access token as your password in the command line.

  2. You can persist your user name by including it into your project remote url. One of the way to do it is to edit your .git/configto modify the urlline into the following format:

    url = https://[email protected]/owner/repo.git

  3. You can persist your password by run this for one time only:

    $ git config credential.helper store

    and then your future git password(s) will be stored in ~/.git-credentials, in plaintext, using the format https://user:[email protected].

    Storing password(s) in plaintext would normally be considered as a security risk. But in this 2FA case, the credential is NOT your real password, it is a randomly generated string. So it is as secure as using a ssh private keya passphrase-less ssh private key. CAVEAT: keep in mind that, if you happen to also use another git account(s) without 2FA on this machine, those real password(s) will also be stored in plaintext.

  1. 向 Gergo Erdosi 致敬。他的回答基本正确,只是 Github 更改了该设置页面。截至 2016 年底,您需要从您的个人访问令牌页面生成访问令牌

    在此处输入图片说明

    在命令行中使用此访问令牌作为密码。

  2. 您可以通过将您的用户名包含在您的项目远程 url 中来保留您的用户名。一种方法是编辑您.git/config要修改的url行为以下格式:

    url = https://[email protected]/owner/repo.git

  3. 您可以通过仅运行一次来​​保留您的密码:

    $ git config credential.helper store

    然后你未来的 git 密码将使用格式以明文形式存储在 ~/.git-credentials 中https://user:[email protected]

    以明文形式存储密码通常会被视为存在安全风险。但在这种 2FA 情况下,凭据不是您的真实密码,而是随机生成的字符串。因此,它与使用ssh 私钥和无密码ssh 私钥一样安全。警告:请记住,如果您碰巧在这台机器上还使用了另一个没有 2FA 的 git 帐户,那么这些真实密码也将以明文形式存储。

PS: Alternatively, you could choose to use ssh-based login, using a passphrase-protected ssh private key, which would be more secure and less convenient, but it is outside the scope of this answer.

PS:或者,您可以选择使用基于 ssh 的登录,使用密码保护的 ssh 私钥,这会更安全,更不方便,但这不在本答案的范围内。

回答by Jester

I had a similar problem. I had to alter the url used in the git command to include my username.

我有一个类似的问题。我必须更改 git 命令中使用的 url 以包含我的用户名。

git push https://[email protected]/mlbileschi/scala.git

Then when it asks for PW use the access token you created from following the instructions in Gergo Erdosi's answer.

然后,当它要求 PW 时,使用您按照 Gergo Erdosi 的回答中的说明创建的访问令牌。

回答by Jossef Harush

On linux, you can authenticate your GitHub identity using an SSH key.

在 Linux 上,您可以使用 SSH 密钥验证您的 GitHub 身份。

1) Generating a new SSH key (source)

1) 生成新的 SSH 密钥(来源

Open terminal.

打开终端。

Paste the text below, substituting in your GitHub email address.

粘贴下面的文本,替换为您的 GitHub 电子邮件地址。

ssh-keygen -t rsa -b 4096 -C "[email protected]"

This creates a new ssh key, using the provided email as a label.

这将创建一个新的 ssh 密钥,使用提供的电子邮件作为标签。



2) Linking the key to your GitHub account

2) 将密钥链接到您的 GitHub 帐户

Open terminal and copy the generated public key

打开终端并复制生成的公钥

cat ~/.ssh/id_rsa.pub

Should output somthing as

应该输出一些东西

ssh-rsa AAAAB3NzaC1y ... mKAKw== [email protected]

Navigate to https://github.com/settings/keysand click New SSH Key, give it a Title and copy-paste the public key.

导航到https://github.com/settings/keys并单击New SSH Key,给它一个标题并复制粘贴公钥。

enter image description here

在此处输入图片说明



3) Change git origin from https://to ssh

3) 将 git origin 从 更改https://ssh

Open terminal, cdto your repository location and type

打开终端,cd到您的存储库位置并键入

git remote set-url origin [email protected]:<github username>/<repository name>

回答by imsinu9

This worked for me:

这对我有用:

  • Go to [your-git-repo]/.git/config

  • Under [remote "origin"]Change the URLkey from http protocol to git.

  • 转到 [your-git-repo]/.git/config

  • [remote "origin"]URL密钥从 http 协议更改为 git 下。

Example

例子

If the value of urlis https://github.com/.git change it to [email protected]:<repo-url>.git

如果值urlhttps://github.com/git的将其更改为[email protected]:<repo-url>.git

回答by Sakhi Mansoor

If you are already using ssh keys, after enabling 2FA it will enforce you to read/write remote with SSH. You don't really need to add personal tokens rather keep using your existing SSH key pair.

如果您已经在使用 ssh 密钥,则在启用 2FA 后,它将强制您使用 SSH 远程读/写。您实际上并不需要添加个人令牌,而是继续使用现有的 SSH 密钥对。

Just change your remote url from HTTPS to SSH:

只需将您的远程 URL 从 HTTPS 更改为 SSH:

git remote set-url origin [email protected]:<github-username>/<repo-name>

回答by ravikcm

This worked for me after enabling 2FA on Github:

在 Github 上启用 2FA 后,这对我有用:

  1. Create a Personal Access Token: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line.
    NOTE: Remember to select the correct permissions for the token.

  2. Run:
    git clone https://github.com/username/repo.git
    Username: your_username
    Password: your_token

  1. 创建个人访问令牌:https: //help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
    注意:请记住为令牌选择正确的权限。

  2. 运行:
    git clone https://github.com/username/repo.git
    用户名:your_username
    密码:your_token

Further reading: https://help.github.com/en/github/using-git/which-remote-url-should-i-use

进一步阅读:https: //help.github.com/en/github/using-git/which-remote-url-should-i-use

回答by Milind

I was facing this problem on existing repo when I enabled 2FA(two factor authentication) for one of my private repos. I was able to solve it following below steps on my ubuntu 19.0 terminal:-

当我为我的一个私人存储库启用 2FA(双因素身份验证)时,我在现有存储库上遇到了这个问题。我能够在我的 ubuntu 19.0 终端上按照以下步骤解决它:-

  1. Add your ssh key to githubso that you dont need to use your password again,as now you have enabled 2FA.Visit github pageto know how to do it easily.
  2. Once key is added, go to your terminal, and update the origin url

    git remote set-url origin [email protected]:<USERNAME>/<BRANCH-NAME>

  1. 将您的 ssh 密钥添加到github,这样您就不需要再次使用您的密码,因为现在您已经启用了 2FA。访问 github 页面以了解如何轻松完成。
  2. 添加密钥后,转到您的终端,并更新原始 url

    git remote set-url origin [email protected]:<USERNAME>/<BRANCH-NAME>

Thats it.Hope it helps

就是这样。希望它有帮助