Add SSH Agent on Login (Linux)


I’m not sure if this is best necessarily best practice (so please comment below). I had the need to be using SSH key based authentication a lot. So rather than manually running the SSH Agent and then adding my private key (which then meant entering the passphrase on each use), I added this to my BASH profile so when logging on these would start; sure i’d need to enter my SSH key passphrase, but i’d only need to do this once at logon, and not each time I wanted to use the key.

To set this up I edited the ~/.profile file within my home directory, and added the following lines to the bottom of the file:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Then log off, when I logged on again I was prompted for my SSH key passphrase, then when entered I could use key authentication as many times as I needed during my session. It was handy when working with some Ansible configuration!

Leave a Reply

Your email address will not be published. Required fields are marked *