Using SSH-Agent the right way in Windows 10 WSL2
If you use
ssh-agent with an encrypted ssh key, or use it for agent forwarding, you may have come to realize that even though you started an agent session using
eval $(ssh-agent -s) it does not persist when you open a new terminal window. It does not even work with a new tmux window or pane.
Fortunately, it's pretty simple.
keychain to the rescue.
sudo apt-get install keychain
Edit your `~/.bashrc` , `~/.zshrc` or whatever rc file that corresponds to your weird shell of choice (I'm not judging you) and add the following to the bottom of your file.
# For Loading the SSH key /usr/bin/keychain --nogui $HOME/.ssh/id_rsa source $HOME/.keychain/$HOSTNAME-sh
IMPORTANT: For some reason, the variable
$HOSTNAMEis unset. So, you need to change it to the hostname of your computer (Use
hostnamecommand to find out the hostname of your machine)
So, this is how my
~/.zshrc looks like
# For Loading the SSH key /usr/bin/keychain --nogui $HOME/.ssh/id_rsa source $HOME/.keychain/LAPTOP-C9EO4ILB-sh
And that is all. When you open a new shell, it will ask you to enter the password for your ssh key, and then onwards it will greet you with this sweet screen saying that
keychain took care of loading your ssh-key