Git总是要求输入我的ssh密钥密码。

31

我在使用git时遇到了一个问题。

当我使用命令git pull origin my_branch或者git fetch或者git push origin my_branch时,git总是要求我输入密钥密码。我不明白为什么会这样,请问有人能解释一下原因,并告诉我如何避免每次都输入密钥密码吗?

$ git fetch
Enter passphrase for key '/home/khanhpn/.ssh/id_rsa':

1
阅读有关此内容的信息 https://help.github.com/articles/generating-ssh-keys/ - danleyb2
如果您对某个答案满意,请将其标记为已接受的答案。 - Matthieu Moy
2个回答

76

就像 Nasreddine 所说的那样,这是因为您的密钥已经使用密码短语加密,以防止其他人读取它。

使用密码短语密钥的最方便方法是使用 ssh-agent 启动身份验证代理(在后台运行):

$ eval "$(ssh-agent)"
Agent pid 44304

接下来使用ssh-add将您的密钥注册到代理程序中,以便在之后的SSH连接中自动使用:

$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /home/khanhpn/.ssh/id_rsa:

当您运行 ssh-add 时,会要求您输入密语,但只要 ssh-agent 在运行,就不需要再次输入。

您可以在 GitHub 上找到更多信息,点击这里。另外请注意,每次登录时都必须执行此操作,因此您可能希望将 eval "$(ssh-agent)" 步骤添加到您的 .profile 脚本中。


我们需要把那个命令放在文件.profile里面吗? - Sandesh Sapkota

12

这是因为您的私有SSH密钥受到密码保护。您可以使用此命令将其删除(不建议,因为任何人都可以复制您的密钥并使用它访问您的存储库/帐户):

$ ssh-keygen -p
Enter file in which the key is (/path/to/your/.ssh/key):

在此处提示输入当前的密码短语:

Enter old passphrase:
Key has comment 'rsa w/o comment'

如果您想在此处取消输入密码,请将其留空:

Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.

2
这个方法很有效,但你必须明白,任何拥有你私钥文件访问权限的人都可以无需密码访问你的远程账户(例如,如果你的笔记本电脑被盗了怎么办?)。一个更安全的选择是使用ssh-agent,正如Will Vousden所建议的那样。 - Matthieu Moy
也许我应该强调那部分。 - Nasreddine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接