123

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?

1
  • 1
    you cannot save the password, instead you can generate a key and copy its contents in remote linux machine - which ssh uses to save & remember your device (that's what all the answers below mention) Commented Jul 24, 2024 at 4:42

7 Answers 7

160

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/keys/<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.

Sign up to request clarification or add additional context in comments.

9 Comments

great answer! but no need to copy <host>_rsa.pub file, only to copy its contents and paste it on authorized_keys on the server.
This does not answer the original question.
ssh-copy-id did not add PreferredAuthentications option. added it with the help of this answer
"/Users/<localuser>/.ssh/<myhost>_rsa" should be "/Users/<localuser>/.ssh/keys/<myhost>_rsa"
I followed the steps by 1) copying the public key to the server and changing the file name as authorized_keys, 2) adding the two line to the config file of the server, but the ssh username@host still ask for password, somehow.
|
49

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.

20 Comments

This is still not an answer to the question 'save ssh password'.
It caches the passphrase of a key, not a password, which is commonly known as username-password authentication and should not be confused with passphrase.
I don't think a name that there is not any other alias could refer to could be taken as a 'colloquialism' to another concept. "ssh password", in common sense, is literally a password that is used while logging in to an ssh server. Although it is not recommended, there is no way to say that it could not have a name that clearly refers to it without confusion. If "ssh password" means private key passphrase, what simply means the ssh password?
I think the correct answer is "no, but you can use ssh keys and that is better"
@ShawnCicoria Yes, I did mention PasswordAuthentication in my previous comment. And caching that password does not seem supported in VSCode for SSH session.
|
49

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

6 Comments

Maybe the best way to sidestep setting a password in vscode ssh
The easiest answer by far. In my case I already had a generated key, so I didn't overwrite and just pasted the existing one. Worked like a charm!
This worked and is a million times more straightforward than the typical answer you get given to this question. Thank you.
the best way. First time I didn't replaced pub-key text with the generated public key in the echo "pub-key" >> ~/.ssh/authorized_keys command
This works for me. Thanks.
|
5

If you don't want to enter your password each time you are connecting to your server, according to vs code documentation you have to:

"Set up SSH key-based authentication to the server so you do not need to enter your password multiple times."

To do this on a Linux system (I did it on Ubuntu 22.04) :

1- Generate a key pair if you haven't already: ssh-keygen -t ed25519 -b 4096. This would generate two files under the names: id_ed25519 and id_ed25519.pub

2- Assuming that the files are saved on the ~/.ssh/ directory, use this command: ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server_ip. You will be prompted to enter your server password. After doing so, you no longer need to enter a password to connect to your server both in the terminal and vs code.

2 Comments

Tried this and it still asks for my password in VSCode
Maybe nitpicking but ED25519 doesn't accept a bit length. -b is ignored. The default bit length is 256.
2

You can use sshpass to save the password:

sshpass -p 'PASSWORD' ssh HOST

where HOST is defined in ~/.ssh/config.

sshpass is not integrated into vscode, but you can write a shell script to wrap around the ssh binary:

#!/bin/sh

for arg in "$@"; do
    if [ "$arg" = "HOST" ]; then
        target_host="HOST"
        break
    fi
done

if [ "$target_host" = "HOST" ]; then
    exec sshpass -p 'PASSWORD' /usr/bin/ssh "$@"
else
    exec /usr/bin/ssh "$@"
fi

Name this script to "ssh" and add its directory to $PATH in .bashrc (or .zshrc on Mac), vscode will use it to establish the ssh connection, without prompting the user to enter the password.

Alternatively, there's also a remote.ssh.path setting to specify the ssh executable.

There are definitely security issues with this method, but it works. ssh keys are safer, but sometimes the server does not support them.

Comments

1

After locally configuring /.ssh/config,

On the local terminal, run:

  1. ssh-keygen
  2. Keep pressing enter is fine.
  3. type $env:USERPROFILE\\.ssh\id_rsa.pub | ssh *ip or username@hostname* "cat >> .ssh/authorized_keys"
  4. Enter your remote password in the prompt.
  5. Then simply run ssh *ip or username@hostname*

You're in!

1 Comment

%USERPROFILE% for cmd
0

Windows (local)

SSH Vault for VSCode (no affiliation/can't vouch)

Mac (local)

perhaps combination of:

SSH Config Editor and vscode settings:

remote.SSH.externalSSH_ASKPASS
remote.SSH.path

(will update if successful)

Mac/Linux (remote)

Tailscale SSH

This automates SSH key handling, not password entry

not supported on windows yet

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.