31

I'm trying to have the following commands be auto-executed when I login to my server via ssh:

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

My ssh key has a passphrase and I'm fine with entering it once per login.

I tried putting this in my .bashrc file, however I believe that ssh-agent starts a new bash session. When I try to login after having this in my .bashrc, it gets stuck, and I have to type 'exit' to then see the 'enter passphrace to unlock key' prompt

Any other suggestions?

Server is running Ubuntu LTS

  • Why key agent on the server? It should be on your local client that you are connecting from. – Zoredache Mar 2 '15 at 5:49
  • @Zoredache I want to be able to do git pull and such on the remote server – Click Upvote Mar 2 '15 at 16:21
  • Then run an SSH agent on your local machine, and forward the agent. – Zoredache Mar 2 '15 at 17:39
  • @Zoredache Thanks, didn't know that was possible. However I'd still like to be able to do ssh-add / ssh-agent from within a bash script, even from my local machine. Its a pain to have to run those commands manually. – Click Upvote Mar 3 '15 at 8:39
46

You can try adding this:

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

This way the ssh-agent does not start a new shell, it just launches itself in the background and spits out the shell commands to set the appropriate environment variables.

As said in the comment, maybe you do not want to run the agent at all on the remote host, but rather on the box you are working from, and use

ssh -A remote-host

to forward the services of your local ssh agent to the remote-host.

For security reasons you should only use agent forwarding with hosts run by trustworthy people, but it is better than running a complete agent remotely any time.

  • When I replace ssh-agent /bin/bash with ssh-agent -s, and then I try to do git pull, I'm still asked for the passphrase of the private key to unlock it. That's not really what I want, I want to only have to enter the passphrase on doing ssh-add, and not have to repeat it each time I run any git commands. Any ideas? – Click Upvote Mar 3 '15 at 8:34
  • and running the agent locally and using ssh -A is not an option for you? The problem is, that you can not 'restart' the ssh-agent on every connection. Every time you start it, it knows 'nothing' and when you ssh-add the key you will have to decrypt it ... – Tobi Oetiker Mar 3 '15 at 11:09
  • for your .ssh/config file: Host remote-host ControlMaster auto ControlPath ~/.ssh/master-%l-%r@%h:%p ControlPersist 2h – Tobi Oetiker Mar 3 '15 at 11:10
  • 1
    oops just realised in bash you need an extra eval in front of the ssh-agent call .. updated my answer above – Tobi Oetiker Mar 12 '15 at 23:15
  • 1
    you could create an alias – Tobi Oetiker Mar 17 '15 at 12:27
1

One alternative is to use Funtoo's Keychain. Then you can stick this one-liner in your bash shell:

eval $(keychain --eval id_rsa)

That does the same thing (launches ssh agent, etc.), while also not running an ssh-agent process for each subshell. Instead it looks for "already running" instances you own and attaches you to them.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.