Git 不断询问我的 ssh 密钥密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10032461/
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
Git keeps asking me for my ssh key passphrase
提问by Rogach
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?
可能是什么原因?
回答by Roberto Bonvallet
Once you have started the SSH agent with:
使用以下命令启动 SSH 代理后:
eval $(ssh-agent)
You have 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.
To save key permanently on macOS:
ssh-add -K
This will persist it after you close and re-open it by storing it in user's keychain.
您必须向其中添加您的私钥:
ssh-add
这将只询问您一次密码,然后您应该被允许推送,前提是您将公钥上传到 Github。
要在 macOS 上永久保存密钥:
ssh-add -K
这将在您关闭并通过将其存储在用户的钥匙串中重新打开后保留它。
回答by Kyle Clegg
This has been happening to me after restarts since upgrading from OS X El Capitan (10.11) to macOS Sierra (10.12). The ssh-add
solution worked temporarily but would not persist across another restart.
自从从 OS X El Capitan (10.11) 升级到 macOS Sierra (10.12) 以来,重新启动后我就遇到了这种情况。该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
回答by Darryl Young
If you've tried ssh-add
and you're still prompted to enter your passphrase then try using ssh-add -K
. This adds your passphrase to your keychain.
如果您已经尝试过ssh-add
并且仍然提示您输入密码,请尝试使用ssh-add -K
. 这会将您的密码短语添加到您的钥匙串中。
Update: if you're using macOS Sierra then you likely need to do another step as the above might no longer work. Add the following to your ~/.ssh/config
:
更新:如果您使用的是 macOS Sierra,那么您可能需要执行另一个步骤,因为上述操作可能不再有效。将以下内容添加到您的~/.ssh/config
:
Host *
UseKeychain yes
回答by Roland
I would try the following:
我会尝试以下方法:
- Start GitBash
- Edit your
~/.bashrc
file - Add the following lines to the file
- 启动GitBash
- 编辑您的
~/.bashrc
文件 - 将以下行添加到文件中
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
- Save and close the file
- Close GitBash
- Reopen GitBash
- Enter your passphrase
- 保存并关闭文件
- 关闭 GitBash
- 重新打开 GitBash
- 输入您的密码
回答by IgorGanapolsky
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
回答by Arthur
What worked for me on Windows was (I had cloned code from a repo 1st):
在 Windows 上对我有用的是(我从 repo 1st 克隆了代码):
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
回答by 11101101b
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.
我有一个类似的问题,但其他答案没有解决我的问题。我想我会继续发布这个以防万一其他人像我一样有一个奇怪的设置。
It turns out I had multiple keys and Git was using the wrong one first. 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 第一个使用了错误的键。它会提示我输入密码,然后我输入它,然后 Git 会使用另一个可以工作的密钥(我不需要输入密码)。
I just deleted the key that it was using to prompt me for a passphrase and now it works!
我刚刚删除了它用来提示我输入密码的密钥,现在它可以工作了!
回答by Jeff Welling
It sounds like you may be having trouble with SSH-Agent itself. I would try troubleshooting that.
听起来您可能在使用 SSH-Agent 本身时遇到了问题。我会尝试解决这个问题。
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) 您是否在两次使用之间关闭了终端窗口,因为如果关闭该窗口,您将不得不在重新打开它时再次输入密码。
回答by Stephen Harris
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
). It is unusual not to, but that was the cause for me.
如果上述解决方案对我不起作用,则需要检查的一件事是您实际上也拥有公钥(通常为id_rsa.pub
)。不这样做是不寻常的,但这就是我的原因。
To create your public key from your private key:
要从您的私钥创建您的公钥:
ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub
回答by alx lark
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:
这个想法是:
you create
~/.bashrc
fileadd 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
re-run Bash
你创建
~/.bashrc
文件添加以下脚本:
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
重新运行 Bash