我按照github教程中的指示创建了密钥,在github上注册了它们,并尝试显式地使用ssh-agent -然而,每当我尝试进行拉或推操作时,git仍然会继续询问我的密码。

原因可能是什么?


当前回答

也许不是最安全的方法来解决这个问题,但不要设置密码短语,这是可选的。如果您不设置密码短语,它将不会请求它。你可以用更改密码

$ 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.

其他回答

我也有类似的问题,但其他答案并没有解决我的问题。我想我应该继续发布这篇文章,以防有人像我一样有一个奇怪的设置。

结果发现我有多个密钥,而Git首先使用了错误的密钥。它会提示我输入密码,然后我输入它,然后Git会使用另一个可以工作的密钥(我不需要在这个密钥上输入密码)。

我刚刚删除了它用来提示我输入密码的密钥,现在它工作了!

从OS X El Capitan(10.11)升级到macOS Sierra(10.12)后重新启动后,这种情况一直发生在我身上。ssh-add解决方案暂时有效,但不会在再次重新启动时继续存在。

永久的解决方案是编辑(或创建)~/。ssh/config,启用UseKeychain选项。

Host *
    UseKeychain yes

相关:自从我更新到Sierra, macOS一直在问我的ssh密码

对于Windows或Linux用户,GitHub Docs中描述了一个可能的解决方案,为了方便起见,我在下面报告。

您可以在打开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

如果您的私钥没有存储在默认位置之一(如~/. SSH /id_rsa),您需要告诉SSH身份验证代理在哪里可以找到它。要将密钥添加到ssh-agent,输入ssh-add ~/path/ To /my_key。

现在,当您第一次运行Git Bash时,系统会提示您输入密码。ssh-agent进程将继续运行,直到您注销、关闭计算机或杀死该进程为止。

我会尝试以下方法:

开始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

一旦你启动了SSH代理,使用:

eval $(ssh-agent)

做:

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 add and save your 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. If you see a warning about deprecated flags, try the new variant: ssh-add --apple-use-keychain To add and save your key permanently on Ubuntu (or equivalent): ssh-add ~/.ssh/id_rsa