Using KeePass & KeeAgent with Visual Studio Code

Posted by  symos  in  Web development

When using Visual Studio Code in Windows, a lot of times you might want to connect to a remote server via SSH, so you can edit files on that server directly, instead of setting up a local development environment or pulling and pushing files repeatedly, e.g. via FTP.

Although documentation on how to install and use the remote SSH extension exists, things start to get a bit more complicated when you want to use secure key based authentication.

You obviously don't want an unencrypted private key lying around in your PC and you also don't want to have to type in a password every time you use the key to login to a remote server.

This is why most people opt for using an SSH agent, like KeeAgent for KeePass. That way, you can store your private key along with its password in encrypted form in KeePass, while SSH clients (like Putty) are able to access/use the key when needed to initiate a login session, without asking for a password every time.

Problem is, Visual Studio Code doesn't work with Putty in Windows and the Windows OpenSSH client doesn't work with KeePass/KeeAgent. The only solution provided is to use the Windows SSH agent, but you might not prefer to do this for a variety of reasons, which I am not going to list here.

So, what is one to do? Fortunately, I've found there is a way to make Visual Studio Code work with KeePass/KeeAgent without too much fuss.

First of all, uninstall the Windows OpenSSH client, by going into Windows Settings -> Apps -> Apps & Features and clicking on the "Optional Features" link. From the list, select the OpenSSH client, uninstall it and restart the system.

Then, install Git for Windows (if you haven't already) and follow the instructions here to create the two socket files (section "Tell Git Bash to use KeeAgent"). You don't actually need to edit the .bashrc file, though if you want to (in order to be able to also use the agent within Git Bash), note that the file should be named ".bash_profile" in newer versions of Git, not ".bashrc".

Finally, add a Windows environment variable with a name of "SSH_AUTH_SOCK" and value of "C:\Temp\cyglockfile". If you don't know how to, see here.

Now, if you open a PowerShell terminal, navigate to "C:\Program Files\Git\usr\bin" and run "./ssh user@host" where "user" is your username and "host" is a host server's IP to which you can login with a key stored in KeeAgent, it should log you in using the stored key.

It should now also work within Visual Studio Code too, without any extra configuration. If it doesn't, try opening VS Code's Remote-SSH settings and explicitly set the "Path" option to "C:\Program Files\Git\usr\bin\ssh.exe".

Hope this works for you and makes your life just a little bit easier, as it did for me.

comments powered by Disqus