我在我的 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).
-i id_rsa.pub
时,我遇到了这个问题。似乎强制在 ssh 命令强制使用一些公钥来询问密码(即使密码是空的,至少在我的情况下)
我建议你这样做:
chmod 400 ~/.ssh/id_rsa
这对我来说可以。
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
.pub
) 文件而不是私有文件。
ssh -i id_ed25519.pub
而执行 ssh -i id_ed25519
时收到此错误
登录用户应该可以读取密钥。
尝试这个:
chmod 400 ~/.ssh/Key file
chmod 400 ~/.ssh/vm_id_rsa.pub
ssh-add ~/.ssh/id_rsa.pub
时得到无效格式
chmod 400 path/to/filename
这对我有用。当我做这个文件时,我能够连接到我的 EC2 实例
运行以下命令后,它对我有用
sudo chmod 600 /path/to/my/key.pem
就我而言,它是一个 .pem 文件。事实证明这也适用。更改了文件的权限并且它起作用了。
chmod 400 ~/.ssh/dev-shared.pem
感谢所有在上面提供帮助的人。
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/
如果密钥在 ~/.ssh 目录中,请使用
chmod 400 ~/.ssh/id_rsa
如果密钥在不同的目录中,请使用
chmod 400 目录路径/id_rsa
这对我有用。
chmod 400 ~/.ssh/id_rsa
这就是你的意思 @Anirban。如上所述: 带有“.pub”扩展名的文件是公钥文件。对应的不带“.pub”扩展名的文件就是私钥文件。我们需要保护私钥。
很多类似的答案,但没有解释......
由于私钥文件权限太开放而引发错误。这是一个安全风险。
将私钥文件的权限更改为最小(所有者只读)
更改所有者 chown
chmod 600 id_rsa
从密钥存储在 vm ex 中的路径运行上述命令:cd /home/opc/.ssh
我有类似的问题,并通过更改为我工作的相应文件和文件夹的权限来解决它。
这是对我有用的解决方案:
$ chmod 0600 ~/.ssh/id_rsa.pub
$ chmod 0600 ~/.ssh/authorized_keys
$ chmod 0600 ~/.ssh/id_rsa
$ chmod 0700 ~/.ssh
至于我,id_rsa
的默认模式是600
,即readable
和writable
。
在我将此文件推送到 git repo 并从另一台电脑中提取后,有时私钥文件的模式变为 -rw-r--r--
。
当我在指定私钥文件后使用 ssh 拉取 repo 时,它失败并提示与您相同的警告。以下是我的脚本。
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
您必须运行以下命令
chmod 400 /path/to/my/key.pem
授予权限 400 使密钥成为私有密钥,并且无法被未知的人访问。它使密钥成为受保护的密钥。
chmod 400 /Users/tudouya/.ssh/vm/vm_id_rsa.pub
只需在您的 pem 下方运行
sudo chmod 600 /path/to/my/key.pem
如果您使用的是 .ssh/config 文件,请尝试
chmod 0400 .ssh/config
然后:
chmod 0400 .ssh/<<KEYFILE_PATH>>
这应该可以解决问题:
chmod 600 id_rsa
chmod 600 blah
的请求描述性不够;如之前的评论所述,您需要在存储密钥的位置运行此命令
chmod 400 /etc/ssh/*
适合我。
那些建议 chmod 400 id_rsa.pub 的人听起来根本不对。 op 很可能使用 pub 密钥而不是 ssh 的私钥。
因此,修复它可能就像 ssh -i /Users/tudouya/.ssh/vm/vm_id_rsa (the private key) user@host
一样简单。
- - 更新 - -
查看这篇文章 https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2,了解如何设置 ssh 密钥
在我的情况下,如果发现这个文件实际上不是我需要的文件,因为显然我用来创建 ssh 密钥 ssh-keygen -t rsa
的命令
使用我使用的名称在我正在工作的文件目录中为我创建了密钥。
因此,检查您正在工作的目录中的隐藏文件,您可能只会看到 ssh 和 ssh.pub
我在使用 macbook m1 时遇到了这个问题,后来我意识到连接脚本中缺少 sudo
sudo ssh -i "key.pem" ubuntu@IP_Address
如果您使用的是 macbook,请确保您没有丢失连接脚本中的 sudo
。
上面已经有很多很好的解释了,所以我建议阅读和理解。
这是我的简单分步解决方案:
在您的终端上,运行: open ~/.ssh/config 在您的文件中,您将看到与此类似的内容(在我个人的情况下):
Host *
IgnoreUnknown UseKeychain
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519.pub
或像这样(根据this question中的示例)。
Hostname 10.211.55.17
PreferredAuthentications publickey
IdentityFile /Users/tudouya/.ssh/vm/vm_id_rsa.pub
从最后一行删除“.pub”扩展名,应该如下所示:
Hostname 10.211.55.17
PreferredAuthentications publickey
IdentityFile /Users/tudouya/.ssh/vm/vm_id_rsa
或者在我的情况下:
Host *
IgnoreUnknown UseKeychain
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519
保存文件并测试您的 ssh 连接。
我删除了 .pub file
,它起作用了。
ssh-keygen -i -f /path/to/private.key > /desired/path/to/public.key
从私钥重新生成丢失的公钥文件。所以,它并没有真正丢失。 :)
“Permissions 0644 for 'id_rsa.pub' are too open."
并且密钥因此被忽略。这可能是因为我从另一台 PC 复制了密钥文件。但是修复权限解决了这个问题。谢谢!