LoginSignup
6
4

More than 3 years have passed since last update.

VSCodeのRemote SSHで「Permission denied (publickey).」と出て接続できない

Posted at

経緯

表題の通り。私の場合2パターンの原因があったためエラーログと対応を紹介する。

開発環境

  • Windows 10 Enterprise 64bit
  • AWS EC2 Ubuntu 18.04 LTS - Bionic
  • Visual Studio Code 1.52.1 (user setup)

ケース1 - 秘密鍵ファイルの配置場所が悪い

設定ファイル

config
Host EC2環境
    Hostname [EC2のグローバルIP]
    User ubuntu
    IdentityFile "C:\\work\ssh\KeyPair-WebDBserver.pem"

エラーログ

ログ
[19:57:51.445] Log Level: 2
[19:57:51.454] remote-ssh@0.62.0
[19:57:51.454] win32 x64
~中略~
[19:57:52.874] > OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5

[19:57:52.880] Running script with connection command: ssh -T -D 64047 "EC2環境" bash
[19:57:52.884] Terminal shell path: C:\WINDOWS\System32\cmd.exe
[19:57:53.203] > @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
> ]0;C:\WINDOWS\System32\cmd.exe
[19:57:53.203] Got some output, clearing connection timeout
[19:57:53.225] > @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
> Permissions for 'C:\\work\\ssh\\KeyPair-WebDBserver.pem' are too open.  
> It is required that your private key files are NOT accessible by others.
> This private key will be ignored.
> Load key "C:\\work\\ssh\\KeyPair-WebDBserver.pem": bad permissions      
> ubuntu@[EC2のグローバルIP]: Permission denied (publickey).
> プロセスが、存在しないパイプに書き込もうとしました。
> 
[19:57:53.515] "install" terminal command done
[19:57:53.515] Install terminal quit with output: プロセスが、存在しないパイプに書き込もうとしました。
[19:57:53.516] Received install output: プロセスが、存在しないパイプに書き込もうとしました。
[19:57:53.517] Stopped parsing output early. Remaining text: プロセスが、存在しないパイプに書き込もうとしました。
[19:57:53.517] Failed to parse remote port from server output
~後略~

原因

秘密鍵ファイルをCドライブ直下のアクセス権限がゆるいフォルダに配置していたため。

ログ抜粋
> Permissions for 'C:\\work\\ssh\\KeyPair-WebDBserver.pem' are too open.  
> It is required that your private key files are NOT accessible by others.
> This private key will be ignored.
> Load key "C:\\work\\ssh\\KeyPair-WebDBserver.pem": bad permissions      

↑にある通り、秘密鍵ファイルは他のユーザーがアクセス出来ないフォルダに配置していなければならない。Windowsの場合自分のユーザーフォルダ(C:\\Users\[ユーザー名]\配下)が最適。
いつもの癖でシステムユーザーがアクセス出来ないといけないものと思って適当なフォルダに入れてしまっていた。

対策

VSCodeが初期設定時推奨してくるC:\\Users\[ユーザー名]\.ssh\配下に秘密鍵ファイルを再配置する。

ケース2 - 秘密鍵を作成したリモートユーザーと接続しようとしているリモートユーザーが異なる

設定ファイル

config
Host EC2環境
    Hostname [EC2のグローバルIP]
    User appuser
    IdentityFile "C:\\Users\[ユーザー名]\.ssh\KeyPair-WebDBserver.pem"

エラーログ

ログ
[20:09:59.934] Log Level: 2
[20:09:59.943] remote-ssh@0.62.0
[20:09:59.943] win32 x64
~中略~
[20:10:00.067] > OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5

[20:10:00.073] Running script with connection command: ssh -T -D 64309 "EC2環境" bash
[20:10:00.079] Terminal shell path: C:\WINDOWS\System32\cmd.exe
[20:10:00.386] > appuser@[EC2のグローバルIP]: Permission denied (publickey).
> ]0;C:\WINDOWS\System32\cmd.exe
[20:10:00.386] Got some output, clearing connection timeout
[20:10:00.401] > プロセスが、存在しないパイプに書き込もうとしました。
[20:10:00.681] "install" terminal command done
[20:10:00.681] Install terminal quit with output: プロセスが、存在しないパイプに書き込もうとしました。
[20:10:00.682] Received install output: プロセスが、存在しないパイプに書き込もうとしました。
[20:10:00.683] Stopped parsing output early. Remaining text: プロセスが、存在しないパイプに書き込もうとしました。
[20:10:00.683] Failed to parse remote port from server output
~後略~

原因

EC2のUbuntuで作業用ユーザーappuserを新規作成し、そのユーザーに直接ログインしようとしていたが、秘密鍵としてEC2立ち上げ時に生成されたubuntu用のものをそのまま参照していた。

こちらのケースで具合が悪いのは、エラーログ上だと原因が特定できそうな文面が出てこない点である(気づいていないだけかも)。
ログに具体的な原因らしき文面が無かったらこのケースを疑うといいかもしれない。

対策

新たな秘密鍵公開鍵ペアを作成し、それに基づくよう設定する。
設定方法→Visual Studio CodeでLinux ホストリモート開発 - Qiita

参考

Windowsにおける秘密鍵の管理場所について - Qiita
VSCodeのRemote SSHで「プロセスが、存在しないパイプに書き込もうとしました。」と出て繋がらない - Qiita

6
4
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
6
4