简体   繁体   English

Git 不断询问我的 ssh 密钥密码

[英]Git keeps asking me for my ssh key passphrase

I created keys as instructed in the github tutorial, registered them with github, and tried using ssh-agent explicitly — yet git continues to ask me for my passphrase every time I try to do a pull or a push.我按照 github 教程中的说明创建了密钥,将它们注册到 github,并尝试显式使用 ssh-agent — 但每次我尝试拉取或推送时,git 都会继续询问我的密码。

What could be the cause?可能是什么原因?

Once you have started the SSH agent with:使用以下命令启动 SSH 代理后:

eval $(ssh-agent)

Do either:执行以下任一操作:

  1. To add your private key to it:要将您的私钥添加到其中:

     ssh-add

    This will ask you your passphrase just once, and then you should be allowed to push, provided that you uploaded the public key to Github.这只会询问你一次密码,然后你应该被允许推送,前提是你将公钥上传到 Github。

  2. To add and save your key permanently on macOS :要在macOS上永久添加和保存您的密钥:

     ssh-add -K

    This will persist it after you close and re-open it by storing it in user's keychain.这将在您关闭并通过将其存储在用户的钥匙串中重新打开后保留它。

    If you see a warning about deprecated flags, try the new variant:如果您看到有关deprecated标志的警告,请尝试使用新变体:

     ssh-add --apple-use-keychain
  3. To add and save your key permanently on Ubuntu (or equivalent):要在Ubuntu (或等效电话)上永久添加和保存您的密钥:

     ssh-add ~/.ssh/id_rsa

This has been happening to me after restarts since upgrading from OS X El Capitan (10.11) to macOS Sierra (10.12).自从从 OS X El Capitan (10.11) 升级到 macOS Sierra (10.12) 后,这在我身上一直发生。 The ssh-add solution worked temporarily but would not persist across another restart. ssh-add解决方案暂时有效,但不会在另一次重启后持续存在。

The permanent solution was to edit (or create) ~/.ssh/config and enable the UseKeychain option.永久解决方案是编辑(或创建) ~/.ssh/config并启用UseKeychain选项。

Host *
    UseKeychain yes

Related: macOS keeps asking my ssh passphrase since I updated to Sierra相关: 自从我更新到 Sierra 后,macOS 一直在询问我的 ssh 密码

If you've tried ssh-add and you're still prompted to enter your passphrase then try using ssh-add -K .如果您尝试过ssh-add但系统仍然提示您输入密码,请尝试使用ssh-add -K This adds your passphrase to your keychain.这会将您的密码添加到您的钥匙串中。

Update: if you're using macOS Sierra then you likely need to do another step as the above might no longer work.更新:如果您使用的是 macOS Sierra,那么您可能需要执行其他步骤,因为上述步骤可能不再有效。 Add the following to your ~/.ssh/config :将以下内容添加到您的~/.ssh/config

Host *
  UseKeychain yes

I would try the following:我会尝试以下操作:

  1. Start GitBash启动GitBash
  2. Edit your ~/.bashrc file编辑你的~/.bashrc文件
  3. Add the following lines to the file将以下行添加到文件中

SSH_ENV=$HOME/.ssh/environment

# start the ssh-agent
function start_agent {
    echo "Initializing new SSH agent..."
    # spawn ssh-agent
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
    echo succeeded
    chmod 600 ${SSH_ENV}
    . ${SSH_ENV} > /dev/null
    /usr/bin/ssh-add
}

if [ -f "${SSH_ENV}" ]; then
     . ${SSH_ENV} > /dev/null
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi
  1. Save and close the file保存并关闭文件
  2. Close GitBash关闭 GitBash
  3. Reopen GitBash重新打开 GitBash
  4. Enter your passphrase输入您的密码

What worked for me on Windows was (I had cloned code from a repo 1st):在 Windows 上对我有用的是(我从第一个 repo 克隆了代码):

eval $(ssh-agent)
ssh-add 
git pull 

at which time it asked me one last time for my passphrase那时它最后一次问我密码

Credits: the solution was taken from https://unix.stackexchange.com/questions/12195/how-to-avoid-being-asked-passphrase-each-time-i-push-to-bitbucket学分:解决方案取自https://unix.stackexchange.com/questions/12195/how-to-avoid-being-asked-passphrase-each-time-i-push-to-bitbucket

Try adding this to your ~/.ssh/config :尝试将其添加到您的~/.ssh/config

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

... assuming your private key is named id_rsa ...假设你的私钥被命名为id_rsa

If you are not using GitBash and are on Windows - you need to start your ssh-agent using this command如果你没有使用 GitBash 并且在 Windows - 你需要使用这个命令启动你的 ssh-agent

start-ssh-agent.cmd

