Bitbucket 上的 Git:总是要求输入密码,即使在上传了我的公共 SSH 密钥之后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8600652/
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-10 12:34:41  来源:igfitidea点击:

Git on Bitbucket: Always asked for password, even after uploading my public SSH key

gitsshbitbucket

提问by Nicolas Raoul

I uploaded my ~/.ssh/id_rsa.pubto Bitbucket's SSH keysas explained, but Git still asks me for my password at every operation (such as git pull). Did I miss something?

我按照解释将我的 SSH 密钥上传~/.ssh/id_rsa.pubBitbucket,但 Git 仍然在每次操作(例如)时询问我的密码。我错过了什么?git pull

It is a private repository (fork of another person's private repository) and I cloned it like this:

它是一个私有存储库(另一个人的私有存储库的分支),我像这样克隆了它:

git clone [email protected]:Nicolas_Raoul/therepo.git

Here is my local .git/config:

这是我的本地.git/config

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = https://[email protected]/Nicolas_Raoul/therepo.git
[branch "master"]
        remote = origin
        merge = refs/heads/master

In the same environment with the same public key, Git on Github works fine.
.sshis rwx------, .ssh/id_rsais -rw-------, .ssh/id_rsa.pubis -rw-r--r--

在相同公钥的相同环境下,Github 上的 Git 运行良好。
.sshrwx------,.ssh/id_rsa-rw-------,.ssh/id_rsa.pub-rw-r--r--

回答by manojlds

Are you sure you cloned it using the ssh url?

你确定你是用 ssh url 克隆的吗?

The url for origin says url = https://[email protected]/Nicolas_Raoul/therepo.gitso if it is using https it will ask for password irrespective of your ssh keys.

origin 的 url 表示url = https://[email protected]/Nicolas_Raoul/therepo.git,如果它使用 https,无论您的 ssh 密钥如何,它都会要求输入密码。

So what you want to do is the following:

所以你想要做的是以下内容:

open your config file in your current repo ..

在您当前的 repo 中打开您的配置文件 ..

vim .git/config

vim .git/config

and change the line with the url from

并更改带有 url 的行

[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = https://[email protected]/Nicolas_Raoul/therepo.git

to

[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = [email protected]:Nicolas_Raoul/therepo.git

回答by Ajeet Shah

As explained here, if you clone with SSHurl, you don'tneed to enter username / password each time you push / pull. Check above answerby @manojlds

正如解释在这里,如果你用克隆SSH网址,你需要输入用户名/密码,每次推/拉时间。检查@manojlds的上述答案

But if you want to clone with HTTPSand want to avoidentering username / password each time, you can store credentials into cachewith below command:

但是,如果您想使用HTTPS进行克隆并希望避免每次都输入用户名/密码,则可以使用以下命令将凭据存储到缓存中:

git config --global credential.helper 'cache --timeout 3600'

git config --global credential.helper 'cache --timeout 3600'

where 3600 (seconds) means 1 hour, you may change it as per your requirement.

其中 3600(秒)表示 1 小时,您可以根据需要更改它。

回答by shiva kumar

Its already answered above. I will summarise the steps to check above.

上面已经回答了。我将总结上面检查的步骤。

run git remote -vin project dir. If the output shows remote url starting with https://abcthen you may need username password everytime.

git remote -v在项目目录中运行。如果输出显示远程 url 开头,https://abc那么您可能每次都需要用户名密码。

So to change the remote url run git remote set-url origin {ssh remote url address starts with mostly [email protected]:}.

所以要更改远程 url 运行git remote set-url origin {ssh remote url address starts with mostly [email protected]:}.

Now run git remote -vto verify the changed remote url.

现在运行git remote -v以验证更改的远程 url。

Refer : https://help.github.com/articles/changing-a-remote-s-url/

参考:https: //help.github.com/articles/changed-a-remote-s-url/

回答by Vaiden

Hello Googlers from the future.

你好,来自未来的 Google 员工。

On MacOS >= High Sierra, the SSH key is no longer saved to the KeyChain because of reasons.

在 MacOS >= High Sierra 上,由于原因,SSH 密钥不再保存到 KeyChain

Using ssh-add -Kno longer survives restarts as well.

使用ssh-add -K不再存活重新启动。

Here are 3 possible solutions.

这里有3 种可能的解决方案

I've used the first method successfully. I've created a file called configin ~/.ssh:

我已经成功地使用了第一种方法。我创建了一个名为configin的文件~/.ssh

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa

回答by Sven W.

In the HTTP request case, it is also and alternatively possible to paste the credentials (with password) directly into the url:

在 HTTP 请求的情况下,也可以将凭据(带密码)直接粘贴到 url 中:

http://username:[email protected]/...

This will save the pain to give your credentials every times again. Simple modify your .git/config (the url).

这将省去每次再次提供您的凭据的痛苦。简单地修改你的 .git/config (url)。

回答by stackPusher

None of these answers helped me, turned out my issue was slightly different. It was ssh that was asking for my password each time, before sending the key. So what I had to do was link my password with this command:

这些答案都没有帮助我,结果我的问题略有不同。在发送密钥之前,ssh 每次都要求我输入密码。所以我必须做的是将我的密码与这个命令链接起来:

ssh-add -K ~/.ssh/id_rsa

It'll then prompt you for your password and store it. This could be the solution you're looking for if each time your prompted for a password it says

然后它会提示您输入密码并存储它。如果每次提示您输入密码时,这可能是您正在寻找的解决方案

Enter passphrase for key '/Users//.ssh/id_rsa':

输入密钥“/Users//.ssh/id_rsa”的密码:

More info here

更多信息在这里

NOTE: I used this on my mac machine successfully, but as @Rob Kwasowski pointed out below, the upper case Koption is unique to mac. If not on mac you will need to use lowercase k(which probably works for mac too but I haven't tested).

注意:我在我的 mac 机器上成功使用了它,但正如@Rob Kwasowski 在下面指出的那样,大写K选项是 mac 独有的。如果不在 mac 上,您将需要使用小写字母k(这可能也适用于 mac,但我还没有测试过)。

回答by webdev5

回答by pob

The following assumes command-line access via iTerm / Terminal to bitbucket.

以下假设通过 iTerm / Terminal 命令行访问 bitbucket。

For MacOS Sierra10.12.5, my system manifested an equivalent problem - asking for my SSH passphrase on each connection to bitbucket.

对于MacOS Sierra10.12.5,我的系统出现了同样的问题 - 在每次连接到 bitbucket 时都要求我的 SSH 密码。

The issue has to do with OpenSSH updates in macOS 10.12.2, which are described here in Technical Note TN2449.

该问题与 macOS 10.12.2 中的 OpenSSH 更新有关,此处在技术说明 TN2449 中进行了描述。

You very well might want to tailor your solution, but the following will work when added to your ~/.ssh/config file:

您很可能想要定制您的解决方案,但将以下内容添加到您的 ~/.ssh/config 文件时将起作用:

Host *
    UseKeychain yes

For more information on ssh configs, take a look at the man pages for ssh_config:

有关 ssh 配置的更多信息,请查看 ssh_config 的手册页:

% man ssh_config

One other thing: there is a good write-up on superuserherethat discusses this problem and various solutions depending on your needs and setup.

还有一两件事:有一个很好的写了关于超级用户在这里,讨论这个问题,并根据您的需要和设置的各种解决方案。

回答by Swaroop Bhagwat

I cloned the repository with HTTPS URL instead of SSH URL hence even after adding the SSH Key it was asking me for password on Bash Shell.

我使用 HTTPS URL 而不是 SSH URL 克隆了存储库,因此即使在添加了 SSH 密钥之后,它也要求我在 Bash Shell 上输入密码。

I just edited the ./.git/configfile and changed the value of urlvariable by simply replacing the https://to ssh://

我刚刚编辑了./.git/config文件并url通过简单地替换https://to 来更改变量的值ssh://

E.g.

例如

[core]
        ...
        ...
        ...
[remote "origin"]
        url = https://<username>@bitbucket.org/<username>/<repository_name>.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        ...
        ...
        ...

Changed to:

变成:

[core]
        ...
        ...
        ...
[remote "origin"]
        url = ssh://<username>@bitbucket.org/<username>/<repository_name>.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        ...
        ...
        ...

回答by Grant

You may need to double-check your SSH identities file. You may be guiding BitBucket to look at a different/incorrect private key to the equivalent public key that you have saved on BitBucket.

您可能需要仔细检查您的 SSH 身份文件。您可能正在引导 BitBucket 查看与您保存在 BitBucket 上的等效公钥不同/不正确的私钥。

Check it with tail ~/.ssh/config- you will see something similar to:

检查它tail ~/.ssh/config- 你会看到类似的东西:

Host bitbucket.org
 HostName bitbucket.org
 IdentityFile ~/.ssh/personal-bitbucket-ssh-key

Remember, that adding additional identities (such as work and home) can be done with the ssh-addcommand, for example:

请记住,可以使用以下ssh-add命令添加其他身份(例如工作和家庭),例如:

ssh-keygen -t rsa -C "companyName" -f "companyName"
ssh-add ~/.ssh/companyName

Once you have confirmed which private key is being looked at locally, you can then take your public equivalent, in this case:

一旦您确认在本地查看哪个私钥,您就可以使用您的公共等价物,在这种情况下:

cat ~/.ssh/personal-bitbucket-ssh-key.pub | pbcopy

And paste that cipher onto BitBucket. Your git pushes will now (provided you are using the SSH clone as aforementioned answers have pointed out) be allowed without a password, as your device is a recognised friendly.

并将该密码粘贴到 BitBucket 上。现在,您的 git push 将(假设您使用上述答案所指出的 SSH 克隆)无需密码,因为您的设备是公认的友好设备。

Hopefully this helps clear it up for someone.

希望这有助于为某人清除它。