SSH密钥: 在mac上“id_rsa.pub”的权限0644太开放。

632

我在我的Mac上生成了一对SSH密钥,并将公钥添加到我的Ubuntu服务器(实际上,它是我Mac上的虚拟机)。但是当我尝试登录Ubuntu服务器时,它显示:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/tudouya/.ssh/vm/vm_id_rsa.pub' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /Users/tudouya/.ssh/vm/vm_id_rsa.pub
Permission denied (publickey,password).

我尝试了许多方法来解决这个问题,比如更改关键文件的模式、更改文件夹的模式,就像stackoverflow上的一些答案所说,但都没有奏效。
关键文件的权限为:

vm dir:
drwxr-xr-x   4 tudouya  staff    136  4 29 10:37 vm

key file:
-rw-------  1 tudouya  staff  1679  4 29 10:30 vm_id_rsa
-rw-r--r--  1 tudouya  staff   391  4 29 10:30 vm_id_rsa.pub

请给我一些想法...



我将主机信息写入ssh_config文件:

Host ubuntuvm
    Hostname 10.211.55.17
    PreferredAuthentications publickey
    IdentityFile /Users/tudouya/.ssh/vm/vm_id_rsa.pub

我运行了命令"ssh -v ubuntuvm",它显示:

ssh -v ubuntuvm
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: /etc/ssh_config line 103: Applying options for *
debug1: /etc/ssh_config line 175: Applying options for ubuntuvm
debug1: Connecting to 10.211.55.17 [10.211.55.17] port 22.
debug1: Connection established.
debug1: identity file /Users/tudouya/.ssh/vm/vm_id_rsa.pub type 1
debug1: identity file /Users/tudouya/.ssh/vm/vm_id_rsa.pub-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-8
debug1: match: OpenSSH_6.6.1p1 Ubuntu-8 pat OpenSSH*
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5-etm@openssh.com none
debug1: kex: client->server aes128-ctr hmac-md5-etm@openssh.com none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 55:6d:4f:0f:23:51:ac:8e:70:01:ec:0e:62:9e:1c:10
debug1: Host '10.211.55.17' is known and matches the RSA host key.
debug1: Found key in /Users/tudouya/.ssh/known_hosts:54
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/tudouya/.ssh/vm/vm_id_rsa.pub
debug1: Server accepts key: pkalg ssh-rsa blen 279
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/tudouya/.ssh/vm/vm_id_rsa.pub' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /Users/tudouya/.ssh/vm/vm_id_rsa.pub
debug1: No more authentication methods to try.
Permission denied (publickey,password).

1
你能否编辑你的问题,包括你正在运行的具体ssh命令?如果涉及到的密钥文件在ssh配置文件中指定,请同时包含相关的配置文件行。 - Kenster
对我来说是“权限不足”。 - insign
2
当我在ssh命令中添加“-i id_rsa.pub”用于连接时,我遇到了这个问题。似乎强制使用ssh命令上的某些公钥会强制要求输入密码(即使我的密码为空,至少在我的情况下是这样)。 - Diego Andrés Díaz Espinoza
2
这些家伙竟然费力地打印出那条消息,包括当前的权限值,但却不告诉你应该是什么权限。这就像一种“我本可以告诉你,但这是Linux,所以我会保持神秘,让你去Google浪费时间”的态度,实在是疯狂。 - Chris Vilches
1
@DiegoAndrésDíazEspinoza 对于ssh -i,您应该指定私钥。对于ssh-copy-id -i,您应该指定公钥!您遇到的问题是在执行ssh时指定了公钥(错误的操作),它会将其视为私钥并使用它作为私钥,然后失败,因此它会要求密码,因为ssh服务器设置为在密钥无法工作时允许密码。 - barlop
25个回答

1334

我建议你执行以下操作:

chmod 400 ~/.ssh/id_rsa

这对我来说很有效。


64
“'id_rsa.pub' 的权限为 0644 太开放了,因此该密钥被忽略了。这可能是因为我从我的另一台电脑上复制了密钥文件。但修复权限问题解决了这个问题。谢谢!” - parasrish
2
@xoxn--1'w3k4n为什么这么糟糕?如果人们复制或以其他方式管理其对这些敏感文件的读写访问不当,那么修复它们是有意义的。 - Gerard
6
这不是胶带。如果你复制了你的身份证,这就有意义了。 - ALisboa
4
从旧电脑复制ssh密钥时,这个方法对我有用! - Nick
9
WSL上也可以工作。 - h-rai
我在id_rsa.pub和id_rsa文件上都做了这个更改,发现这对我解决问题很有帮助。我还确保我的配置文件中的IdentityFile指向id_rsa文件。 - Michael Dally

