The Problem Link to heading
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.
The Solution Link to heading
Fortunately, it’s pretty simple. keychain
to the rescue.
Install keychain
sudo apt-get install keychain
Edit your ~/.bashrc
, ~/.zshrc
or whatever rc file that corresponds to your shell of choice and add the following to the bottom of your file.
Note: If you are not sure which shell you are using,you can run the command
echo $SHELL
and it will show you whether you are using BASH or ZSH
# For Loading the SSH key
/usr/bin/keychain -q --nogui $HOME/.ssh/id_rsa
source $HOME/.keychain/$(hostname)-sh
So, this is how my ~/.zshrc
looks like
# For Loading the SSH key
/usr/bin/keychain -q --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 keychain
takes care of loading your ssh-key