SSH错误:未知的密钥类型'----- BEGIN'


8

我在使用授权密钥通过SSH登录到远程服务器时遇到问题。我收到的错误消息如下所示:

OpenSSH_5.2p1, OpenSSL 0.9.8r 8 Feb 2011
debug1: Reading configuration data /etc/ssh_config
debug1: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to xx.xx.xx [xxx.xx.xx.xx] port 22.
debug1: Connection established.
debug3: Not a RSA1 key file /Users/bfenker/.ssh/id_rsa.
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug3: key_read: missing keytype
debug3: key_read: missing whitespace
...
debug2: key_type_from_name: unknown key type '-----END'
debug3: key_read: missing keytype
debug1: identity file /Users/bfenker/.ssh/id_rsa type 1
ssh_exchange_identification: Connection closed by remote host

该网站上的其他问题也发布了类似的问题,解决方案通常是仔细检查客户端的所有权限,我已经这样做了:

drwxr-xr-x+ 23 bfenker          staff   782 May  8 11:02 bfenker
drwx------   8 bfenker          staff   272 May  8 10:05 .ssh
-rw-------   1 bfenker  staff  1675 May  8 09:51 id_rsa
-rw-r--r--   1 bfenker  staff   418 May  8 09:51 id_rsa.pub
-rw-------   1 bfenker  staff   999 May  8 09:46 identity
-rw-r--r--   1 bfenker  staff   663 May  8 09:46 identity.pub
-rw-r--r--   1 bfenker  staff   416 May  8 09:06 known_hosts

我能够使用授权密钥将SSH SSH到另一个服务器,并从该服务器SSH加密到我想要的服务器。这是我尝试解决的可解决方法,但我认为这也表明我的客户端和服务器都设置正确。

请注意,当我将SSH成功​​连接到其他服务器时,会收到相同的错误消息,但是从以下几行开始似乎可以恢复:

debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0

有谁知道为什么这在某些情况下有效,但在我想要的情况下无效?任何其他建议将不胜感激!


您是否更改了服务器/etc/hosts.allow/etc/hosts.deny文件?
迈克尔·汉普顿

Answers:


13

死灵问题!基于您可以使用此密钥登录另一台服务器的事实,@ michael-hampton正确无误:目标服务器上有某些东西(防火墙/ tcp包装器/ sshd配置)拒绝访问。所有这些关于不正确的密钥格式的讨论都是基于对调试信息的不正确解释而引起的。线

debug1: identity file /Users/bfenker/.ssh/id_rsa type 1

表示ssh能够理解密钥。


4
这应该更高一些,我拥有与此完全相同的日志,但是最终确实了解了SSH密钥,并且由于其他原因我没有连接。为什么它在日志中说它无法理解密钥,而实际上却无法理解,这超出了我的范围。
mgrandi

它尝试首先以单行格式读取它,如果失败,则将其输出到详细日志,然后也尝试其他格式,这将成功。
萨莫斯(Samoth)

8

您的SSH密钥存储格式错误。OpenSSH使用放在一行中的密钥。你需要ssh-keygen-i-m选项,请参阅man ssh-keygen。可能是其中之一:

ssh-keygen -m RFC4716 -i -f /Users/bfenker/.ssh/id_rsa

将输出用作新的密钥文件(ssh-keygen ... >newkeyfile)。

编辑1:

请注意:“此选项将读取未加密的私有(或公共)密钥文件”

因此,可能必须通过理解该格式的程序将文件更改为无密码的文件。


感谢您的回复。我的ssh-keygen应用程序没有-m选择,并且尝试不使用该-m选项会给我这个错误: buffer_get_string_ret: bad string length 813827235 key_from_blob: can't read key type decode blob failed.
fenkerbb

然后,您应该检查SSH软件的文档或在普通的Linux系统(您信任的系统)上进行转换。
Hauke Laging,

哈!“普通” Linux!我使用的是MacOSX,因此我认为我的操作系统可能是这里的问题。
fenkerbb

@fenkerbb不是您的操作系统,而是您(=您的操作系统)SSH实现的默认密钥格式。putty在Windows下,您将遇到同样的问题。但是油灰太好了,可以很方便地提供两种格式...(至少对于公共密钥而言)
Hauke Laging

