如何使用 putty 生成 OpenSSH 格式的密钥:

如何使用 putty 生成 OpenSSH 格式的密钥:

我启动了一个 AWS EC2 实例。我可以使用 Amazon 生成的 SSH 私钥以用户 ec2-user 的身份连接到我的实例。

我按照以下说明进行操作:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/managing-users.html为新用户“bob”添加凭证。

我创建了一个新的用户 bob,并使用 PuTTY Key Generator 生成了一组新的公钥+私钥。我将公钥移至我的 EC2 实例上的 /home/bob/.ssh/authorized_keys

我将 /home/bob/.ssh 和 /home/bob/.ssh/authorized_keys 的所有者和组更改为 bob:bob

我将 /home/bob/.ssh 的权限更改为 700

我将 /home/bob/.ssh/authorized_keys 的权限更改为 600

当我尝试以 bob 的身份使用 bob 的私钥通过 SSH 进入我的 EC2 实例时,出现错误:

PuTTY Fatal Error
Disconnected: No supported authentication methods available (server sent: pulickey,gssapi-keyex,gssapi-with-mic)

PuTTY控制台显示:

Using username "bob".
Server refused our key

我还尝试从命令行使用 bob 的私钥通过 ssh 进入我的 EC2 实例,但没有成功。它不接受密钥的密码。我还尝试使用没有密码的密钥对,但仍然提示我输入密码,无法进入服务器。

这是我的非常详细的输出

$ ssh -vvv -i bobs_key.ppk  [email protected]
OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007
debug2: ssh_connect: needpriv 0
debug1: Connecting to ec2-12-345-678-910.us-west-2.compute.amazonaws.com [12.345.678.910] port 22.
debug1: Connection established.
debug3: Not a RSA1 key file bobs_key.ppk.
debug2: key_type_from_name: unknown key type 'PuTTY-User-Key-File-2:'
debug3: key_read: missing keytype
debug2: key_type_from_name: unknown key type 'Encryption:'
debug3: key_read: missing keytype
debug2: key_type_from_name: unknown key type 'Comment:'
debug3: key_read: missing keytype
debug2: key_type_from_name: unknown key type 'Public-Lines:'
debug3: key_read: missing keytype
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug2: key_type_from_name: unknown key type 'Private-Lines:'
debug3: key_read: missing keytype
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug2: key_type_from_name: unknown key type 'Private-MAC:'
debug3: key_read: missing keytype
debug1: identity file bobs_key.ppk type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.4
debug1: match: OpenSSH_6.4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.6
debug2: fd 3 setting O_NONBLOCK
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,[email protected],aes128-ctr,aes192-ctr,a
es256-ctr
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,[email protected],aes128-ctr,aes192-ctr,a
es256-ctr
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,[email protected],zlib
debug2: kex_parse_kexinit: none,[email protected],zlib
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: kex_parse_kexinit: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-grou
p14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ecdsa-sha2-nistp256
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,[email protected],[email protected],aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,ae
s192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,[email protected],[email protected],aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,ae
s192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-512-etm@
openssh.com,[email protected],[email protected],[email protected],hmac-md5,hmac-sha1,[email protected],[email protected],hmac-sha2-256,h
mac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-512-etm@
openssh.com,[email protected],[email protected],[email protected],hmac-md5,hmac-sha1,[email protected],[email protected],hmac-sha2-256,h
mac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,[email protected]
debug2: kex_parse_kexinit: none,[email protected]
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: mac_init: found hmac-md5
debug1: kex: server->client aes128-cbc hmac-md5 none
debug2: mac_init: found hmac-md5
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug2: dh_gen_key: priv key bits set: 140/256
debug2: bits set: 512/1024
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug3: check_host_in_hostfile: filename /c/Users/bob/.ssh/known_hosts
debug3: check_host_in_hostfile: match line 1
debug3: check_host_in_hostfile: filename /c/Users/bob/.ssh/known_hosts
debug3: check_host_in_hostfile: match line 1
debug1: Host 'ec2-12-345-678-910.us-west-2.compute.amazonaws.com' is known and matches the RSA host key.
debug1: Found key in /c/Users/bob/.ssh/known_hosts:1
debug2: bits set: 501/1024
debug1: ssh_rsa_verify: signature correct
debug2: kex_derive_keys
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: bobs_key.ppk (0x0)
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug3: start over, passed a different list publickey,gssapi-keyex,gssapi-with-mic
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Trying private key: bobs_key.ppk
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Enter passphrase for key 'bobs_key.ppk':
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
debug2: bad passphrase given, try again...
Enter passphrase for key 'bobs_key.ppk':
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
debug2: bad passphrase given, try again...
Enter passphrase for key 'bobs_key.ppk':
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
debug2: bad passphrase given, try again...
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

Putty 日志:

2015-02-08 08:35:15 Looking up host "ec2-12-345-678-910.us-west-2.compute.amazonaws.com"
2015-02-08 08:35:15 Connecting to 12.345.678.910 port 22
2015-02-08 08:35:15 Server version: SSH-2.0-OpenSSH_6.4
2015-02-08 08:35:15 Using SSH protocol version 2
2015-02-08 08:35:15 We claim version: SSH-2.0-PuTTY_Release_0.62
2015-02-08 08:35:15 Doing Diffie-Hellman group exchange
2015-02-08 08:35:15 Doing Diffie-Hellman key exchange with hash SHA-256
2015-02-08 08:35:16 Host key fingerprint is:
2015-02-08 08:35:16 ssh-rsa 2048 b6:76:a5:c6:06:dc:3c:d6:c2:65:fe:f5:55:27:b9:ef
2015-02-08 08:35:16 Initialised AES-256 SDCTR client->server encryption
2015-02-08 08:35:16 Initialised HMAC-SHA1 client->server MAC algorithm
2015-02-08 08:35:16 Initialised AES-256 SDCTR server->client encryption
2015-02-08 08:35:16 Initialised HMAC-SHA1 server->client MAC algorithm
2015-02-08 08:35:16 Reading private key file "C:\bob.ppk"
2015-02-08 08:35:16 Pageant is running. Requesting keys.
2015-02-08 08:35:16 Pageant has 1 SSH-2 keys
2015-02-08 08:35:16 Configured key file not in Pageant
2015-02-08 08:35:16 Offered public key
2015-02-08 08:35:16 Server refused our key
2015-02-08 08:35:16 Using SSPI from SECUR32.DLL
2015-02-08 08:35:16 Attempting GSSAPI authentication
2015-02-08 08:35:16 GSSAPI authentication request refused
2015-02-08 08:35:16 Disconnected: No supported authentication methods available (server sent: publickey,gssapi-keyex,gssapi-with-mic)

服务器 sshd 日志:

Feb  8 08:35:16 ip-101-10-101-1 sshd[3182]: error: Received disconnect from 22.33.44.555: 14: No supported authentication methods available [preauth]

以下是 PuTTY 生成的公钥示例

---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20150209"
AAAAB3NzaC1yc2EAAAABJQAAAQEAs2f7SVX8KOu0RvzQh0r6uwWS/TB5uXO7+QEs
CxF+0CF3vwLeOn+cNw1HQHy7EDHhGw4eYvL/ErbIyv4I8HgGe/IFe4qPUOMy3vtz
ZNjqemBsy32Lgsx/flQBYLW+XzYC4LeZv3TSZjfYyIk8ilRBOzdkP4CdYeK0rPRb
PPtlLQY5l95h1UnmuORNgA93yLqvKI/dSnkvKyTrJoCGbRXr5pept5Y9LSEHYlEa
drBpogsF9LYj/HDeaoIjBhGQu+CZRDzZSxgtv3+nePEkoeV1xC8tyJ83JiHcrX3P
bjouJHLLTWa2tl40XM913dkUSsbha2Fbd0wBchry9y2BlpUA9Q==
---- END SSH2 PUBLIC KEY ----

以下是与之配套的私钥示例

PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: rsa-key-20150209
Public-Lines: 6
AAAAB3NzaC1yc2EAAAABJQAAAQEAs2f7SVX8KOu0RvzQh0r6uwWS/TB5uXO7+QEs
CxF+0CF3vwLeOn+cNw1HQHy7EDHhGw4eYvL/ErbIyv4I8HgGe/IFe4qPUOMy3vtz
ZNjqemBsy32Lgsx/flQBYLW+XzYC4LeZv3TSZjfYyIk8ilRBOzdkP4CdYeK0rPRb
PPtlLQY5l95h1UnmuORNgA93yLqvKI/dSnkvKyTrJoCGbRXr5pept5Y9LSEHYlEa
drBpogsF9LYj/HDeaoIjBhGQu+CZRDzZSxgtv3+nePEkoeV1xC8tyJ83JiHcrX3P
bjouJHLLTWa2tl40XM913dkUSsbha2Fbd0wBchry9y2BlpUA9Q==
Private-Lines: 14
UP7YT8e9aWk159T5xO1P0MR5AbAuhTh9hzjFw98igBaYhco9ATUhOyXVumBuHnuB
1+2aiucV22XUuJIWDjRDdX3uSmUMsDXPPS4li6GOvdGMBo66bcLAqNbwNuzS7ogX
fFQ23TzsPnDbrOqwNcNOl9Suf7MA6u6s7sACFsflxAuCSRe9iiyxcRLXafvAl/AH
BD3Ltd4i49SICrjmW3vEAndX6r0QqMQ5GjzorQhu1I2b4dYdQqVAOThYSfXTR9p4
nHTvoeO+Qi7qTmzZMRUAEkh7tC+4J8idHN5e3k6hpzV8z0E7arjbtPH8gDbG1FL2
ZevCJX2s4b4bGL9a9vzxzLLg/TikZBr0QAVHtTU5sPgViCSgkeAKTeIsQcLKT/vt
Kl8UOpaalyoeRTSo9HNUEwuz99aK7WjzvKRGHIUpioqBsA7C7Yj/hLq/ofKOZQgJ
lp6bsSA0GkY5zncPHUSIrCu5xcdO9cNnKaXwf5qjJfW9udqzHRIf19Jgzo6NlymL
SxGBa+bXmn+15BIg1R23mE/VTBZoKRbmCgVRFlz4vLGIyTwbtnNIaFIZIxvhUVgs
PWIG2E4orNKZ+n1g92RI0MGoyk+WMOajAoCbMWsIEgFBTqWDAxliEgtyu/npuIFD
RbufWC/kIoSm/GVbIUoEYCDbR6KSi3OzdHCQKYufSUk67vP2BGfS9jxRe98/YhvQ
WXgxtHFWZ2p6KqDIurdWUtnoO2IYrBBeZwXSOPHuJkQaIOC6XBLFflKM4XXAdXl1
vsuAGXBnZ2BJfbxifWNvSlf60SXOeggvxYXx3euVkTpBtb6yy6AeIN7VJDjqMpn5
+Jz/PazNXUbHLZqBvBxYF9MBgACqGmV3b8dtiL3bCvFcuDv/JYTdSFtCgysgbs0s
Private-MAC: 44a5eabcdebd7d33a1c99f72016563d26b435832

答案1

如果您已将公钥粘贴到authorized_keys问题(---- BEGIN SSH2 PUBLIC KEY ----)中显示的格式的文件中,那么它将不起作用。

您必须使用 PuTTYgen 中显示的单行格式“用于粘贴到 OpenSSH authorized_keys 文件中的公钥”

它会是这样的:

rsa-key AAAAB3NzaC1yc2EAAAABJQAAAQEAs2f7SVX8KOu0RvzQh0r6uwWS/TB5uXO7+QEsCxF+0CF3vwLeOn+cNw1HQHy7EDHhGw4eYvL/ErbIyv4I8HgGe/IFe4qPUOMy3vtzZNjqemBsy32Lgsx/flQBYLW+XzYC4LeZv3TSZjfYyIk8ilRBOzdkP4CdYeK0rPRbPPtlLQY5l95h1UnmuORNgA93yLqvKI/dSnkvKyTrJoCGbRXr5pept5Y9LSEHYlEadrBpogsF9LYj/HDeaoIjBhGQu+CZRDzZSxgtv3+nePEkoeV1xC8tyJ83JiHcrX3PbjouJHLLTWa2tl40XM913dkUSsbha2Fbd0wBchry9y2BlpUA9Q== rsa-key-20150209

答案2

如果您在 putty 中生成私钥/公钥并直接将其复制到 Linux 发行版,则密钥将采用Putty格式。您需要先将其更改为OpenSSH格式,然后才能使用它连接到 github 或其他服务器。否则,它会给您一个不正确的错误并告诉您需要一个密码。

即使私钥没有.ppk文件扩展名,它仍然可以是 putty 格式。您可以在记事本或其他文本编辑器中打开该文件以检查格式是否正确。

公钥的 OpenSSH 格式

ssh-rsa AAAAB3NzaC1yc2EAAAA…

私钥的 OpenSSH 格式

-----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEA4Jgy…

私钥的较新 OpenSSH 格式

-----BEGIN OPENSSH PRIVATE KEY----- b3BlbnNzaC1rZXktdjEAA…

如何使用 putty 生成 OpenSSH 格式的密钥:

如果你在Linux中使用ssh来登录其他服务器或者登录github,你必须使用openssh格式的id。步骤:

  1. 在开始菜单中输入Puttygen打开Puttygen。然后按Load或按Alt-l加载私钥:

将公钥加载到 putty gen

  1. 将putty私钥转换为Open SSH格式Conversions>Export OpenSSH key

转换为 OpenSSH 格式

  1. 将密钥另存为id_rsa

  2. 如果你使用 Putty 登录 Linux,然后通过 ssh 进入另一台服务器,或者通过 ssh 进入 github,请将 id_rsa 复制到 ~/.ssh/id_rsa

  3. 如果仍然不起作用,请按照以下步骤操作:https://superuser.com/a/1285777/658319

答案3

我尝试了从 Putty 生成的多个密钥,但没有一个适用于我的 EC2 实例。我获得有效密钥对的方法是在 EC2 服务器上生成它们。

ssh-keygen -t rsa -b 2048

然后我将私钥移到我的电脑上,并使用 PuTTY 密钥生成器将密钥转换为 PuTTY 的格式。我现在可以使用密钥连接到我的 EC2 实例。

相关内容