我们如何使用VM上生成的公钥从Mac终端SSH连接到Google Cloud VM?

问题描述 投票:-1回答:2

我们希望使用从Mac终端在VM上生成的公钥连接到Google Cloud VM。但我们看到以下错误

konathal:.ssh konathal$ ssh -i ~/.ssh/runnhostkey1.pub [email protected]

Load key "/Users/konathal/.ssh/runnhostkey1.pub": invalid format
[email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

我们做了什么?

遵循https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys#createsshkeys的文档

1.我们在Google Cloud VM上创建了一个新的公钥

~/.ssh/runnhostkey1
~/.ssh/runnhostkey1.pub 

2.将公钥runnhostkey1.pub内容添加到VM>编辑> SSH密钥

3.在本地mac中处理新文件并复制公钥的内容

$ vi ~/.ssh/runnhostkey1.pub
$ chmod 400 ~/.ssh/runnhostkey1.pub

我们无法使用$ ssh -i ~/.ssh/runnhostkey1.pub [email protected]连接到VM

我们缺少什么?

ssh google-cloud-platform
2个回答
1
投票

我们发现了问题和解决方案:

1)密钥(公共和私有)文件的所有者和组应由用于生成密钥的用户拥有。在我们的例子中,我们以root身份登录,但是用户确定了密钥。

ssh-keygen -t rsa -f ~/.ssh/runnkey -C suren但是我们以root的身份运行了这个文件,因此文件的所有者为root。快速解决方法是更改​​所有权

chown suren:suren ~/.ssh/runnkeychown suren:suren ~/.ssh/runnkey.pub

2)将公钥添加到/.ssh/authorized_keys。

例子cat ~/.ssh/runnkey.pub >> ~/.ssh/authorized_keys

3)更改文件的权限

 chmod 0700 /.ssh/authorised_keys
 chmod 0600 /.ssh/runnkey
 chmod 0600 /.ssh/runnkey

4)将密钥添加到VM实例。计算引擎> VM实例> [您要访问的VM]>编辑> SSH密钥。复制公钥的文本(vi ~/.ssh/runnkey.pub副本)并在此处添加粘贴。保存。

5)在runnkey文件夹下的本地mac上创建文件runnkey.pub/.ssh,并从服务器上的相应文件中复制内容。

示例测试命令:

ssh -i ~/.ssh/runnkey.pub [email protected]
scp -i ~/.ssh/runnkey.pub /Downloads/ship.png [email protected]:/home/temp

上面的1,2,3在document的下面提到了$ HOME或连接用户的$ HOME / .ssh目录的权限是错误的。


0
投票

您无法使用公钥通过SSH进行连接。您必须使用私钥。

VM使用公钥来验证您的连接授权。

很可能你的私钥是:~/.ssh/runnhostkey1

© www.soinside.com 2019 - 2024. All rights reserved.