我已尝试使用其他版本的ssh-keygen命令执行此命令,但出现此错误buffer_get_string_ret: bad string length 813827235 。我的密钥文件的第一行是-----BEGIN RSA PRIVATE KEY-----,从下一行开始是一长串字母数字字符。@Hauke Laging
fenkerbb

2

我觉得这里的答案不是很清楚。马克·瓦格纳(Mark Wagner)的回答确实涵盖了该问题,但并未完全说明情况。

输出中的行以调试级别作为前缀,这也暗示了它们的重要性:但是数字越小意义越重要。这些debug3:东西远不如重要debug1:

读取密钥文件后,ssh首先尝试将其解析为已弃用的RSA密钥(现在称为“ RSA1”),这些密钥以SSH PRIVATE KEY FILE FORMAT和版本号开头。新的RSA密钥全部启动-----BEGIN RSA PRIVATE KEY-----。这是一个登录尝试,其中identity是旧的RSA1样式密钥,id_rsa也是新样式。

OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to localhost [::1] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/identity type 0
debug1: identity file /home/user/.ssh/identity-cert type -1
debug3: Not a RSA1 key file /home/user/.ssh/id_rsa.
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug3: key_read: missing keytype
debug3: key_read: missing whitespace
[...]
debug2: key_type_from_name: unknown key type '-----END'
debug3: key_read: missing keytype
debug1: identity file /home/user/.ssh/id_rsa type 1
debug1: identity file /home/user/.ssh/id_rsa-cert type -1
debug1: identity file /home/user/.ssh/id_dsa type -1
debug1: identity file /home/user/.ssh/id_dsa-cert type -1
debug1: identity file /home/user/.ssh/id_ecdsa type -1
debug1: identity file /home/user/.ssh/id_ecdsa-cert type -1

在这一点上,已经确定了这些文件的密钥类型identitytype 0id_rsa作为type 1。它检查的其他文件不存在,因此存在type -1

debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3

由于这是协议2,因此在密钥identity交换期间将忽略协议1 RSA密钥输入。

debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /home/user/.ssh/id_rsa (0x5622d77426a0)
debug2: key: /home/user/.ssh/id_dsa ((nil))
debug2: key: /home/user/.ssh/id_ecdsa ((nil))

在这里,它使我们想起了要使用的密钥文件。不知道为什么它没有拒绝丢失的文件,只有协议1文件,但是...

debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
[...]
debug1: Next authentication method: publickey
debug1: Offering public key: /home/user/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug3: Wrote 372 bytes for a total of 1689
debug1: Server accepts key: pkalg ssh-rsa blen 277

在这里,它提供了id_rsa密钥并被接受。

简而言之,问题标题中的问题是一个红色的鲱鱼,它来自ssh尝试多种方法来解析找到的键,而不是键的真正问题。


1

首先,您应该检查sshd日志。即

less /var/log/secure

根据带有安全日志的UNIX分发文件,可能会有所不同。但是,如果找到,则应说明无法登录的原因。


1

我最近也遇到了这个问题。就我而言,所有权限都是正确的,包括.ssh,rsa文件,主目录以及与用户相关的所有内容。问题是我在.ssh中有一个先前生成的公钥,它与我用于登录的私钥不对应。从.ssh删除公钥可解决此问题。

我正在使用ssh-keygen创建.ssh目录,该目录导致了此发布密钥,因此产生了问题。


0

在装有MacOS 10.7.5的MacBook Pro上,我遇到了同样的问题。我的密钥没有任何问题,只是它们已加密(使用密码,就像您应该做的那样)并且没有被ssh正确解密。似乎ssh-agent有问题。

根据本文,尝试以下操作:

  1. /usr/bin/ssh-agent你的登录项目(系统偏好设置- >用户和组- >选择用户- >登录项)。/usr/bin在对话框中导航至它非常困难,因此本文建议在您的主目录(ln -s /usr/bin/ssh-agent)中建立一个链接,将其放入“登录项”后可以将其删除。

  2. 退出Terminal.app

  3. 重新启动机器。

  4. 打开终端,然后重试您的ssh命令。

为我工作(至少有一次)。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.