How do I get Visual Studio Code access to Bitbucket using an SSH key with a password

Viewed 14665

I have a personal and professional Bitbucket account. I am able to access both via Git command line, but cannot get the professional account to work from within the Visual Studio Code GUI interface.

The difference between the two accounts is that my professional account has a passphrase on the SSH key and prompts me for it when I access the remote repo. I cannot get Visual Studio Code to prompt me for the passphrase.

When I try to use the GUI interface and push/pull my changes: enter image description here

I get an error reading ‘Git: git@bitbucket.org: Permission denied (publickey).’ enter image description here

The git log output shows:

enter image description here

But if I jump in the integrated Terminal and ‘git push’ it is successful: enter image description here

I have read through a lot of different approaches to fix this and none seem to be doing the trick.

I have ssh-agent running. I have added the key to the agent. ssh-add -L shows the loaded key.

I have added "remote.SSH.showLoginTerminal": true, and "remote.SSH.useLocalServer": false to the VS Code settings.json per the instructions provided on https://code.visualstudio.com/docs/remote/troubleshooting This has no impact.

I have added the Remote Development extension pack and followed instructions at https://code.visualstudio.com/docs/remote/ssh but was unable to complete them since Bitbucket shell access is disabled

I have tried adding AddKeysToAgent yes on the top of the .ssh/config file as was recommended in another post. This didn't change anything.

I have tried git config --global credential.helper cache but it looks like that will only work with HTTPS, not SSH.

Despite all that I have tried, VS Code still fails to connect with the error above. I can remove the passphrase from the key and it works fine, but I would like to make it work with the passphrase.

What am I missing?

1 Answers

This might help. From the vs code docs "Remote Development Tips and Tricks" https://code.visualstudio.com/docs/remote/troubleshooting

Enabling alternate SSH authentication methods

If you are connecting to an SSH remote host and are either:

  • Connecting with two-factor authentication
  • Using password authentication
  • Using an SSH key with a passphrase when the SSH Agent is not running or accessible

then VS Code should automatically prompt you to enter needed information. If you do not see the prompt, enable the remote.SSH.showLoginTerminal setting in VS Code. This setting displays the terminal whenever VS Code runs an SSH command. You can then enter your authentication code, password, or passphrase when the terminal appears.

If you are still having trouble, you may need to the following properties in settings.json and retry:

"remote.SSH.showLoginTerminal": true,
"remote.SSH.useLocalServer": false

If you are on macOS and Linux and want to reduce how often you have to enter a password or token, you can enable the ControlMaster feature on your local machine so that OpenSSH runs multiple SSH sessions over a single connection.

To enable ControlMaster:

  1. Add an entry like this to your SSH config file:

    Host *
        ControlMaster auto
        ControlPath  ~/.ssh/sockets/%r@%h-%p
        ControlPersist  600
    
  2. Then run mkdir -p ~/.ssh/sockets to create the sockets folder.

Setting up the SSH Agent

If you are connecting to an SSH host using a key with a passphrase, you should ensure that the SSH Agent is running locally. VS Code will automatically add your key to the agent so you don't have to enter your passphrase every time you open a remote VS Code window.

To verify that the agent is running and is reachable from VS Code's environment, run ssh-add -l in the terminal of a local VS Code window. You should see a listing of the keys in the agent (or a message that it has no keys). If the agent is not running, follow these instructions to start it. After starting the agent, be sure to restart VS Code.

Windows:

To enable SSH Agent automatically on Windows, start a local Administrator PowerShell and run the following commands:

# Make sure you're running as an Administrator
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent

Now the agent will be started automatically on login.

Linux:

To start the SSH Agent in the background, run:

eval "$(ssh-agent -s)"

To start the SSH Agent automatically on login, add these lines to your ~/.bash_profile:

if [ -z "$SSH_AUTH_SOCK" ]; then
   # Check for a currently running instance of the agent
   RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
   if [ "$RUNNING_AGENT" = "0" ]; then
        # Launch a new instance of the agent
        ssh-agent -s &> .ssh/ssh-agent
   fi
   eval `cat .ssh/ssh-agent`
fi

macOS:

The agent should be running by default on macOS.

10/8/2020

Related