ssh-agent
is a thing we normally take for granted when using SSH on linux. We add the key, then it’s there until we restart.
When switching to WSL2 we quickly find that isn’t the case. We might first try eval $(ssh-agent -s)
but that is lost every time we open a new window or tmux session. Another option is to stop using encrypted SSH keys. For those of us who prefer to keep our keys encrypted, there is a better way. We can use the keychain
package to persist our SSH keys between windows and sessions.
Install keychain
sudo apt-get install keychain
Edit your ~/.bashrc, ~/.zshrc or your choice of shell, and add the following to the bottom of your file.
# Store SSH key between sessions
/usr/bin/keychain -q --nogui $HOME/.ssh/id_rsa
source $HOME/.keychain/$HOST-sh
This will prompt you for the key on startup if it doesn’t already exist. Adjust to suit your keys.
tags: wsl2 - ssh