id_rsa.pub 文件 SSH 错误:格式无效

67

我和我的同事一直尝试让我的Macbook可以通过ssh连接到我们的AWS生产服务器,但是一直没有成功。这在几个月前曾经成功过,但是昨天再次尝试时却一直出现以下错误信息。

(在下面的部分中,我已经用占位符替换了我的用户名和主机名)

$ ssh titan-data
Load key "/Users/aronlilland/.ssh/id_rsa.pub": invalid format
user@my_ip_address: Permission denied (publickey).

相关配置文件数据

<!-- config file -->
Host titan-data
    HostName my_ip_address
    User user
    IdentityFile ~/.ssh/id_rsa.pub
    AddKeysToAgent yes

我已经尽可能地完成了每一步来让它再次正常工作,以下是我按顺序完成的步骤:

步骤1)确保在服务器上添加了.pub文件

这一步骤被反复执行了多次,我在此过程中重新生成了我的.pub文件4次。看起来错误是本地问题,而不是服务器问题,如果我错了,有人需要纠正我。

步骤2)检查以下文件夹和文件是否具有正确的权限

drwx------    6 aronlilland  staff   192B Jan 18 09:41 .ssh/

drwx------    6 aronlilland  staff   192B Jan 18 09:41 ./
drwxr-xr-x+ 104 aronlilland  staff   3.3K Jan 16 16:09 ../
-rw-r--r--    1 aronlilland  staff   114B Jan 18 09:37 config
-rw-------    1 aronlilland  staff   1.7K Jan 18 09:41 id_rsa
-r--------@   1 aronlilland  staff   414B Jan 18 09:41 id_rsa.pub
-rw-r--r--    1 aronlilland  staff   4.6K Jan 18 09:48 known_hosts

这没有任何作用来修复错误消息。

步骤3) 删除了我的id_rsa文件和id_rsa.pub文件并重新生成了一个新的,让我的同事将新的.pub文件添加到服务器中

  1. 我生成密钥的第一种方法是运行 ssh-keygen -t rsa -b 4096 -C "your_email@example.com",如github文档中为生成新SSH密钥所建议的
    • 通过我的同事将新的pub文件添加到服务器中,但仍然无法解决问题。
  2. 我删除旧文件后生成.pub文件的第二种方式是运行 ssh-keygen -t rsa,如joyent手动在OSX上生成ssh密钥的文档中所建议的
    • 仍然得到相同的错误消息。

步骤4) 用homebrew升级ssh

按照Tim Hilliard的文章所列的文档进行了操作。

步骤5) 从我的“known hosts”文件中删除IP地址(抓狂)

没有任何作用,只需再次确认IP地址即可。

编辑1)

我最近做的一件事是我安装了另一个版本的Python,我记得在安装过程中看到了错误消息,因此我的计算机上安装了两个版本的Python。我不确定这是否会对ssh造成任何问题。

$ python -V
Python 2.7.10
$ python3 -V
Python 3.6.4
$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. 
Thanks!

Warning: Python is installed at /Library/Frameworks/Python.framework

Homebrew only supports building against the System-provided Python or a
brewed Python. In particular, Pythons installed to /Library can interfere
with other software installs.

编辑2)

根据评论中Alfabravo的建议,将-v添加到ssh命令中。

aronlilland:.ssh$ ssh titan-data -v
OpenSSH_7.6p1, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /Users/aronlilland/.ssh/config
debug1: /Users/aronlilland/.ssh/config line 1: Applying options for titan-data
debug1: Reading configuration data /usr/local/etc/ssh/ssh_config
debug1: Connecting to my_ip_address [my_ip_address] port 22.
debug1: Connection established.
debug1: identity file /Users/aronlilland/.ssh/id_rsa.pub type 0
debug1: key_load_public: No such file or directory
debug1: identity file /Users/aronlilland/.ssh/id_rsa.pub-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.2 pat OpenSSH* compat 0x04000000
debug1: Authenticating to my_ip_address:22 as 'user'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:2l8QDc7x/fyzshKaU32jj9MeFtMAccu25MZBFNQuStQ
debug1: Host 'my_ip_address' is known and matches the ECDSA host key.
debug1: Found key in /Users/aronlilland/.ssh/known_hosts:19
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:MuOjm+6gAzaxGr/n/If4LVjk/0H5/VT6zvib9/9C7c8 /Users/aronlilland/.ssh/id_rsa.pub
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
Load key "/Users/aronlilland/.ssh/id_rsa.pub": invalid format
debug1: No more authentication methods to try.
user@my_ip_address: Permission denied (publickey).
aronlilland:.ssh$ 

修改 3)

检查 .pub 文件是否是有效的格式。

aronlilland:.ssh$ ssh-keygen -l -f id_rsa.pub
2048 SHA256:MuOjm+6gAzaxGr/n/If4LVjk/0H5/VT6zvib9/9C7c8 aronlilland@arons-mbp.cacc.local (RSA)
aronlilland:.ssh$ touch foo.txt
aronlilland:.ssh$ ls
config       foo.txt      id_rsa       id_rsa.pub   known_hosts
aronlilland:.ssh$ ssh-keygen -l -f foo.txt
foo.txt is not a public key file.

编辑4)

确认我的id_rsa密钥和id_rsa.pub公钥有效(我删掉了我的ssh-rsa密钥的结尾,在网上没有必要包含它)

aronlilland:.ssh$ ssh-keygen -y -f id_rsa
Enter passphrase: 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQA .......

确认更新文件权限后访问权限未发生变化(我尝试了0444、444、0644、644以确保)。

aronlilland:.ssh$ chmod 0444 id_rsa.pub
aronlilland:.ssh$ ssh titan-data
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0444 for '/Users/aronlilland/.ssh/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.
Load key "/Users/aronlilland/.ssh/id_rsa.pub": bad permissions
user@my_ip_address: Permission denied (publickey).

另一个

aronlilland:.ssh$ chmod 0644 id_rsa.pub
aronlilland:.ssh$ ssh titan-data
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/aronlilland/.ssh/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.
Load key "/Users/aronlilland/.ssh/id_rsa.pub": bad permissions
user@my_ip_address: Permission denied (publickey).

将其设置回chmod 400 id_rsa.pub

编辑5)

运行-vv

aronlilland:.ssh$ chmod 400 id_rsa.pub
aronlilland:.ssh$ ssh titan-data -vv
OpenSSH_7.6p1, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /Users/aronlilland/.ssh/config
debug1: /Users/aronlilland/.ssh/config line 1: Applying options for titan-data
debug1: Reading configuration data /usr/local/etc/ssh/ssh_config
debug2: resolving "my_ip_address" port 22
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to my_ip_address [my_ip_address] port 22.
debug1: Connection established.
debug1: identity file /Users/aronlilland/.ssh/id_rsa.pub type 0
debug1: key_load_public: No such file or directory
debug1: identity file /Users/aronlilland/.ssh/id_rsa.pub-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.2 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to my_ip_address:22 as 'user'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,zlib
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com
debug2: compression stoc: none,zlib@openssh.com
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:2l8QDc7x/fyzshKaU32jj9MeFtMAccu25MZBFNQuStQ
debug1: Host 'my_ip_address' is known and matches the ECDSA host key.
debug1: Found key in /Users/aronlilland/.ssh/known_hosts:19
debug2: set_newkeys: mode 1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey after 134217728 blocks
debug2: key: /Users/aronlilland/.ssh/id_rsa.pub (0x7fe9ff41c510), explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:MuOjm+6gAzaxGr/n/If4LVjk/0H5/VT6zvib9/9C7c8 /Users/aronlilland/.ssh/id_rsa.pub
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug2: input_userauth_pk_ok: fp SHA256:MuOjm+6gAzaxGr/n/If4LVjk/0H5/VT6zvib9/9C7c8
Load key "/Users/aronlilland/.ssh/id_rsa.pub": invalid format
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
user@my_ip_address: Permission denied (publickey).

1
"id_rsa.pub" 应该拥有权限 "0444" 或 "0644"。 - iamauser
很不幸,我仍然遭遇了“Permission Denied(权限被拒绝)”的错误,@iamauser。 - ochero
14
.pubж–Ү件дёҚжҳҜиә«д»ҪйӘҢиҜҒж–Ү件пјҲIdentityFileпјүпјҢдҪ еә”иҜҘеңЁй…ҚзҪ®ж–Ү件дёӯж”ҫзҪ®id_rsaиҖҢдёҚжҳҜid_rsa.pubгҖӮ - Charles Duffy
1
顺便说一句,这通常不在StackOverflow的范围内 - 我们欢迎关于编写软件的问题,而不是使用软件的问题。考虑将来有关使用标准UNIX工具的非开发特定问题,请参阅[unix.se]或SuperUser - Charles Duffy
第一个错误很明显...你看过你的.ssh/id_rsa.pub文件了吗?它说格式无效,而你没有在这里发布它...你能发一下吗?最有可能的事情是你不知道地编辑了它并且毁掉了它。 - Luis Colorado
显示剩余9条评论
7个回答

228

IdentityFile 配置参数应该指向 SSH 客户端用来向远程服务器证明身份的私钥。(然后,远程服务器应该在其authorized_keys文件或等效位置中安装id_rsa.pub的内容)。

您应该将id_rsa的路径而不是id_rsa.pub作为参数放入到您的~/.ssh/config文件中的IdentityFile中。


36
简单的事情变得复杂了,真是令人惊讶。 - David C. Rankin
非常感谢这个有效,我的同事的配置文件配置了id_rsa.pub,他的可以工作,但实际上不应该这样,所以我希望这可能会帮助将来的某个人 :) - ochero
哇,我一直在向ssh提供.pub文件...这个“服务器接受密钥”消息非常误导。 - daveloyall
4
如此冗长的问题,却有一个简洁优美的答案。 - ZaidRehman
3
错误信息没有提示使用私钥是不妥的,这是违法的。 - user3673
显示剩余6条评论

