.ssh/id_rsa 失败:权限被拒绝

Posted

技术标签:

【中文标题】.ssh/id_rsa 失败:权限被拒绝【英文标题】:.ssh/id_rsa failed: permission denied 【发布时间】:2012-03-27 00:43:38 【问题描述】:

我一直在扫描网络/SO 并阅读了一些权限被拒绝请求的帮助我只是找不到以我理解的方式解决我的问题的请求。

我正在遵循这些说明 (Getting Started with Python on Heroku/Cedar)。一切都很顺利,直到:

drewverlee@ubuntu:~/helloflask$ source venv/bin/activate
(venv)drewverlee@ubuntu:~/helloflask$ git push heroku master

The authenticity of host 'heroku.com (50.19.85.132)' can't be established.
RSA key fingerprint is ##:##:##:##:##:##:##:##:##:##:##:## (I replaced with #)
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/drewverlee/.ssh/known_hosts).
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

(不确定安全性,所以我将密钥替换为(#))

我想可能是因为

drwx------  2 root       root        1024 2012-03-08 21:26 .ssh

因为

drewverlee@ubuntu:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/drewverlee/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
open /home/drewverlee/.ssh/id_rsa failed: Permission denied.
Saving the key failed: /home/drewverlee/.ssh/id_rsa.

作为在这些问题上经验不足的人,我不确定如何安全地撤消我所做的事情,因为我知道我正在使用强大的工具进行干预。关于这里发生的事情有什么建议吗? 如果我需要包含更多信息来解决问题,请告诉我。

【问题讨论】:

@CIRCLE 在我看来不是个好主意 【参考方案1】:

您应该拥有自己目录中 .ssh 目录的权限,但在您的情况下,它归根用户所有。试试

cd ~
sudo chown drewverlee .ssh

然后重试创建密钥和连接。

【讨论】:

当我读到这可能是有效的事情时,我想出了如何拥有我的整个用户目录。感谢您为我指明正确的方向。 我发现 ssh-keygen 更喜欢创建 .ssh 目录。如果目录已经存在,它会发出一条权限被拒绝的消息,而不管允许的所有权权限配置如何。【参考方案2】:

我在 CentOS 6 上遇到了同样的问题。通过删除 selinux 解决了它:

sudo yum remove selinux*

found the answer here

注意:如果您不知道自己在做什么,那么盲目删除 selinux 可能不是一个好主意

【讨论】:

禁用 selinux 比在 /etc/selinux/config 中更改 SELINUX=disabled 更容易。见Cenos Docs Stop Disabling Selinx 这确实是一个 selinux 问题,您应该通过查看审核日志来修复上下文。【参考方案3】:

由于某些原因,~/.ssh 文件夹中的 id_rsa 文件对我的用户 (0400) 处于只读模式。我用

将其更改为读写(0600)
chmod 0600 id_rsa

在我显然能够覆盖文件之后。我想这些是您可以授予此文件的最高权限,因为其他权限没有太大意义。

【讨论】:

【参考方案4】:

我的用户(ubuntu - 你可以找到键入 whoami)确实拥有 ~/.ssh 文件夹,但它仍然不允许我使用来自 ssh-keygen 的符号链接(文件:~/.ssh/my_file_rsa)。所以我只是 cd 到 ~/.ssh 文件夹,并没有为 rsa 文件名放置外部路径。

whoami
ls -Al ~

cd ~/.ssh
ssh-keygen
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):  my_file_rsa

【讨论】:

cd-ing 进入 ~/.ssh 目录对我有用。我很想更多地了解为什么会这样。 @aalaap:奇怪的是这对我也有用。我也想知道为什么。【参考方案5】:

因为以上答案都不适合我。我会发布我的答案:

如果您还记得密码并想保留旧的 id_rsa,请使用RECOMMENDED SOLUTION,否则请转到NOT RECOMMENDED SOLUTION

推荐的解决方案

    重置权限以更正值
chmod -c 0644 id_rsa.pub
chmod -c 0600 id_rsa

不推荐的解决方案

    删除旧的 ssh
sudo rm -rf ~/.ssh/id_rsa
sudo rm -rf ~/.ssh/id_rsa.pub
    生成新的 ssh 并使用它(见https://help.github.com/enterprise/2.15/user/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/)

为什么有效:

sudo 命令创建的 ssh 是 root 用户的 ssh,而不是用户的 ssh。这意味着ssh-add ~/.ssh/id_rsa 将无法将 root ssh 添加到用户。 当您尝试生成新用户 ssh 时,您无法成功替换旧用户,因为它是为 root 生成的。

(如果有问题,请让我更正我的答案。thx :)

【讨论】:

删除旧密钥后,您将无法访问所有使用旧密钥的 ssh 服务器,而只需更改权限和所有权即可解决此问题!

以上是关于.ssh/id_rsa 失败:权限被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

Enter passphrase for key ‘/c/Users/**/.ssh/id_rsa‘ 解决办法!

免密码连接ssh

代码签名框架失败,出现“权限被拒绝”错误

github ssh 密钥不被接受,获取 ssh 数据包类型 51 (

php/iis:打开流失败:权限被拒绝

部署失败,bin/yarn:权限被拒绝