20

I want to connect to a remote server, I know the username and hostname, and I also have the private_key.pem file. How can I connect to this virtual machine using VSCode?

I get this error with the solutions on the internet, and that's reasonable because I have not imported this private_key.pem file anywhere, and I do not know how.

Permission denied (publickey)

Thanks for your help.

I use windows, but the remote machine is ubuntu.

K.N
  • 871
  • 2
  • 10
  • 30

2 Answers2

35

You can add the private key by following the below steps:

  1. Open VSCode
  2. Press F1 and search for "Remote-SSH: Open Configuration File"
  3. Select the config file to edit and add the new server and private key
    Host *name-of-ssh-host-here*
    User *your-user-name-on-host*
    HostName *host-fqdn-or-ip-goes-here*
    IdentityFile *C:\\path\\to\\my\\id_rsa*
    
    For the IdentityFile specify the path of your private key. For Windows make sure to use double slashes.

Reference

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Jaymit Gupta
  • 567
  • 5
  • 13
  • 1
    Specifically, I had a *relative* path to my identity file, not an absolute path like is specified here. This solved my problem. – James Jul 12 '22 at 23:39
  • where does this `id_rsa` file come from ? – D.L May 30 '23 at 12:37
  • @D.L generate ssh keys for your system. ssh-keygen is used to generate the keys for the system – Jaymit Gupta May 31 '23 at 05:18
  • @JaymitGupta. But how is this related to the original `private_key.pem` file ? – D.L May 31 '23 at 08:58
  • @D.L If you have pre-existing (assuming you have already generated it) , you can use that also. – Jaymit Gupta Jun 01 '23 at 10:30
  • @it turns out that the path to a google drive `g:\\ ... *.pem` does not work, but a local path `c:\\ ... *.pem` does work – D.L Jun 01 '23 at 13:35
  • at first this failed. I had added the key using `ssh-add` before attempting this approach. after removing that key with `ssh-add -d` this worked. – carl verbiest Aug 26 '23 at 14:21
0

Some other things to check:

On File explorer, for both:

  • ~/.ssh directory
  • ~/.ssh/id_rsa

remove permissions for everyone except yourself with:

  • Properties
  • Security
  • Edit

And also ensure that ~/.ssh/config contains:

Host myhostname
  HostName myhostname
  User myusername

Tested on Windows 10, vscode 1.78.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985