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个回答

16

我有类似的问题,通过更改相关文件和文件夹的权限解决了它。

这是对我有效的解决方案:

$ chmod 0600 ~/.ssh/id_rsa.pub
$ chmod 0600 ~/.ssh/authorized_keys
$ chmod 0600 ~/.ssh/id_rsa
$ chmod 0700 ~/.ssh

2
$ chmod 0600 ~/.ssh/* $ chmod 0700 ~/.ssh这是一种简写方式。 - Cory Collier

16

如果密钥在~/.ssh目录中,请使用

chmod 400 ~/.ssh/id_rsa

如果密钥在其他目录中,请使用

chmod 400 directory_path/id_rsa

这对我起作用了。


2
这如何改进其他答案? - Nico Haase
3
需要保护的不是公钥,而是私钥。 - Picarus
私钥必须受到保护。 - bashan
它对我有效。我认为@Anirban你是指chmod 400 ~/.ssh/id_rsa。如上所述:“带有“.pub”扩展名的文件是公钥文件。相应的没有“.pub”扩展名的文件是私钥文件。”我们需要保护私钥文件。 - naveenKumar
修改了答案。应该是私钥。 - Anirban
chmod 400 ~/.ssh/id_rsa非常顺利地工作了! - Code42

10

您需要运行下面的命令

chmod 400 /path/to/my/key.pem

7

对于我来说,id_rsa 的默认模式是 600,这意味着它是可读和可写的。

当我将该文件推送到 Git 仓库并从另一台电脑拉取时,有时私钥文件的模式会变成 -rw-r--r--

当我指定私钥文件后使用 SSH 拉取仓库时,它失败了并提示了与您相同的警告。以下是我的脚本:

ssh-agent bash -c "ssh-add $PATH_OF_RSA/id_rsa; \
git pull git@gitee.com:someone/somerepo.git "

我只需将模式更改为600就能解决这个问题。

chmod 600 $PATH_TO_RSA/id_rsa

6

将权限设置为400会使密钥变为私有,无法被未知人员访问。它将密钥保护起来。

chmod 400 /Users/tudouya/.ssh/vm/vm_id_rsa.pub

6

这个问题有一个快速简单的解决方法。您只需要使用以下命令更改pem文件的权限即可。

chmod 400 /Users/yourUserName/pathOfYourFile/fileName.pem

此命令将使文件变为只读,并删除文件上的所有其他权限。


5
只需运行以下命令到您的pem文件即可。
sudo chmod 600 /path/to/my/key.pem 

4
这应该能够解决问题:
chmod 600 id_rsa

调用“chmod 600 blah”的请求不够描述性;正如早先的评论所指出的那样,您需要在存储密钥的位置运行此命令。 - xxyjoel

4

我曾经在使用MacBook M1时遇到了这个问题,后来我发现连接脚本中缺少了sudo

sudo ssh -i "key.pem" ubuntu@IP_Address

如果你使用的是MacBook,请确保在连接脚本中不要漏掉sudo


4

如果您正在使用 .ssh/config 文件,请尝试

chmod 0400 .ssh/config

那么:

chmod 0400 .ssh/<<KEYFILE_PATH>>

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