带有 SSH 远程错误的 Git 推送:加载密钥"/path/to/file_id_rsa":格式无效



我试图将git push与SSH远程从GitHub CI操作一起使用,但得到一个错误:Load key "/tmp/341b5794-f0a2-4534-90dd-f791510ec77a_id_rsa": invalid format

密钥在存储库中是秘密的。我用ssh-keygen -l -v -f key_id_rsa获取它的信息,它的输出等于我本地机器上的输出。从我的机器上,我可以用这个键推动

GIT_SSH_COMMAND='ssh -v -o StrictHostKeyChecking=accept-new -i /tmp/341b5794-f0a2-4534-90dd-f791510ec77a_id_rsa'

我们是/tmp/341b5794-f0a2-4534-90dd-f791510ec77a_id_rsa.pub文件

git push输出内部的完整ssh -v

+ git push dest-push-remote gh-pages
OpenSSH_8.2p1 Ubuntu-4ubuntu0.2, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to github.com [140.82.112.4] port 22.
debug1: Connection established.
debug1: SELinux support disabled
debug1: identity file /tmp/341b5794-f0a2-4534-90dd-f791510ec77a_id_rsa type 0
debug1: identity file /tmp/341b5794-f0a2-4534-90dd-f791510ec77a_id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.2
debug1: Remote protocol version 2.0, remote software version babeld-10d0a39d
debug1: no match: babeld-10d0a39d
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: rsa-sha2-512
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: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /etc/ssh/ssh_known_hosts:1
Warning: Permanently added the RSA host key for IP address '140.82.112.4' to the list of known hosts.
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /tmp/341b5794-f0a2-4534-90dd-f791510ec77a_id_rsa RSA SHA256:1xl0uMTaSpIRCnMFIGCf9zC4/CTHXPCroAk1cJIK6qY explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa,ssh-dss>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /tmp/341b5794-f0a2-4534-90dd-f791510ec77a_id_rsa RSA SHA256:1xl0uMTaSpIRCnMFIGCf9zC4/CTHXPCroAk1cJIK6qY explicit
debug1: Server accepts key: /tmp/341b5794-f0a2-4534-90dd-f791510ec77a_id_rsa RSA SHA256:1xl0uMTaSpIRCnMFIGCf9zC4/CTHXPCroAk1cJIK6qY explicit
Load key "/tmp/341b5794-f0a2-4534-90dd-f791510ec77a_id_rsa": invalid format
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

UPD经过评论中的讨论,我尝试在Linux机器上生成新的密钥对,它成功了。Linux机器和MacOS机器上生成的不同格式密钥的问题:

MacOS生成的密钥以BEGIN OPENSSH PRIVATE KEY开头。Linux生成的密钥以BEGIN RSA PRIVATE KEY开头。我正在调查wat的差异,但如果有专家建议我一些信息,会很有帮助

如果私钥格式不同,这意味着,正如我在这里提到的:

  • 一个平台使用的是7.8之前的openssh,采用的是每行64个字符的旧PEM格式
  • 一种是使用更新的OpenSSH格式,每行70个字符

您可以使用强制最近的openSSH生成旧格式

ssh-keygen -m PEM -t rsa -P "" -f afile

最新更新