Skip to content

Instantly share code, notes, and snippets.

@coulterpeterson
Last active August 15, 2023 15:03
Show Gist options
  • Save coulterpeterson/c18b925a169349e529eea43e450b782c to your computer and use it in GitHub Desktop.
Save coulterpeterson/c18b925a169349e529eea43e450b782c to your computer and use it in GitHub Desktop.
Install SSH Key in Windows and WSL
# Credit: https://stackoverflow.com/a/63473832/8143105
# Make sure you're running PowerShell as an Administrator
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent
ssh-add
# Enter password
code # Or open regularly - should now have your SSH key pre-loaded into env as expected
# Now to add to WSL
# Symlink to the Windows SSL key from within WSL
cd ~/.ssh/
ln -s /mnt/c/Users/WINDOWS_USERNAME/.ssh/id_rsa id_rsa
nano /etc/wsl.conf
# Add this to the conf file:
[automount]
options = "metadata"
# Restart wsl
wsl --shutdown
wsl
# Set SSH key permissions from a Linux context
chown YOU:YOU ~/.ssh
chown YOU:YOU ~/.ssh/id_rsa
chmod 0700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
# start the ssh-agent in the background, then add
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
# If you have issues with the key, try importing into PUTTYGEN and exporting as an OpenSSH key
# If you've lost your public key, you can use the following to regenerate it:
ssh-keygen -yf ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub
# You can then move it to your Windows side and symlink it
mv ~/.ssh/id_rsa.pub /mnt/c/Users/WINDOWS_USERNAME/.ssh/
ln -s /mnt/c/Users/WINDOWS_USERNAME/.ssh/id_rsa.pub ~/.ssh/id_rsa.pub
# To make it so you only have to enter your SSH passphrase once per WSL session, first install keychain:
sudo apt install keychain
# Then add the following to the bottom of your .bashrc file:
eval `keychain --quiet --eval --agents ssh id_rsa`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment