如何解决“sign_and_send_pubkey: signing failed: agent refused operation”错误?

319

配置新的 Digital Ocean droplet 使用 SSH 密钥。当我运行 ssh-copy-id 时,我得到以下输出:

ssh-copy-id user@012.345.67.89
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
sign_and_send_pubkey: signing failed: agent refused operation
user@012.345.67.89's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'user@012.345.67.89'"
and check to make sure that only the key(s) you wanted were added.

但是,当我尝试进行ssh连接时,发生了这种情况:

ssh user@012.345.67.89
sign_and_send_pubkey: signing failed: agent refused operation
user@012.345.67.89's password: 

输入密码后,我可以正常登录,但这显然违背了首次创建SSH密钥的初衷。我决定查看服务器端的ssh-agent,以下是我得到的结果:

user@012.345.67.89:~# eval `ssh-agent -s`
Agent pid 5715
user@012.345.67.89:~# ssh-add -l
The agent has no identities.

user/.ssh/authorized_keys中也包含一个ssh-rsa键入条目,但是find -name "keynamehere"返回为空。

26个回答

502

在客户端上运行ssh-add,这将把SSH密钥添加到代理中。

使用ssh-add -l(同样在客户端上)确认它确实被添加。


19
天啊,我花了两个小时来尝试修复这个问题,结果只是这个!修复了Bitbucket和Acquia的SSH连接。 - Ronnie
49
我使用 gpg-agent 来进行 SSH 功能,但这未完全解决问题。我已经在 gpg-agent.conf 中添加了 enable-ssh-support,但仍然出现相同的错误信息。我在邮件列表上找到了一个解决方法:运行 gpg-connect-agent updatestartuptty /bye。参考链接:https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=835394 - Roland
5
我刚刚不得不杀掉gpg-agent,然后再次运行它。 - Subin
4
当你生成一个新的SSH密钥时,需要调用ssh-add命令,让ssh-agent意识到新的私钥(参见https://linux.die.net/man/1/ssh-agent)。 - alex
4
我必须更正私钥的许可权限,然后执行ssh-add。警告:未受保护的私钥文件!'/home/<user>/.ssh/id_rsa' 的权限为 0640,过于开放。 要求您的私钥文件不可被其他人访问。 该私钥将被忽略。$ chmod 600 /home/<user>/.ssh/id_rsa$ ssh-add然后工作成功。谢谢。 - Ziprasidone
显示剩余7条评论

248

在将Fedora 26升级到28后,我遇到了同样的问题。 以下日志丢失。

/var/log/secure
/var/log/messages

问题:

antop@localmachine  ~  ssh root@ocp1.example.com
sign_and_send_pubkey: signing failed: agent refused operation
root@ocp1.example.com's password:

错误信息没有指向实际问题。通过以下方法解决问题:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/*

4
我的 .ssh/ 没有所需的权限,因为我自己手动创建了它。 - Brent Bradburn
2
似乎某些版本不允许其他用户看到您的密钥。谢谢! - alan ocallaghan
4
我必须感谢你。我刚遇到了这个问题,用了你的方法解决了它。 - Land
3
谢谢。它起作用了。你真棒 :) @Anto - Imran Rafiq Rather
天啊,谢谢你。真是太糟糕了,即使使用-vv选项,SSH也不会抱怨权限问题.....真的...... - Pierre
显示剩余4条评论

195

我在Linux Ubuntu 18中遇到了同样的问题。在从Ubuntu 17.10更新后,每个git命令都会显示该消息。

解决方法是确保您对id_rsaid_rsa.pub具有正确的权限。

使用stat --format '%a' <file>检查当前文件的chmod编号。对于id_rsa,它应该是600,而对于id_rsa.pub,它应该是644

要更改文件的权限,请使用:

chmod 600 id_rsa
chmod 644 id_rsa.pub

那解决了我的更新问题。


6
我在将Ubuntu从16.04 LTS迁移到18.04 LTS后遇到了这个问题,这个解决方案对我有用。 - Munish Chandel
2
我也是,在将Ubuntu更新到18.04后,我遇到了这个问题。这个解决方案修复了它。 - Cartucho
id_rsa.pub 何时用于客户端身份验证过程? - Dimitri Kopriwa
解决了我的问题。谢谢 :) - Mark
我使用了多台电脑,为了简化操作,我已经将我的ssh密钥上传到云盘中。如果我需要切换电脑,我只需要下载ssh密钥,应用它,然后就可以愉快地工作了,谢谢。 - Ron Michael
显示剩余2条评论

60
运行以下命令以解决此问题。
对我有用。
chmod 600 ~/.ssh/id_rsa

35

我曾经也遇到和你一样的问题,以下是我通过以下步骤解决它的方法。

  1. 将~/.ssh目录权限设置为700
  2. 将~/.ssh/*中所有文件的权限设置为600
  3. ssh-copy-id user@ip 连接到目标服务器,并将公钥添加到authorized_keys文件中
  4. 启动ssh-agent
  5. 添加私钥到ssh-agent中

12
我的关键文件只需要chmod 600,那SSH通信中那是多么愚蠢的错误信息! - U.V.
1
天哪。我真是惊讶得目瞪口呆,OpenSSH竟然对于不安全的密钥文件权限只模棱两可地报告“代理拒绝操作”。</双手捂脸> - Cecil Curry
是的。这也是我解决问题的方法。我五个月后回来处理我的服务器,似乎OpenSSH的更改需要更严格的文件权限。向@Dean致敬,因为他找出了这个问题!点赞! - m8a
1
首先尝试使用 ssh-add -vvv ~/.ssh/keys/<mykey> 命令是检查正确权限的好方法,因为该命令会告诉你出了什么问题(例如“'.ssh/<mykey>' 的权限为 0644,太开放了。”)。 - pixelbrackets

22
使用gpg-agent作为ssh-agent并使用gpg子密钥作为ssh密钥时,我遇到了错误。我怀疑问题是由于我的sway配置中使用的sleep+lock命令引起的gpg无效密码输入tty引起的。
解决问题的方法是重置密码输入tty: gpg-connect-agent updatestartuptty /bye > /dev/null 同时修改我的sway sleep+lock命令: bindsym $mod+Shift+l exec "sh -c 'gpg-connect-agent reloadagent /bye>/dev/null; systemctl suspend; swaylock; gpg-connect-agent updatestartuptty /bye > /dev/null'" 参考链接:https://wiki.archlinux.org/index.php/GnuPG#gpg-agent

3
谢谢。几天前我遇到了这个问题,我和你一样使用gpg,并在我的~/.zshrc中注释掉了gpg-connect-agent updaterstartuptty /bye > /dev/null这行代码,取消注释这行代码解决了我的问题。 - J.Adler
3
你是传奇。你怎么找到了解决方法?这个问题太难了! - RichieHH

15

eval "$(ssh-agent -s)"
执行此命令启动ssh代理

ssh-add
然后添加ssh密钥


这对我来说是工作! - linjiejun

8

对于这个错误:

# git pull
sign_and_send_pubkey: signing failed: agent refused operation
git@github.com: Permission denied (publickey).    
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

请在 Github 账户的个人资料中验证或重新添加公钥。具体操作路径为:profile -> ssh。以下是我的解决方案:
# chmod 400 ~/.ssh/id_rsa

# ls  ~/.ssh/id_rsa -ls  
4 -r--------. 1 reinaldo reinaldo 1679 Jul 26  2017 /home/reinaldo/.ssh/id_rsa

# git pull                                 
remote: Counting objects: 35, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 35 (delta 9), reused 34 (delta 9), pack-reused 0
Unpacking objects: 100% (35/35), done.

谢谢。


5

出现SSH错误可能有多种原因:

sign_and_send_pubkey: signing failed: agent refused operation

其中一些可能与其他答案中提到的问题相关(请参见此线程的答案),而另一些可能是隐藏的,因此需要更仔细的调查。

在我的情况下,我收到了以下错误信息:

sign_and_send_pubkey: signing failed: agent refused operation

user@website.domain.com:Permission denied(publickey,gssapi-keyex,gssapi-with-mic)

找到真正的问题的唯一方法是调用-v详细选项,这将导致打印大量的调试信息:

debug1: Connecting to website.domain.com [xxx.xxx.xxx.xxx] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/id_rsa.website.domain.com type 0
debug1: key_load_public: No such file or directory
debug1: identity file /home/user/.ssh/id_rsa.website.domain.com-cert type -1

请注意,说“key_load_public: No such file or directory”的那一行是指下一行而不是上一行。
因此,SSH实际上是无法找到名为id_rsa.website.domain.com-cert的公钥文件,这似乎是我的问题所在,因为我的公钥文件没有包含-cert后缀。
长话短说:在我的情况下,解决方法就是确保公钥文件的命名符合预期。如果不调试连接,我永远不会怀疑到这一点。
总之,使用SSH详细模式(-v选项)来找出问题所在,可能有各种各样的原因,但都不能在本/另一个线程中找到。

4

首先执行ssh-add命令,然后执行ssh user@ip命令。

这对我有用。


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