55

I'm using a macbook(MacOS) to connect to a remote Ubuntu server. I copied the public ssh key to the server using ssh-copy-id and checked that the ssh key works on the terminal. When I do ssh [email protected], connection is made without asking for password). However, when I try to connect to the server through Visual Studio Code, VSCode keeps asking for password. Is there a way to fix this?

Thanks in advance!

18 Answers 18

22

It was a problem with the config file.
The VSCode needs the "absolute" path.

In case of MacOS, ssh-copy-id seems to only copy the absolute path relative to the user.
In other words, it omits "/Users/username" before "/.ssh".

Adding "/Users/username" in the IdentityFile attribute in .ssh/config solved the problem.

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

2 Comments

I had the same problem on Windows and the solution was related to this: superuser.com/questions/1296024/…
I had a non-standard key name on Mac OS. Running ssh-add /path/to/private/key on the local machine fixed it.
10

Check if this microsoft/vscode-remote-release issue 2518 applies:

You should be able to get out of this state by deleting the file (on the remote server side, as sudo root) in the log, /home/#####/.vscode-server/bin/78a4c91400152c0f27ba4d363eb56d2835f9903a/vscode-remote-lock.#####.78a4c91400152c0f27ba4d363eb56d2835f9903a (with unlink) or running the command "Kill VS Code Server on Host..."

If it happens again, you might try setting remote.SSH.useFlock.

The exact command to run in the command palette (View->Command Palette) is:

Remote-SSH: Kill VS Code Server on Host...

Also:

In my case, deleting entire ~/.vscode-server directory after connecting to the container through ssh using terminal worked.
(Deleting only ~/.vscode-server/bin did not work.)

The OP sukrama confirms in the comments having solved the issue

It was a problem with ssh key path in config file.

6 Comments

I tried deleting ~/.vscode-server and reinstalling .vscode-server (by opening the remote server on VSCode again). It did not work :( Thanks for the answer though.
@sukrama OK. How about the other suggestions on that issue? remote.SSH.useFlock, ` Kill VS Code Server on Host...`, ...
'Kill VS Code Server on Host' did not work. Also, setting remote.SSH.useFlock made the situation worse (VSCode kept asking for password without connecting to server).
@sukrama Is your key passphrase-protected?
No it's not passphrase-protected. I did solve this problem though. It was a problem with ssh key path in config file.
|
6

I had to use UseKeychain yes in my ~/.ssh/config file.

The config file looks like this:

Host server.tld
  HostName server.tld
  User user
  UseKeychain yes
  IdentityFile  ~/.ssh/key

You have to enter ssh-add -K ~/.ssh/key to add your passphrase to KeyChain first.

1 Comment

The UserKeychain prop seemed to work for me. I had to restart VSC, push a change, enter passphrase. Then, I restarted VSC once again, pushed a change and it seemed to take. I didn't have to enter the passphrase again.
5

Here's a quick and handy fix: You do not have to delete the entire .vscode-server folder each time! The problem seems to be a file named 'vscode-remote-lock...'. It can be located inside a folder in ~/.vscode-server/bin/ . That file gets created at each ssh login through vscode. Run the following script on the remote host. It deletes that file whenever it is created:

while true
do
  if ls /home/<your-username>/.vscode-server/bin/*/vscode-remote-lock.<your-username>.* 1> /dev/null 2>&1; then
    find /home/<your-username>/.vscode-server/bin/*/ -name vscode-remote-lock.<your-username>.* -delete
    echo "Killed the troublemaker! ^_^"
  fi
done

The file names and the folder names may differ from machine to machine. So find the names on your machine and paste them in the script. Then run the script and you're good to go.

Comments

5

VSCode in my Windows machine was asking for password even with my key correctly configured (it works from the terminal).

My problem was that VSCode was choosing a wrong user. I was using a host configured in my ssh config file, and VSCode was setting the user as DOMAIN\windowsuser instead of linuxuser. I solved it configuring the correct user in my .ssh/config file:

Host dados
    HostName vrt1234
    User linuxuser

You must pass the user name that exists in your linux machine (without the DOMAIN\ part)

2 Comments

And what is the correct user to add to the config file then? user or DOMAIN\user?
It is the unix user. No `DOMAIN` here.
4

(macos+vscode)

Only this worked for me: https://www.backarapper.com/add-ssh-keys-to-ssh-agent-on-startup-in-macos/

ie: adding the key by ssh-add and then writing this in the ~/.ssh/config file:

  Host *
      UseKeychain yes
      AddKeysToAgent yes
      IdentityFile ~/.ssh/[your-secure-ssh-key-name]

1 Comment

You win :). Thank you!
3

For anybody Googling that runs into this thread. This is more than likely the correct answer if you are trying to setup Remote-SSH for the first time, or, trying to setup your keys for the first time. As in, this is probably your answer if you are a student or fresh graduate and trying to setup a nicer work environment.

If you defined the absolute path in your ~/.ssh/config using backslashes, you actually must replace those with double backslashes.

Screenshot of the sentence that fixed my 3 hour long issue. This is how long things take if you refuse to read, or are just dumb, like me.

Link to the section of the article referenced in my screenshot can be found here: https://code.visualstudio.com/docs/remote/troubleshooting#_improving-your-security-with-a-dedicated-key

And no, I don't know what I'm talking about or why they set it up like this, and no, I'm not a developer or anything like that, but if you have exhausted your options, try this.

