Connect to Host with VSCode using the private key
Asked Answered
L

2

35

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.

Leffler answered 14/6, 2021 at 11:3 Comment(0)
C
58

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

Crystacrystal answered 19/10, 2021 at 7:4 Comment(8)
Specifically, I had a relative path to my identity file, not an absolute path like is specified here. This solved my problem.Noggin
where does this id_rsa file come from ?Trichomoniasis
@D.L generate ssh keys for your system. ssh-keygen is used to generate the keys for the systemCrystacrystal
@JaymitGupta. But how is this related to the original private_key.pem file ?Trichomoniasis
@D.L If you have pre-existing (assuming you have already generated it) , you can use that also.Crystacrystal
@it turns out that the path to a google drive g:\\ ... *.pem does not work, but a local path c:\\ ... *.pem does workTrichomoniasis
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.Rutland
On my Windows 10 system, I use IdentityFile ~/.ssh/id_rsa to specify my key. (VScode recognizes the Linux-style path with no problems...Equipment
L
1

Some other things to check on Winows:

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.

Licketysplit answered 8/5, 2023 at 8:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.