LoginSignup
12
10

More than 5 years have passed since last update.

Paramikoのssh認証でハマった話

Last updated at Posted at 2019-03-06

新PCで既存プロジェクト資源のparamiko ssh認証を実行した際に以下のエラーが発生した。

raise SSHException("not a valid " + tag + " private key file")
paramiko.ssh_exception.SSHException: not a valid RSA private key file

なんの話や

調べてみるとparamikoのissueに同じ報告が上がってた
https://github.com/paramiko/paramiko/issues/1226

原因はparamikoの以下ソース
https://github.com/paramiko/paramiko/blob/master/paramiko/pkey.py#L285
https://github.com/paramiko/paramiko/blob/master/paramiko/pkey.py#L265

秘密鍵のヘッダーがRSAまたはDSAじゃないとダメみたい
で、自分の秘密鍵を見てみると

$ cat id_rsa
-----BEGIN OPENSSH PRIVATE KEY-----

あれなんか違う

また調べてみるとOpenSSHで仕様変更があったみたい
https://www.openssh.com/txt/release-7.8

  • ssh-keygen(1): write OpenSSH format private keys by default instead of using OpenSSL's PEM format. The OpenSSH format, supported in OpenSSH releases since 2014 and described in the PROTOCOL.key file in the source distribution, offers substantially better protection against offline password guessing and supports key comments in private keys. If necessary, it is possible to write old PEM-style keys by adding "-m PEM" to ssh-keygen's arguments when generating or updating a key.

※自端末のOpenSSHのバージョンは以下の通り

$ ssh -V
OpenSSH_7.9

リリースノートに従って-m PEMオプションをつけて鍵を作り直してみる

$ ssh-keygen -t rsa -m PEM
$ cat id_rsa
-----BEGIN RSA PRIVATE KEY-----

paramikoも無事動きました。
小一時間ハマったのは内緒。

12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10