id_rsa.pub 文件 SSH 错误:格式无效
Posted
技术标签:
【中文标题】id_rsa.pub 文件 SSH 错误:格式无效【英文标题】:id_rsa.pub file SSH Error: invalid format 【发布时间】:2018-06-27 22:44:42 【问题描述】:我和我的同事一直试图让我的 macbook 连接到我们的 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 文件添加到服务器
-
我生成密钥的第一种方式
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
建议在 github documentation for generating a new SSH key
通过我的同事将新的 pub 文件添加到服务器,但不起作用。仍然出现同样的错误。
在删除旧文件后生成 .pub 文件的第二种方法是按照 joyent manually generating your ssh key on OSX documentation 中的建议运行 ssh-keygen -t rsa
还是出现同样的错误
第 4 步)使用自制软件升级 ssh
这是按照here, by Tim Hilliard列出的文档完成的
第 5 步)从我的“已知主机”文件中删除 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 在 cmets 中建议将 -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).
【问题讨论】:
id_rsa.pub
应该拥有0444
或0644
的权限。
不幸的是,我的权限仍然被拒绝@iamauser
您是否在 Windows 终端和 OSX 终端之间移动文件?
不,纯 osx @Alfabravo 生产服务器是 ubuntu
.pub
文件不是 IdentityFile。您应该将id_rsa
,not id_rsa.pub
,放在配置文件中。
【参考方案1】:
IdentityFile
配置参数应指向 SSH 客户端用来向远程服务器证明其身份的 私钥。 (那么,远程服务器应该将id_rsa.pub
的内容安装在其authorized_keys
文件或等效位置)。
您应该将id_rsa
的路径,不 id_rsa.pub
作为IdentityFile
的参数放在~/.ssh/config
中。
【讨论】:
如此简单的事情竟然变得如此困难,真是不可思议。 非常感谢这有效,我的同事的配置文件配置了 id_rsa.pub 和他的 works 这是不应该的,所以我希望这可以帮助某人未来:) @milan,通常都有标准名称,但你是对的,对于 Charles 来说,cmets 更像是一个内部侧边栏,而不是对问题或答案内容的评论。 这样一个大而冗长的问题,一个小而优雅的答案 错误信息不建议使用私钥是犯罪行为。【参考方案2】:您的错误在于将-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
“工作”的他们的同事可能犯了你描述的错误。
我假设 OP 已经发送了他认为是他的公钥的东西,不是。他已发送给对方安装他的私钥,而不是公开的。没有什么可以反驳你的评论,但他已经暴露了他的私钥。他最好生成一个新密钥,不要再使用了。【参考方案3】:
在我的例子中,我试图使用-i
标志来指向默认键:ssh -i ~/.ssh/id_rsa.pub user@0.0.0.0
删除-i
键,只需调用ssh user@0.0.0.0
即可解决错误
【讨论】:
-i ~/.ssh/id_rsa
也是有效的——完全删除它的唯一原因是id_rsa
是默认键名;对于任何非默认密钥,只需要从名称中删除 .pub
以获取私钥。【参考方案4】:
这对我有用。
尝试连接 AWS 服务器时,我收到此错误:加载密钥“yourKey.pem”:格式无效且密钥文件内容没有任何问题。
但生成指纹会产生: 无法加载密钥 <...> :期望:任何私钥...
对我有用的是使用带有 nano 的终端并手动创建一个具有相同名称的全新文件
$ sudo nano yourKey.pem
粘贴原始密钥文件中的文本内容并保存。
然后新密钥的指纹匹配并恢复了 ssh 访问。
希望对您有所帮助。
【讨论】:
【参考方案5】:应设置以下权限:
私钥应该是 rw 对于所有者,none 对于组,none 对于其他人:
chmod 600 id_rsa
公钥应该为所有者获得 rw,为组获得 r,其他人获得 r:
chmod 644 id_rsa.pub
【讨论】:
“~/.ssh/id_rsa.pub”的权限 0644 太开放了。【参考方案6】:据说这个问题的主要错误是关于混淆了公钥和私钥,但同样的错误也可能由于其他原因而发生。对于那些只搜索 invalid format
错误的人来说,这可能也有帮助,它只处理私钥:
我在尝试时遇到了类似的错误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 format
和read_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。
【讨论】:
以上是关于id_rsa.pub 文件 SSH 错误:格式无效的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Java 生成 ssh 兼容的 id_rsa(.pub)