If your ssh agent is not set up, you can open PowerShell as admin and set it to manual mode如果你的ssh代理没有设置,可以admin打开PowerShell,设置为手动模式

Get-Service -Name ssh-agent | Set-Service -StartupType Manual

Run the following:运行以下命令:

eval $(ssh-agent) && ssh-add ~/.ssh/id_rsa &>/dev/null

Enter the passphrase, then check git. Git should not ask for passphrase after this command.输入密码,然后检查 git。Git 不应在此命令后询问密码。

The original source:https://gist.github.com/egoens/c3aa494fc246bb4828e517407d56718d原始出处:https://gist.github.com/egoens/c3aa494fc246bb4828e517407d56718d

If the above solutions are not working for me, one thing to check is that you actually have the public key too (typically id_rsa.pub ).如果上述解决方案对我不起作用,要检查的一件事是您实际上也有公钥(通常是id_rsa.pub )。 It is unusual not to, but that was the cause for me.不这样做是不寻常的,但这就是我的原因。

To create your public key from your private key:从您的私钥创建您的公钥:

ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub

previously -K flag is used to add key but now:以前 -K 标志用于添加密钥,但现在:

ssh-add --apple-use-keychain

The -K and -A flags are deprecated and have been replaced by the --apple-use-keychain and --apple-load-keychain flags, respectively. -K 和 -A 标志已弃用,并分别由 --apple-use-keychain 和 --apple-load-keychain 标志取代。


LATER EDIT: you may need to add ssh-add --apple-load-keychain -q to your .bash_profile or .bashrc or .zshrc or equivalent.稍后编辑:您可能需要将ssh-add --apple-load-keychain -q到您的.bash_profile.bashrc.zshrc或等效文件中。

I had a similar issue, but the other answers didn't fix my problem.我有一个类似的问题,但其他答案并没有解决我的问题。 I thought I'd go ahead and post this just in case someone else has a screwy setup like me.我想我会提前 go 并发布这个以防其他人像我一样有一个古怪的设置。

It turns out I had multiple keys and Git was using the wrong one first.原来我有多个密钥,而 Git 首先使用了错误的密钥。 It would prompt me for my passphrase, and I would enter it, then Git would use a different key that would work (that I didn't need to enter the passphrase on).它会提示我输入密码,我会输入它,然后 Git 会使用另一个有效的密钥(我不需要输入密码)。

I just deleted the key that it was using to prompt me for a passphrase and now it works!我刚刚删除了它用来提示我输入密码的密钥,现在可以使用了!

It sounds like you may be having trouble with SSH-Agent itself.听起来您可能遇到了 SSH-Agent 本身的问题。 I would try troubleshooting that.我会尝试排除故障。

1) Did you do ssh-add to add your key to SSH? 1) 您是否通过 ssh-add 将您的密钥添加到 SSH?

2) Are you closing the terminal window between uses, because if you close the window you will have to enter the password again when you reopen it. 2) 您是否在两次使用之间关闭了终端 window,因为如果您关闭了 window,您将不得不在重新打开时再次输入密码。

For Windows or Linux users, a possible solution is described on GitHub Docs , which I report below for your convenience.对于WindowsLinux用户, GitHub 文档中描述了一种可能的解决方案,为方便起见,我将其报告如下。

You can run ssh-agent automatically when you open bash or Git shell. Copy the following lines and paste them into your ~/.profile or ~/.bashrc file:您可以在打开 bash 或 Git shell 时自动运行ssh-agent 。复制以下行并将它们粘贴到您的~/.profile~/.bashrc文件中:

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env

If your private key is not stored in one of the default locations (like ~/.ssh/id_rsa ), you'll need to tell your SSH authentication agent where to find it.如果您的私钥未存储在默认位置之一(如~/.ssh/id_rsa ),您需要告诉您的 SSH 身份验证代理在哪里可以找到它。 To add your key to ssh-agent, type ssh-add ~/path/to/my_key .要将密钥添加到 ssh-agent,请键入ssh-add ~/path/to/my_key

Now, when you first run Git Bash, you are prompted for your passphrase.现在,当您第一次运行Git Bash 时,系统会提示您输入密码。 The ssh-agent process will continue to run until you log out, shut down your computer, or kill the process. ssh-agent进程将继续运行,直到您注销、关闭计算机或终止该进程。

In case you are on Win10:如果您使用的是 Win10:

I had the same problem.我有同样的问题。 (previously had to update ssh-agent individually with a script from here because of a different problem) (以前由于不同的问题不得不使用这里的脚本单独更新 ssh-agent)

Git did access my ssh config (git pull threw exceptions when I had nonsense-lines in ssh config), but never seemed to care about the private key I had added via ssh-agent and referenced in my config. Git 确实访问了我的 ssh 配置(当我在 ssh 配置中有无意义行时,git pull 抛出异常),但似乎从不关心我通过 ssh-agent 添加并在我的配置中引用的私钥。

