我在我的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 ubuntu tuvm”,显示如下:

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).

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文件,它工作了。

我建议你这样做:

chmod 400 ~/.ssh/id_rsa

这对我来说很有效。

密钥应该是可读的登录用户。

试试这个:

chmod 400 ~/.ssh/Key file
chmod 400 ~/.ssh/vm_id_rsa.pub

Chmod 400 /etc/ssh/*适用于我。

chmod 400 path/to/filename

这对我很有用。当我处理这个文件时,我能够连接到我的EC2实例

那些建议chmod 400 id_rsa的人。酒吧听起来一点也不对。op很可能使用pub密钥而不是私钥来ssh。

因此,它可能就像ssh -i /Users/tudouya/一样简单。Ssh /vm/vm_id_rsa(私钥)user@host进行修复。

——更新——

查看这篇文章https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2了解如何设置ssh密钥

如果键在~/。SSH目录,使用

chmod 400 ~/.ssh/id_rsa

如果密钥在不同的目录,请使用

chmod 400方向/id_rsa

这对我很管用。

如果您使用的是.ssh/config文件,请尝试

chmod 0400 .ssh/config

然后:

chmod 0400 .ssh/<<KEYFILE_PATH>>

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

当我将这个文件推到git repo并从另一台pc上拉出它后,有时私钥文件的模式变成-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

在我的例子中,它是一个.pem文件。事实证明这也适用。更改了文件的权限,它工作了。

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

感谢上面所有帮助过我的人。

授予权限400将使密钥成为私有的,无法被未知的人访问。它使钥匙成为受保护的钥匙。

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

很多类似的答案,但没有解释…

引发此错误是因为私钥文件权限太开放。这是一个安全风险。

将私钥文件的权限更改为最小(所有者只读)

更改所有者chown <unix-name> <private-key-file> 设置最小权限(只读文件所有者)chmod 400 <private-key-file>

跑到下面的pem

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

在运行以下命令后,它为我工作

sudo chmod 600 /path/to/my/key.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/

chmod 600 id_rsa

从虚拟机ex中存放key的路径执行上述命令:cd /home/ opt /.ssh

你必须运行下面的命令

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

这应该可以达到目的:

chmod 600 id_rsa

我有类似的问题,并通过更改各自文件和文件夹的权限来解决它。

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

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

更改您的KEY权限为

Chmod 400 your_key.pem

它应该能起作用!

上面有很多很好的解释,所以我建议你阅读和理解。

以下是我的简单一步一步的解决方案:

在终端上运行: 开放的~ / . ssh / config 在你的文件中,你会看到类似的东西(在我个人的情况下):

Host *
  IgnoreUnknown UseKeychain
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519.pub

或者像这样(就像这个问题中的例子)。

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连接。

我在macbook m1上有这个问题,我后来意识到我从连接脚本中丢失了sudo

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

如果您使用的是macbook,请确保在连接脚本中没有遗漏sudo。

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

chmod 400 / uses / yo乌拉圭人/ pathofyour文件/文件.pem

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

我也有类似的问题,我就是这么做的,而且很有效。

Sudo SSH -i webhost。pem ubuntu@ipaddress