Docker Desktop, Alpine Linux, WSL, SSH
Keychain: Manage SSH Agent Sessions in Alpine Linux
Or: How to remove endless number of sessions
Note: if you want to know how to create your own SSH keys, visit Github to read Generating a new SSH key and adding it to the ssh-agent, and Testing your SSH connection.
When I open Windows Terminal into my Windows Subsystem for Linux (WSL) instance, I’m usually greeted by something like this:
Which means my current .bashrc
is adding my SSH private keys to my SSH agent. It works well, here is my current .bashrc
:
However, if I open a second terminal session, here is what I get:
Notice the first line? In the first picture I shared, my terminal session into Alpine says Agent pid 58
. In the second session I opened (above), the output says Agent pid 97
. Why? Because a new ssh-agent session is being created every time I instantiate a new session into Alpine.
While this still works, it’s not very tidy.
Enter Keychain
From nixCraft:
Keychain is a special bash script designed to make key-based authentication incredibly convenient and flexible […] The keychain act as a manager for ssh-agent, typically run from ~/.bash_profile. It allows your shells and cron jobs to share a single ssh-agent process.
To install in Alpine Linux, just run sudo apk add keychain
.
You can see in the Alpine Linux package repository that the package is available and up to date:
Update the bash init script
My new .bashrc
script is updated as follows:
Notice that I’ve commented out the line to automatically start the SSH agent, and replaced it with the keychain
command below. Following the keychain
command is a newline-delimited list of keys that I want to add to the SSH agent.
Lastly, I use the source
command to initiate the bash script created when I installed the keychain
package.
Now when I open to sessions into the WSL Alpine Linux, I am greeted by a message from keychain stating that the same session is being used. You can see this in the line Found existing ssh-agent: 122
.
If you want to verify which keys are currently added, you can still run ssh-add -l
.
That will clean up the endless clutter of SSH agent sessions chilling in your operating system’s background.