Sharing SSH With WSL

The WSL on Windows 10 is a new tool to run Linux commands and Linux dristibutions. More to the point, Windows finally has a mostly working bash environment that works with the CLI tools developers have embraced. Just like any tool that is of use, some things must be done to set them up appropriately. Sharing SSH keys is one of those things that you need to setup to use effectively.

SSH uses keys to cryptographically connect to network resources like or a DigitalOcean server. If you have done any Windows development, you likely already have a Putty or Cmder setup for SSH keys. If you have not used SSH on Windows, then sharing keys can be a moot point; as it would be easier to simply use the WSL.

WSL creates it’s own users with there own permissions and this is the real crux of the issue. This user will have it’s own access to files and it’s own setup for Git and SSH Config.

The first step within the WSL is to create an SSH config for your user that will use the Windows user’s files for keys.

mkdir -p ~/.ssh/config
touch ~/.ssh/config # create a config only if it doesn't exist
vi ~/.ssh/config #begin editing the config

Once in the Vi program (or use nano or whatever you like to edit with) enter the following config.

Host * IdentityFile /mnt/c/Users/WINDOWS_USER_NAME/.ssh/NAME_OF_KEY

You must replace WINDOWS_USER_NAME with the name of the account being used in windows. Also, tell the config file the NAME_OF_KEY that you’d like to share. Usually this is id_rsa.
Finally, save the new config file and then we must change it’s permissions so that Linux will allow it to be used.

chmod 600 ~/.ssh/config
chown $USER ~/.ssh/config

We are also able to share known_hosts so that the servers we are connecting to are in both environments.

touch /mnt/c/Users/WINDOWS_USER_NAME/.ssh/known_hosts
ln -s /mnt/c/Users/WINDOWS_USER_NAME/.ssh/known_hosts ~/.ssh/known_hosts

This creates a symlink with the Windows known_hosts for better sharing in the system.