40

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 username@x.x.x.x, 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!

sukrama
  • 661
  • 1
  • 5
  • 8

13 Answers13

16

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
sukrama
  • 661
  • 1
  • 5
  • 8
  • I had the same problem on Windows and the solution was related to this: https://superuser.com/questions/1296024/windows-ssh-permissions-for-private-key-are-too-open – Jsl Oct 15 '20 at 16:53
  • 1
    I had a non-standard key name on Mac OS. Running `ssh-add /path/to/private/key` on the local machine fixed it. – Taaam Apr 06 '22 at 13:02
8

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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 Sep 03 '20 at 07:33
  • @sukrama OK. How about the other suggestions on that issue? `remote.SSH.useFlock`, ` Kill VS Code Server on Host...`, ... – VonC Sep 03 '20 at 07:37
  • '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 Sep 03 '20 at 14:57
  • @sukrama Is your key passphrase-protected? – VonC Sep 03 '20 at 15:12
  • No it's not passphrase-protected. I did solve this problem though. It was a problem with ssh key path in config file. – sukrama Sep 03 '20 at 16:01
  • @sukrama Great! Well done. I have updated the answer accordingly. – VonC Sep 03 '20 at 16:06
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.

Sathira Silva
  • 51
  • 3
  • 5
2

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\user instead of user. I solved it configuring the correct user in my .ssh/config file:

Host dados
    HostName vrt1234
    User xxxxx
neves
  • 33,186
  • 27
  • 159
  • 192
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.

Kevin D.
  • 911
  • 1
  • 6
  • 18
  • 1
    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 https://code.visualstudio.com/docs/remote/troubleshooting#_configuring-key-based-authentication – pocjoc Jun 13 '23 at 08:12
  • @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. – Kevin D. Jun 13 '23 at 22:01
1

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.

crocteamgg
  • 65
  • 2
  • 7
  • 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. – Mike S. Apr 20 '23 at 18:52
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.

mycelium
  • 11
  • 1
  • 1
  • 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](/help/how-to-answer). – Coder Gautam YT Jul 20 '22 at 19:03
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.

sammens19
  • 161
  • 2
  • 4
1

(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]
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.

Jon
  • 766
  • 1
  • 9
  • 27
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 ~/
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.

user16217248
  • 3,119
  • 19
  • 19
  • 37
El Amo
  • 11
  • 2
0

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
Francisco Cortes
  • 1,121
  • 10
  • 19