Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save ssh password to vscode?

I am using vscode to connect to a remote host. I use Remote-SSH (ms-vscode-remote.remote-ssh) extension to do so. Every time I want to connect to the remote host, I need to enter the password.

Is there a way to save the ssh password to vscode?

like image 763
mayank1513 Avatar asked Feb 09 '21 05:02

mayank1513


People also ask

How add SSH key to VS Code?

Add SSH key to your VM#Select Use existing public key in the dropdown for SSH public key source so that you can use the public key you just generated. Take the public key and paste it into your VM setup, by copying the entire contents of the id_ed25519. pub in the SSH public key.

Can VS Code connect to SSH?

The Visual Studio Code Remote - SSH extension allows you to open a remote folder on any remote machine, virtual machine, or container with a running SSH server and take full advantage of VS Code's feature set. Once connected to a server, you can interact with files and folders anywhere on the remote filesystem.


3 Answers

To setup password-less authentication for ssh on Visual Studio Code, perform the following steps.

These examples assume the following (replace with your actual details)

Host: myhost
Local User: localuser 
Remote User: remoteuser
Remote User Home Dir: remoteuserhome
SSH Port:  22

I'm using a Mac so Windows will be a bit different but the basics are the same

Tell VS Code and your machine in general how you will be connecting to myhost

Edit:

/Users/<localuser>/.ssh/config

Add:

Host <myhost>
  HostName <myhost>
  User <remoteuser>
  Port 22
  PreferredAuthentications publickey
  IdentityFile "/Users/<localuser>/.ssh/<myhost>_rsa"

Next generate a public and a private key with something like OpenSSL

ssh-keygen -q -b 2048 -P "" -f /Users/<localuser>/.ssh/keys/<myhost>_rsa -t rsa

This should make two files:

<myhost>_rsa        (private key)
<myhost>_rsa.pub    (public key)

The private key (<myhost>_rsa) can stay in the local .ssh folder

The public key (<myhost>_rsa.pub) needs to be copied to the server (<myhost>)

I did it with FTP but you can do it however you wish but it needs to end up in a similar directory on the server.

ON THE SERVER

There is a file on the server which has a list of public keys inside it.

 <remoteuserhome>/.ssh/authorized_keys

If it exists already, you need to add the contents of <myhost>_rsa.pub to the end of the file.

If it does not exist you can use the <myhost>_rsa.pub and rename it to authorized_keys with permissions of 600.

If everything goes according to plan you should now be able to go into terminal and type

ssh <remoteuser>@<myhost>

and you should be in without a password. The same will now apply in Visual Studio Code.

like image 108
blissweb Avatar answered Oct 22 '22 20:10

blissweb


Let's answer the OP's question first:

How to 'save ssh password'?

Since there is no such thing as "ssh password", the answer to "how to save the remote user password" is:

This is not supported by VSCode.

VSCode proposes to setup an SSH Agent in order to cache the passphrase (in case you are using an encrypted key)

But if the public key was not properly registered to the remote account ~/.ssh/authorized_key, SSH daemon will default to the remote user credentials (username/password).

It is called PasswordAuthentication, often the remote user password.

And caching that password is not supported for SSH sessions.

It is only supported by a Git credential helper, when using HTTPS URLs.
(it defers to the OS underlying credential manager)
But I don't know of a remote user password cache when SSH is used.

As Chagai Friedlander comments, the answer to the original question is therefore:

No, but you can use SSH keys and that is better.


Speaking of SSH keys:

"ssh password": Assuming you are referring to a ssh passphrase, meaning you have created an encrypted private key, then "saving the ssh password" would mean caching that passphrase in order to avoid entering it every time you want to access the remote host.

Check first if you can setup the ssh-agent, in order to cache the passphrase protecting your private key.
See "VSCode: Setting up the SSH Agent"

This assumes you are using an SSH key, as described in "VSCode: Connect to a remote host", and you are not using directly the remote user password.

Using an SSH key means its public key would have been registered to the remote account ~/.ssh/authorized_keys file.

This section is the workaround the OP ended up accepting: registering the public key on the remote user account, and caching the local private key passphrase worked.

like image 28
VonC Avatar answered Oct 22 '22 20:10

VonC


For those trying to connect through Vscode Remote SSH Extension steps provided at https://code.visualstudio.com/docs/remote/troubleshooting#_ssh-tips)

For Windows(Host) --> Linux(Remote)

  1. Create an SSH .pub key in your windows ssh-keygen -t rsa -b 4096
  2. Copy the contents of the .pub key (default path C:\Users\username/.ssh/id_rsa.pub)
  3. SSH into Remote machine and append the contents of the pub key in authorized keys echo "pub-key" >> ~/.ssh/authorized_keys
like image 4
Abid Zaidi Avatar answered Oct 22 '22 20:10

Abid Zaidi