284
debug1: identity file /Users/tudouya/.ssh/vm/vm_id_rsa.pub type 1

看起来您试图使用错误的密钥文件。扩展名为".pub"的文件包含密钥的公共部分。相应的无".pub"扩展名的文件包含密钥的私有部分。当您运行ssh客户端连接远程服务器时,必须向ssh客户端提供私钥文件。

您的.ssh/config文件(或/etc/ssh_config)中可能有一行看起来像这样:

IdentityFile .../.ssh/vm/vm_id_rsa.pub

你需要从文件名中移除".pub"扩展名:

IdentityFile .../.ssh/vm/vm_id_rsa

我在使用SQLPro时遇到了问题,错误地选择了公共(.pub)文件而不是私有文件。 - Henry
3
当我执行 ssh -i id_ed25519.pub 时出现了错误,但是执行 ssh -i id_ed25519 却可以正常工作。 - user3064538
1
这不应该被接受为答案。@Rick Benetti的回答更简单,适用于更多情况。 - whenitrains
非常感谢!虽然建议很多,但是没有一个对我有用。这个问题只在WSL2上复现,在Ubuntu18上使用*.pub时却可以正常工作。 - Yuriy Yakubskiy
这个不起作用。 我正在遵循 https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent 的文档。但是我遇到了和原帖中提到的相同的错误。而且这个解决方案对我也不起作用。 - akshit bhatia
显示剩余2条评论

98
关键字应该可以被已登录的用户阅读。
试试这个:
chmod 400 ~/.ssh/Key_file

6
使用公钥400或600执行 ssh-add ~/.ssh/id_rsa.pub 命令时,会提示无效格式。 - rhand
4
@rhand ssh-add需要私钥而不是公钥。 - Harry

84
chmod 400 path/to/filename

这方法对我有用。当我按照这个文件的步骤操作后,我成功连接到了我的EC2实例。


60

将您的KEY权限更改为

chmod 400 your_key.pem

它应该可以正常工作!


https://www.cyberciti.biz/faq/unix-linux-bsd-chmod-numeric-permissions-notation-command/ - Ben

31

运行以下命令后,它就可以正常工作:

sudo chmod 600 /path/to/my/key.pem

(注:此命令是用于修改密钥文件权限的。)

21

在我的情况下,它是一个 .pem 文件。事实证明对于它也适用。更改文件权限后,它就可以工作了。

chmod 400 ~/.ssh/dev-shared.pem

感谢所有帮助过我的人。


20

SSH密钥应该是私有的,因此644权限太开放了。

二进制引用设置权限。

 r(read) = 4
 w(write) = 2
 x(execute) = 1

通过将这些数字相加并将总和数字传递给chmod命令,我们设置文件/目录的权限。第一个数字设置所有者的权限,第二个数字设置组的权限,第三个数字设置系统上其他没有对该文件的权限的用户的权限。

A permission of 644 means 
(4+2) = read/write permission for the owner
(4) = read permission for the group 
(4) = read permission for all other users 
 

使用以下方法将文件的权限更改为400

chmod 400 <filename>

解决了这个问题。因为它使密钥只读,只能被所有者访问。

参考: https://www.linux.com/training-tutorials/understanding-linux-file-permissions/


如果您已经在使用私有文件(而不是.pub文件),那么这就是正确的解决方案,只需运行chmod 400 id_rsa_privatefilename命令,然后尝试重新连接即可。 - Dany Balian
很好的答案 :o) - Jeremy

17
chmod 600 id_rsa

在存储密钥的虚拟机路径下运行上述命令,例如:cd /home/opc/.ssh


600 给予读写权限。 - Gene

17
很多答案都很相似,但没有解释…… 这个错误是由于私钥文件权限过于开放所引起的。这是一种安全风险。
将私钥文件的权限更改为最小化(仅由所有者只读)。
  1. 更改所有者 chown <unix-name> <private-key-file>
  2. 设置最小权限(仅限文件所有者读取)chmod 400 <private-key-file>

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