3
以下权限应该设置:
私钥应该为所有者设置 rw,对于组和其他人则为 none
chmod 600 id_rsa

公钥应该为所有者获取rw,对于组和其他人则为r
chmod 644 id_rsa.pub

6
“~/.ssh/id_rsa.pub” 的权限为 0644,过于开放。 - itsazzad

1

以下是对我有效的解决方法。

在尝试连接到AWS服务器时,我遇到了这个错误:Load key "yourKey.pem": invalid format,但密钥文件内容没有问题。

但生成指纹会产生以下结果: unable to load key <...> :Expecting: ANY PRIVATE KEY...

对我有效的方法是使用终端和nano手动创建一个具有相同名称的全新文件。

$ sudo nano yourKey.pem

粘贴原始密钥文件的文本内容并保存。

然后新密钥的指纹匹配,ssh访问得以恢复。

希望这可以帮助您。


1

这可能是对这个问题的答案,但一般来说,IdentityFile并非必须指定私钥,也可以处理公钥。如果您的私钥已经加载到SSH代理中,则使用公钥作为IdentityFile(或-i选项)是完全可以的。

一旦您的代理程序加载了5个或更多的密钥,连接某些服务器时就会遇到问题,因为可用的身份标识将超过服务器的MaxAuthTries(“Too many authentication failures”)。然后您要么必须从代理程序中删除密钥,要么使用-i来指定代理程序应使用哪个私钥进行签名。

如果您正在使用代理程序,并提供一个有效的公钥文件作为-i参数,仍然收到“无效格式”的错误消息,请仔细检查您的SSH客户端是否能够与SSH代理程序通信。根据我的经验,WSL(2)尤其容易丢失套接字(SSH_AUTH_SOCK环境变量)。


目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

0

该问题的主要错误被认为是混淆了公钥和私钥,但同样的错误也可能出现在其他原因上。对于那些仅搜索“无效格式”错误的人来说,这也可能有所帮助,它只涉及私钥:

当我尝试时,我遇到了类似的错误Load key "/root/.ssh/id_rsa": invalid format

RUN echo "$ssh_prv_key" > /root/.ssh/id_rsa &&     chmod 600 /root/.ssh/id_rsa

这导致了错误,如identity file /root/.ssh/id_rsa type -1 invalid formatread_passphrase: can't open /dev/tty
正确的方法是使用:
COPY id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa

解决方案:我的私钥格式错误 - 它被传递为一行而不是多行,您可能还有其他格式问题,例如在开头或结尾忘记了“-”,或者在行末出现问题,例如缺少换行符格式或行末附加字母。
有关更多详细信息,请参见Dockerfile: clone repo with passwordless private key. Errors: “authentication agent” or “read_passphrase: can't open /dev/tty”,主要思路来自Add private key to ssh-agent in docker file,该思路又来自Gitlab CI/Docker: ssh-add keeps asking for passphrase

0

你的错误在于使用了-f选项与公钥名称... 你必须生成一个新的密钥,因为你已经通过将其存储在-f .ssh/id_rsa.pub中发送(并泄漏)了你的私钥。

默认情况下,私钥存储在.ssh/id_rsa中,公钥存储在.ssh/id_rsa.pub中。通过指定.ssh/id_rsa.pub作为你的私钥位置,你很可能会在.ssh/id_rsa.pub.pub中拥有公钥(请核对此内容)。但无论如何,你已经通过网络发送了你的私钥,所以它已经被泄漏了,你最好生成一个新的密钥对。

下次,你可以信任为密钥文件提供的默认设置或使用适当的密钥文件(不带.pub扩展名)。


如果 OP 真的按照您在此处提出的建议去做了,那么 (1) 使用 IdentityFile id_rsa 将无法解决问题,因此他们不会接受我的答案;(2) 他们不会因为将先前的(.pub)文件用作私钥而收到“格式无效”的错误,因为其中实际上会有一个私钥。现在,对于使用 IdentityFile id_rsa.pub “有效”的他们的同事可能确实犯了您所描述的错误。 - Charles Duffy
我假设 OP 发送了他认为是自己的公钥,但实际上不是。他发送给对方安装他的私钥,而不是公钥。没有什么可以反驳你的评论,但他已经暴露了他的私钥。他最好生成一个新的密钥,不再使用它。 - Luis Colorado

-1
在我的情况下,我试图使用-i标志来指向默认密钥:ssh -i ~/.ssh/id_rsa.pub user@0.0.0.0 删除-i密钥,仅调用ssh user@0.0.0.0解决了错误。

4
如果您使用的是非默认密钥,只删除名称中.pub部分以获取私钥将是必要的。 -i ~/.ssh/id_rsa也是有效的选项,但它被移除了,因为id_rsa是默认密钥名称。 - Charles Duffy

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