What fixed the problem was to execute the following command in PowerShell:解决问题的方法是在 PowerShell 中执行以下命令:

git config core.sshCommand (get-command ssh).Source.Replace('\','/')

(Details are in this link ) (详情在此链接中)

I try different solutions but nothing help.我尝试了不同的解决方案,但没有任何帮助。 But this steps ( My GitBash SSH environment always asks for my passphrase, what can I do? ) from Bitbucket.com seams works well:但是这个步骤( 我的 GitBash SSH 环境总是询问我的密码,我该怎么办? )来自 Bitbucket.com 接缝效果很好:

The idea is:这个想法是:

  1. you create ~/.bashrc file你创建~/.bashrc文件

  2. add follow script:添加以下脚本:

     SSH_ENV=$HOME/.ssh/environment # start the ssh-agent function start_agent { echo "Initializing new SSH agent..." # spawn ssh-agent /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}" echo succeeded chmod 600 "${SSH_ENV}". "${SSH_ENV}" > /dev/null /usr/bin/ssh-add } if [ -f "${SSH_ENV}" ]; then. "${SSH_ENV}" > /dev/null ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || { start_agent; } else start_agent; fi
  3. re-run Bash重新运行 Bash

Another possible solution that is not mentioned above is to check your remote with the following command:上面没有提到的另一种可能的解决方案是使用以下命令检查您的遥控器:

git remote -v

If the remote does not start with git but starts with https you might want to change it to git by following the example below.如果遥控器不是以 git 开头,而是以 https 开头,您可能需要按照以下示例将其更改为 git。

git remote -v // origin is https://github.com/user/myrepo.git
git remote set-url origin git@github.com:user/myrepo.git
git remote -v // check if remote is changed

on mac, if your ssh key need passphrase everytime and you want to skip it, then you can try below, it works fine for me在 Mac 上,如果你的 ssh 密钥每次都需要密码,而你想跳过它,那么你可以在下面尝试,它对我来说很好用

  1. eval "$(ssh-agent -s)"评估“$(ssh-agent -s)”
  2. ssh-add -K.ssh/id_rsa ssh 添加-K.ssh/id_rsa
  3. add this default ssh configuration works for me添加此默认 ssh 配置对我有用

Host *主持人 *
AddKeysToAgent yes AddKeysToAgent 是
UseKeychain yes使用钥匙串 是
IdentityFile ~/.ssh/id_rsa身份文件 ~/.ssh/id_rsa

Update the url of the origin remote using SSH instead of HTTPS;使用 SSH 而不是 HTTPS 更新源远程的 url;

git remote set-url origin "SSH URL COPIED FROM GIT REPO."

This what works with me.这对我有用。

If you happen to be using fish, there's a gist for it:如果您碰巧使用的是鱼,那么有一个要点

# config.fish
if not pgrep -f ssh-agent > /dev/null
  eval (ssh-agent -c)
  set -Ux SSH_AUTH_SOCK $SSH_AUTH_SOCK
  set -Ux SSH_AGENT_PID $SSH_AGENT_PID
  set -Ux SSH_AUTH_SOCK $SSH_AUTH_SOCK
end

Maybe not the most secure way to fix this, but simply do not set a passphrase, it is optional.也许不是解决此问题的最安全方法,但不要设置密码,它是可选的。 If you don't set a passphrase, it will not ask for it.如果你不设置密码,它不会要求它。 You can change the passphrase with您可以更改密码

$ ssh-keygen -p -f ~/.ssh/id_ed25519
> Enter old passphrase: [Type old passphrase]
> Key has comment 'your_email@example.com'
> Enter new passphrase (empty for no passphrase): [Type new passphrase]
> Enter same passphrase again: [Repeat the new passphrase]
> Your identification has been saved with the new passphrase.

Seems that your local repo hasnt updated with ssh keys...at least this is what happened to me when going from https to ssh.似乎您的本地存储库尚未更新为 ssh 密钥...至少这是我从 https 到 ssh 时发生的情况。

Have you done a remote reset?你做过远程重置吗?

git remote set-url origin <ssh url>

Idk if someone needs something different but this help me a lot https://stackoverflow.com/a/6445525/11891580 Idk 如果有人需要不同的东西,但这对我有很大帮助https://stackoverflow.com/a/6445525/11891580

To me on every restart, I have to run ssh-add --apple-use-keychain to load credentials, so I add this command to the stack overflow answer and now its fixed对我来说,每次重新启动时,我都必须运行ssh-add --apple-use-keychain来加载凭据,所以我将此命令添加到堆栈溢出答案中,现在它已修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM