简体   繁体   English

如何在 Mac 终端上通过 ssh 登录使用私钥? 我的尝试导致`加载密钥“/path/to/privatekey”:无效格式`

[英]How to use private key with ssh login on Mac terminal? My attempts result in `Load key “/path/to/privatekey”: invalid format`

I usually use putty on windows for ssh login, which has an option to add a private key for logging in.我通常在 windows 上使用 putty 进行 ssh 登录,它可以选择添加用于登录的私钥。

I switched to Mac and I am using iTerm2 to attempt to log into the server.我切换到 Mac 并使用 iTerm2 尝试登录服务器。 I tried the command ssh user@serverIPAddress -p portNumber but I got Permission denied (publickey).我尝试了命令ssh user@serverIPAddress -p portNumber但我得到了Permission denied (publickey). , which I am guessing is because I didn't add my private key like I was doing with Putty. ,我猜这是因为我没有像使用 Putty 那样添加我的私钥。

I tried looking up how to add my private key here我尝试在此处查找如何添加我的私钥

https://docs.rackspace.com/support/how-to/logging-in-with-an-ssh-private-key-on-linuxmac/ https://docs.rackspace.com/support/how-to/logging-in-with-an-ssh-private-key-on-linuxmac/

I saved my private key file from my windows laptop and saved it into my mac.我从我的 Windows 笔记本电脑中保存了我的私钥文件并将其保存到我的 mac 中。 I think tried我想尝试过

ssh -i /path/to/privatekey user@serverIPAddress -p portNumber

But then I got this但后来我得到了这个

Permissions 0644 for '/path/to/privatekey' are too open. “/path/to/privatekey”的权限 0644 太开放了。 It is required that your private key files are NOT accessible by others.要求其他人无法访问您的私钥文件。 This private key will be ignored.此私钥将被忽略。

I looked at the website and it had instructions to secure the file, so I tried:我查看了网站,它有保护文件的说明,所以我尝试了:

chmod 600 /path/to/privatekey

And tried并尝试

ssh -i /path/to/privatekey user@serverIPAddress -p portNumber

Again, but then I got再一次,但后来我得到了

Load key "/path/to/privatekey": invalid format.加载密钥“/path/to/privatekey”:格式无效。

I open the file and noticed that it's not in same format as in the the webpage.我打开文件并注意到它的格式与网页中的格式不同。 My file is formatted like this我的文件格式如下

PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: rsa-key-20200908
Public-Lines: 6
public key
Private-Lines: 14
private key

So I remove everything everything except the private key and I add '-----BEGIN RSA KEY-----' and '-----END RSA KEY-----' like on the website.因此,我删除了除私钥之外的所有内容,并在网站上添加了“-----BEGIN RSA KEY-----”和“-----END RSA KEY-----”。

But I still get但我仍然得到

Load key "/path/to/privatekey": invalid format.加载密钥“/path/to/privatekey”:格式无效。

Any ideas on where I went wrong?关于我哪里出错的任何想法?

TLDR: Putty uses a different format TLDR:Putty 使用不同的格式

You're not really using iTerm2 to 'login to the server'.您并没有真正使用 iTerm2 来“登录到服务器”。 You're using the OpenSSH client program ssh , run from a shell in iTerm2, to do so.您正在使用 OpenSSH 客户端程序sshiTerm2 中的 shell 运行,来执行此操作。 The problem is between Putty and OpenSSH.问题出在 Putty 和 OpenSSH 之间。 Putty uses its own format for privatekey files, which is different from the formats OpenSSH uses. Putty 使用自己的私钥文件格式,这与 OpenSSH 使用的格式不同。 Putty cannot use an OpenSSH keyfile, and OpenSSH cannot use a Putty keyfile. Putty 不能使用 OpenSSH 密钥文件,OpenSSH 不能使用 Putty 密钥文件。

Changing the PEM labels on a file does not change its contents.更改文件上的 PEM 标签不会更改其内容。 BTW, the PEM labels normally used by OpenSSH below 7.8 for an RSA keyfile are -----BEGIN/END RSA PRIVATE KEY----- , as shown on the webpage you link, NOT RSA KEY as you say.顺便说一句,低于 7.8 的 OpenSSH 通常用于 RSA 密钥文件的 PEM 标签是-----BEGIN/END RSA PRIVATE KEY----- ,如您链接的网页所示,而不是您所说的RSA KEY But putting either of these on an otherwise unchanged Putty keyfile is like taking a box of rat poisin and putting a label on it that says 'candy'.但是,将其中任何一个放在未更改的 Putty 密钥文件中就像拿一盒老鼠药并在上面贴上“糖果”标签。 The contents are still rat poison, not candy, and anyone who eats them thinking they are candy is in big trouble.里面的东西还是老鼠药,不是糖果,谁吃了以为是糖果就麻烦大了。

The Putty-related program PuttyGen can convert between these formats.油灰相关程序的puttygen可以在这些格式之间转换 See dupe from years ago when the scope of SO included non-programming questions like this:看到多年前的欺骗,当时 SO 的范围包括这样的非编程问题:
How to convert SSH keypairs generated using PuTTYgen (Windows) into key-pairs used by ssh-agent and Keychain (Linux) 如何将使用 PuTTYgen (Windows) 生成的 SSH 密钥对转换为 ssh-agent 和 Keychain (Linux) 使用的密钥对
and crossdupes:和交叉:
https://security.stackexchange.com/questions/146360/ssh-keypair-works-in-putty-but-not-in-openssh-inside-the-puttys-session https://security.stackexchange.com/questions/146360/ssh-keypair-works-in-putty-but-not-in-openssh-inside-the-puttys-session
https://unix.stackexchange.com/questions/48303/using-rsa-private-key-from-putty-in-filezilla https://unix.stackexchange.com/questions/48303/using-rsa-private-key-from-putty-in-filezilla
https://unix.stackexchange.com/questions/74545/what-is-the-difference-between-an-openssh-key-and-putty-key https://unix.stackexchange.com/questions/74545/what-is-the-difference-between-an-openssh-key-and-putty-key
https://superuser.com/questions/990275/generate-rsa2-key-in-openssh-format https://superuser.com/questions/990275/generate-rsa2-key-in-openssh-format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM