从 txt 恢复 SSH 密钥

从 txt 恢复 SSH 密钥

我已将我的私钥和公钥内容保存到文本文件中,最近我不得不重新安装 Ubuntu,这意味着我丢失了主文件夹中的大部分内容。现在,我想从 txt 文件恢复我的 SSH 密钥。

以下是我目前使用的步骤:

mkdir ~/.ssh
nano ~/.ssh/id_rsa.pub # copy/paste public key
nano ~/.ssh/id_rsa # copy/paste private key

但是当我尝试连接到具有我的密钥的远程主机时,我得到:

Agent admitted failure to sign using the key.

为了寻找这个,我尝试了:

ssh-add

它要求我输入密码,我有密码并且输入正确,但我收到错误:

Bad passphrase, try again for /home/brandon/.ssh/id_rsa

我该如何修复这个问题并从复制/粘贴的文本中恢复我的 SSH 密钥?


信息请求

OpenSSH_6.2p2 Ubuntu-6ubuntu0.1, OpenSSL 1.0.1e 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to SNIPPED [SNIPPED] port 22.
debug1: Connection established.
debug1: identity file /home/brandon/.ssh/id_rsa type 1
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048
debug1: identity file /home/brandon/.ssh/id_rsa-cert type -1
debug1: identity file /home/brandon/.ssh/id_dsa type -1
debug1: identity file /home/brandon/.ssh/id_dsa-cert type -1
debug1: identity file /home/brandon/.ssh/id_ecdsa type -1
debug1: identity file /home/brandon/.ssh/id_ecdsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2p2 Ubuntu-6ubuntu0.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1.1
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1.1 pat OpenSSH_5*
debug2: fd 3 setting O_NONBLOCK
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
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-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],[email protected],[email protected],ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,[email protected],[email protected],aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-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,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-md5,hmac-sha1,[email protected],[email protected],hmac-sha2-256,hmac-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],[email protected],[email protected],[email protected],[email protected],hmac-md5,hmac-sha1,[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,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-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,[email protected],hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,[email protected],hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,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_setup: found hmac-md5
debug1: kex: server->client aes128-ctr hmac-md5 none
debug2: mac_setup: found hmac-md5
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA SNIPPED
debug1: Host 'SNIPPED' is known and matches the ECDSA host key.
debug1: Found key in /home/brandon/.ssh/known_hosts:1
debug1: ssh_ecdsa_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: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /home/brandon/.ssh/id_rsa (0x7f80ce9c7630),
debug2: key: /home/brandon/.ssh/id_dsa ((nil)),
debug2: key: /home/brandon/.ssh/id_ecdsa ((nil)),
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/brandon/.ssh/id_rsa
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug2: input_userauth_pk_ok: fp SNIPPED
Agent admitted failure to sign using the key.
debug1: Trying private key: /home/brandon/.ssh/id_dsa
debug1: Trying private key: /home/brandon/.ssh/id_ecdsa
debug2: we did not send a packet, disable method
debug1: Next authentication method: password

答案1

OpenSSH 以 Base64 文本格式存储其密钥。您无法提供正确的密码这一事实告诉我们,至少有一半可能已损坏。请发布您的 ssh 尝试的输出,并添加“ssh -vv”标志。调试信息可能会揭示有关该问题的更多详细信息。

请注意,.pub(公共部分)是一行很长的文字。如果您在其中插入换行符,则它不再是有效的关键部分。

此外,当您重新创建这些文件时,请确保它们具有适当的权限:

chmod 700 ~/.ssh; chmod 600 ~/.ssh/*

如果你不确定 .pub 密钥部分的状态/健全性,你可以从私有部分重新生成它:

ssh-keygen -y ~/.ssh/id_rsa | tee ~/.ssh/id_rsa.pub

如果这样做,请注意您可能必须手动将注释附加到公共部分,因为该部分在重新生成公钥时会被销毁。

nano ~/.ssh/id_rsa.pub

转到行尾,然后添加一个空格,后跟 username@hostname。无需按 Enter 键,保存文件。确保 .pub 文件没有添加额外的回车符/换行符,否则它会再次损坏。

假设您已完成所有这些操作,但 id_rsa.pub 仍然与远程系统的 authorized_keys 文件中的内容不匹配,那么您几乎陷入困境。您要么必须找到 id_rsa 私钥的另一个备份,要么是时候重新运行 ssh-keygen 并使用新的密钥对重新开始。在这种情况下,往好的方面想 - 定期更改密钥是一件好事。;)

相关内容