(O+P)ut

アウトプット



(O+P)ut

エンジニアのアウトプット

【OpenSSH】Load key ".ssh/id_rsa": invalid formatと出るエラーの解決策

スポンサーリンク

事象

公開鍵暗号方式にてSSHDを起動しているサーバに接続するも

$ ssh -i .ssh/id_rsa xx@xx -p 22
Load key ".ssh/id_rsa": invalid format
xx: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

エラーとなる。尚、サーバ側のauthorized_keysには対応する公開鍵(id_rsa.pub)を追記済。

環境情報
$ ssh -V
OpenSSH_8.8p1, OpenSSL 1.1.1l  24 Aug 2021

原因/解決策

秘密鍵のフォーマットが想定と異なっていた。

$ nkf --guess .ssh/id_rsa
ASCII (CRLF)

ファイル作成直後であれば以下となっている。

$ nkf --guess .ssh/id_rsa
ASCII (LF)

以下、補足です。

補足

sshd側のログを以下のように確認すると

失敗時は以下でログが止まっていました。

sshd[49717]: debug1: Forked child 50518.
sshd[50518]: debug1: Set /proc/self/oom_score_adj to 0
sshd[50518]: debug1: rexec start in 5 out 5 newsock 5 pipe 7 sock
sshd[50518]: debug1: inetd sockets after dupping: 3, 3
sshd[50518]: Connection from .. port 50572 on ... port 22
sshd[50518]: debug1: Client protocol version 2.0; client software version OpenSSH_8.8
sshd[50518]: debug1: match: OpenSSH_8.8 pat OpenSSH* compat 0x04000000
sshd[50518]: debug1: Local version string SSH-2.0-OpenSSH_7.4
sshd[50518]: debug1: Enabling compatibility mode for protocol 2.0
sshd[50518]: debug1: SELinux support enabled [preauth]
sshd[50518]: debug1: permanently_set_uid: 74/74 [preauth]
sshd[50518]: debug1: list_hostkey_types: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519 [preauth]
sshd[50518]: debug1: SSH2_MSG_KEXINIT sent [preauth]
sshd[50518]: debug1: SSH2_MSG_KEXINIT received [preauth]
sshd[50518]: debug1: kex: algorithm: curve25519-sha256 [preauth]
sshd[50518]: debug1: kex: host key algorithm: ssh-ed25519 [preauth]
sshd[50518]: debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none [preauth]
sshd[50518]: debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none [preauth]
sshd[50518]: debug1: kex: curve25519-sha256 need=64 dh_need=64 [preauth]
sshd[50518]: debug1: kex: curve25519-sha256 need=64 dh_need=64 [preauth]
sshd[50518]: debug1: expecting SSH2_MSG_KEX_ECDH_INIT [preauth]
sshd[50518]: debug1: rekey after 134217728 blocks [preauth]
sshd[50518]: debug1: SSH2_MSG_NEWKEYS sent [preauth]
sshd[50518]: debug1: expecting SSH2_MSG_NEWKEYS [preauth]
sshd[50518]: debug1: SSH2_MSG_NEWKEYS received [preauth]
sshd[50518]: debug1: rekey after 134217728 blocks [preauth]
sshd[50518]: debug1: KEX done [preauth]
sshd[50518]: debug1: userauth-request for user root service ssh-connection method none [preauth]
sshd[50518]: debug1: attempt 0 failures 0 [preauth]
sshd[50518]: debug1: PAM: initializing for "root"
sshd[50518]: debug1: PAM: setting PAM_RHOST to "..."
sshd[50518]: debug1: PAM: setting PAM_TTY to "ssh"

怪しい箇所は無かったので改めてクライアント側を見直すと、鍵の形式が誤っていました。

以上、同様のエラー時のご参考になれば幸いです。