ばーろぐわにる

SIerからWEB系?インフラエンジニアにジョブチェンジした見習いの備忘録。投稿内容は私個人の意見であり、所属企業・部門見解を代表するものではありません。

SSH接続元のid_rsa.pubのせいで公開鍵認証が失敗してた

概要

SSHの公開鍵認証を使ってパスワード無しでログインしようとしたとき、公開鍵も暗号鍵も正しく配置してかつ権限も間違ってないのにログインできなくてハマったのでメモ。知ってる人は当たり前じゃん?って内容なのかもしれませんが色々調べたのでメモ。

事象

結論を言うと接続元から接続先にSSHする際、接続元に暗号鍵 id_rsaid_rsa.pub が配置されていて、id_rsa.pub のキーが id_rsa と異なっていると接続に失敗する。

ssh -i .ssh/id_rsa hogehoge@hogehoge.com -vvv で確認

id_rsaのみ

debug1: Next authentication method: publickey
debug1: Trying private key: .ssh/id_rsa
debug3: sign_and_send_pubkey: RSA XXXXXXXX
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 52
debug1: Authentication succeeded (publickey).

id_rsaとid_rsa.pub(同じキー)

debug1: Next authentication method: publickey
debug1: Offering public key: RSA SXXXXXXXX .ssh/id_rsa
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 60
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug2: input_userauth_pk_ok: fp XXXXXXXX
debug3: sign_and_send_pubkey: RSA XXXXXXXX
debug3: send packet: type 50
debug3: receive packet: type 52
debug1: Authentication succeeded (publickey).

## id_rsaとid_rsa.pub(異なるキー)

debug1: Next authentication method: publickey
debug1: Offering public key: RSA XXXXXXXX .ssh/id_rsa
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.

原因

なんで接続元の公開鍵がsshで使われてるんだと思って色々調べてみたけど、正式なドキュメントは見当たらず。一番それっぽい回答が 公開鍵に対して正しい暗号鍵が使われているか確認している ってのだけど、そうだとしたら秘密鍵が違ってる旨のメッセージがほしい。その他にも公開鍵の計算で利用しているってのもあった。もしも正しい理由をご存知の方いましたらご教示ください。

serverfault.com

blog.yuryu.jp

感想

変なところでハマったけど暗号鍵ファイルの管理をちゃんとしてないと発生しそうな事象なので勉強になった。