Absolute path in the config, formatted like so should work:

IdentityFile C:\\path\\to\\my\\key

Or, more clearly, in your config, put:

IdentityFile C:\\Users\\your_username\\.ssh\\id_ed25519

Comments

2

In case you're having this problem in Windows, keep in mind that the public/private keys that you might use to connect to a remote machine from WSL aren't the same ones that VS Code will use to connect from Windows. You need to create a separate public/private key pair for Windows, and export that private key to the remote server too.

From VS Code remote debug tips and tricks:

In a Powershell window, create a public/private key pair just as you would in a Linux terminal:

ssh-keygen -t rsa -b 4096

Then export it to the remote server:

export USER_AT_HOST="your-user-name-on-host@hostname"
export PUBKEYPATH="$HOME/.ssh/id_rsa.pub"

ssh $USER_AT_HOST "powershell New-Item -Force -ItemType Directory -Path \"\$HOME\\.ssh\"; Add-Content -Force -Path \"\$HOME\\.ssh\\authorized_keys\" -Value '$(tr -d '\n\r' < "$PUBKEYPATH")'"

Make sure you can connect via passwordless SSH via PowerShell.

Finally, in VS Code. press Ctrl+Shift+P to open the command palette and select "Remote-SSH: Open SSH Configuration File..." and edit the config file like so:

Host [convenient name]
    HostName [hostname]
    User [username]
    IdentityFile C:/Users/[username]/.ssh/id_rsa*

Then when you run "Remote-SSH: Connect to Host..." in VS Code and choose the host above, it should connect without prompting for a password.

2 Comments

Remember that if you use windows, the IdentityFile path can use / If you use \, then you have double it: C:/Users\... or C:\\Users\\.... See Improving your security with a dedicated key (point 3) at code.visualstudio.com/docs/remote/…
@pocjoc: Thank you, I've edited the IdentityFile example to use forward slashes per your linked documentation. Interestingly, the single backslashes worked when I posted this answer over a year ago.
2

I ran into this issue recently this resolved it for me

  1. set parameter to force non-password authentication
Host <ip>
  HostName <ip>
  User <user>
  PasswordAuthentication no
  IdentityFile /home/<user>/.ssh/<key_name>
  1. ensure your key has the proper permissions
chmod 600 ~/.ssh/id_rsa

1 Comment

I had to check my permissions of Windows and remove some permissions because they were too open for private and public key: > Permissions for 'C:\\Users\\uic92979/.ssh/id_rsa' are too open.
2

For Linux and Windows WSL users (may work on Mac but untested)

  1. Download this ssh agent key caching script I created to your ~/.ssh/ folder

    key_agent.sh

  2. Link to it in your ~/.bashrc by adding these lines to the end of the file

    # Load the SSH key agent management script
    if [ -f "$HOME/.ssh/key_agent.sh" ]; then
      . "$HOME/.ssh/key_agent.sh"
    fi
    
    # Need to add git config changes here to persist them
    git config --global core.sshCommand "ssh -o IdentityAgent=$SSH_AUTH_SOCK"
    
  • The key agent script will check whether keys are already loaded and if so apply them to the current terminal or if not load any of the keys that have not been loaded and apply them.

  • The git global config sets git to always use the running ssh-agent to resolve keys.

Comments

1

Not enough rep to comment, but if you followed the steps from this Stack Overflow post and are still running into issues, your VSCode Remote-SSH config file path may not be set.

Make sure that the setting remote.SSH.configFile is set to ~/.ssh/config.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1

You could also type Ctrl + Shift + P to open the Command Palette.

Inside the Command Palette type,

Remote-SSH: Kill VS Code Server on Host... 

You will be required to type in your server password for it to work.

Comments

0

In case this helps someone, i had a similar issue where VSC was asking for a password (instead of a passphrase). I noticed that my key was on a network drive and it looks like VSC cannot read it there. I moved it to a local file (C:) and it worked.

Comments

0

For me it was that my public auth ssh was not working and my home directory permissions were the problem. I had to remove group and other write permissions to my home directory and then everything worked:

chmod go-w ~/

Comments

0

In fact none of the above did not work for me. Me I followed the below steps after a little research:

  1. On my Mac terminal I tried to ssh with the verbose tag to see more logs: ssh -v myUser@myIp

  2. Here I saw that the private key was to open and for security reason it jumped the pub key login so I did: chmod 600 ~/.ssh/id_rsa

  3. After I made up a new connection to my VM through VS code by running with the following tags: ssh -T myUser@myIp -A

    -A Enables forwarding of connections from an authentication agent such as ssh-agent(1). This can also be specified on a per-host basis in a configuration file.

    -T Disable pseudo-terminal allocation.

Comments

0

When you click on the Remote-SSH icon in VS Code and then connect to a host, a prompt box appears where you choose your hostname. In that box, there will be an option to configure SSH Hosts. Clicking there opens the config file, where you need to add the exact path of your private key file, like this:

Host <enter host>
  HostName <enter hostname>
  User <enter username>
  IdentityFile <path to the private key file, usually ~/.ssh/id_rsa>

Comments

0

I tried this solution

https://marksmakerspace.com/code/vscode-keeps-asking-for-ssh-kay-passphrase

ssh-add --apple-use-keychain ~/.ssh/yourkeyfilename

Comments

0

I solved it by adding

Host gitlab.abc.xyz
  HostName gitlab.com
  IdentityFile /home/username/.ssh/base1abc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.