Configuring PyCharm, Bash on Ubuntu on Windows 10 & WinSCP to work with SSH keys in Keepass using the KeeAgent Plugin
This is a small guide on how to set up PyCharm, Bash on Ubuntu on Windows & WinSCP on Windows to use SSH keys which are saved in KeePass. This significantly improves the user experience while keeping the keys save. It also alleviates any issues with Cygwin which prompted me to set this all up (see setup with Babun before https://gist.github.com/duebbert/1cd30115cf8ade37aa5b619080154e85)
- Activate Bash on Ubuntu on Windows 10
- Install KeePass2
- Install KeeAgent
- Install PyCharm (optional)
- Install WinSCP (optional)
- Start KeePass
Tools -> Options -> KeeAgent
- Activate "Create mSys compatible socket file" and set path to a file
like
C:\Users\<USERNAME>\keeagent_msys.socket
- Add SSH keys to an entry in KeePass. You can just add them as attachments and then activate them in the tab "KeeAgent".
- Start Bash on Ubuntu on Windows
- Get python script to bridge between WSL and Windows from https://gist.github.com/duebbert/4298b5f4eb7cc064b09e9d865dd490c9:
wget https://gist.githubusercontent.com/duebbert/4298b5f4eb7cc064b09e9d865dd490c9/raw/2d47ef8e88087d79dbf77b64117f3b5a13d6598a/msysgit2unix-socket.py
- Create a startup script
~/start_keepass_agent.sh
which will also be used by a batch file later:
#!/bin/sh
export KEEAGENT_SOCK="/mnt/c/Users/kduebbert/keeagent_msys.socket"
ssh_auth_tmpdir=`mktemp --tmpdir --directory keeagent-ssh.XXXXXXXXXX`
export SSH_AUTH_SOCK="${ssh_auth_tmpdir}/agent.$$"
PIDFILE="${ssh_auth_tmpdir}/pid.$$"
python3 ~/msysgit2unix-socket.py --pidfile $PIDFILE $KEEAGENT_SOCK:$SSH_AUTH_SOCK
- Add the file to
.bashrc
so that you can use SSH in Bash seamlessly:source ~/start_keepass_agent.sh
- Create batch file in e.g. d:\git.bat: https://gist.github.com/duebbert/c4f26c9d0691b7d4a0e932e7d9f36271
@echo off
SETLOCAL enableextensions enabledelayedexpansion
:: Thanks:
:: - https://stackoverflow.com/questions/37000166/getting-pycharm-to-recognize-python-on-the-windows-linux-subsystem-bash-on-wind
:: ###################################################################################
:: Set up the current directory, so that we can change into it before calling git
:: ###################################################################################
:: Extract the drive letter
SET "DIR=%cd%"
:: Convert drive letter to lower case and add /mnt/
FOR %%i IN ("A:\=/mnt/a/" "B:\=/mnt/b/" "C:\=/mnt/c/" "D:\=/mnt/d/" "E:\=/mnt/e/" "F:\=/mnt/f/" "G:\=/mnt/g/" "H:\=/mnt/h/" "I:\=/mnt/i/" "J:\=/mnt/j/" "K:\=/mnt/k/" "L:\=/mnt/l/" "M:\=/mnt/m/" "N:\=/mnt/n/" "O:\=/mnt/o/" "P:\=/mnt/p/" "Q:\=/mnt/q/" "R:\=/mnt/r/" "S:\=/mnt/s/" "T:\=/mnt/t/" "U:\=/mnt/u/" "V:\=/mnt/v/" "W:\=/mnt/w/" "X:\=/mnt/x/" "Y:\=/mnt/y/" "Z:\=/mnt/z/") DO CALL SET "DIR=%%DIR:%%~i%%"
:: Change backslash to forward slash. Must run after converting the drive letter.
SET "DIR=%DIR:\=/%"
:: ###################################################################################
:: Fix the arguments
:: ###################################################################################
SET ARGS=%*
:: Convert drive letter to lower case and add /mnt/
FOR %%i IN ("A:\=/mnt/a/" "B:\=/mnt/b/" "C:\=/mnt/c/" "D:\=/mnt/d/" "E:\=/mnt/e/" "F:\=/mnt/f/" "G:\=/mnt/g/" "H:\=/mnt/h/" "I:\=/mnt/i/" "J:\=/mnt/j/" "K:\=/mnt/k/" "L:\=/mnt/l/" "M:\=/mnt/m/" "N:\=/mnt/n/" "O:\=/mnt/o/" "P:\=/mnt/p/" "Q:\=/mnt/q/" "R:\=/mnt/r/" "S:\=/mnt/s/" "T:\=/mnt/t/" "U:\=/mnt/u/" "V:\=/mnt/v/" "W:\=/mnt/w/" "X:\=/mnt/x/" "Y:\=/mnt/y/" "Z:\=/mnt/z/") DO CALL SET "ARGS=%%ARGS:%%~i%%"
:: Backslash to forward slash
SET ARGS=!ARGS:\=/!
:: ###################################################################################
:: Execute it!!!
:: ###################################################################################
If %PROCESSOR_ARCHITECTURE% == x86 (
C:\Windows\sysnative\bash.exe --login -c '. ~/start_keepass_agent.sh; cd "%DIR%"; git %ARGS%'
) Else (
bash.exe --login -c '. ~/start_keepass_agent.sh; cd "%DIR%"; git %ARGS%'
)
- Save script
- Start PyCharm
- Go to
Settings -> Version Control -> Git
: Path to Git executable:D:\git.bat
SSH executable: Native - Try out to clone a Git project or update one
WinSCP should work out of the box. Enter the server name, port and user name and connect to a server.
This doesn't work for me, putty and winscp use keeagent fine, but keeagent never reports bash trying to access the key. Any ideas how to debug?
Update: apparently it fails the first time, and works after that. The first time I issue a git command I am prompted for a password. I ctrl-c out of that, repeat the command, then it works.