4

I have to run the following commands each time I restart:

ssh-agent /bin/bash
ssh-add ~/.ssh/key_rsa

First question is why is /bin/bash necessary and what it is used for?

Second, Is it ok I run these commands every time on restart, or should I put this in .bashrc or somewhere so I don't have to ssh-add everytime.

Note that I use screen and every screen tab is different and I have to run these commands for every screen tab.

Edit:

For example when I need to push to github I need to do ssh-add ~/.ssh/github_rsa, but this doesn't work unless I do ssh-agent /bin/bash first.

This might be related to screen because plain ssh-agent doesn't work, and ssh-agent is already running, yet I have to do ssh-agent /bin/bash.

3
  • Just curious what problem you are trying to solve - how do you determine "I have to run the following" ; to get an ssh key in each login I have the following as a single line in my .bashrc "eval keychain --eval id_rsa id_dsa"
    – nwaltham
    Oct 2, 2014 at 11:01
  • @nwaltham for example I run these commands if I want to push to github, I need to do ssh-add github_rsa if I haven't done already. (ssh-add doesn't work by default so I have to run ssh-agent /bin/bash first). Can you explain your solution a little more, what is keychain and post it as answer. Also see my edit please.
    – mattheew
    Oct 2, 2014 at 11:50
  • You should check out ssh-ident if you haven't found or heard of it yet. It's obviously been a while since the op, but you never know. It fixes your issue of having to always restart the agent and add your keys - and it does it in just about the most secure way currently available ... unless I've missed something. github.com/ccontavalli/ssh-ident Oct 31, 2019 at 8:29

2 Answers 2

4

ssh-agent is a daemon that caches the decrypted ssh key in memory when you ssh-add it so that you don't have to type in the password to decrypt it each time ssh tries to use it for authentication. It being cached in memory means that the next time ssh-agent is started the key has to be decrypted and added to the agent again, which is why you need to ssh-add it again.

The /bin/bash part isn't actually necessary. Normally when you start ssh-agent it will print some environment variables to stdout which are meant to be evaled so that ssh knows where the ssh-agent socket is. If you instead give ssh-agent a command when you start it, it will run that command with the environment variables set instead.

This means that the bash instance you're using after running the command is a subshell of the one you were using before. The issue with adding that to your .bashrc is that the new shell ssh-agent starts would run ssh-agent again, causing a recursive loop, which you don't want. Instead you might want to just start ssh-agent in your .bash_profile and eval the output instead of starting a subshell, and then use ssh-add yourself manually once each time you log in.

Another option would be to use the GNOME Keyring's SSH agent emulation instead of using ssh-agent. Unlike ssh-agent that doesn't save your passphrase, the GNOME Keyring will store the passphrase to the key in a keyring so you don't have to add keys manually.

3
  • How is this related to me using screen. Also /bin/bash part is necessary in my case, when I run only ssh-agent and do a ssh-add -l it errors: Failed connection to auth agent. What could be the problem about that? Finally is it a good practice to put ssh-add id_rsa to bashrc along with ssh-agent? Thanks @kyrias.
    – mattheew
    Oct 2, 2014 at 12:21
  • It's related because you don't have the environmental variables set in screen. When you start ssh-agent it will print some output containing commands to set variables (foo="bar") and then exports them to the environment (export foo), and since you haven't ran those commands ssh-add won't be able to find the ssh-agent socket (The SSH_AUTH_SOCK part of the output from ssh-agent).
    – remmy
    Oct 2, 2014 at 12:31
  • Re: ssh-add id_rsa, if your key doesn't have a passphrase set it will work fine, but then ssh-agent would be rather pointless to use, and if your ssh key does have a passphrase it might not work because it possibly won't be able to prompt you for the passphrase to decrypt the key.
    – remmy
    Oct 2, 2014 at 12:31
2

I recommend you use keychain http://www.funtoo.org/Keychain

Keychain helps you to manage ssh and GPG keys in a convenient and secure manner. It acts as a frontend to ssh-agent and ssh-add, but allows you to easily have one long running ssh-agent process per system, rather than the norm of one ssh-agent per login session.

Firstly you would install it according to your distribution for debian/ubuntu

apt-get install keychain

Then add the following to ~/.bashrc file:

eval `keychain --eval id_rsa`
1
  • This is the best way to go. MUCH better than prompting every single time you subshell. Aug 1, 2018 at 19:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .