Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh-add, add a private key automatically

I have a public-private ssh key pair (generated by ssh-keygen without a passphrase) and I use it with my bitbucket account, in particular with a mercurial repository.

Usually I add manually the private key in my home working pc, using ssh-add ~/.ssh/id_rsa. I'm searching a way to do this automatically at every boot. There are many tutorials but I still have some problems.

I have tried with:

  1. Add the line ssh = ssh -i ~/.ssh/id_rsa -C in my ~/.hgrc mercurial settings file. This one doesn't work.

  2. Create ~/.ssh/config ssh configuration file with the following content:

    Host bitbucket-ssh
        Hostname ssh://[email protected]/...
        IdentityFile ~/.ssh/id_rsa
    

The second one does not work too.

What I'm wrong with? Should I use a simple bash script running ssh-add at every boot?

Thanks in advance.

like image 787
eang Avatar asked Dec 01 '22 21:12

eang


1 Answers

I know this question is old, but I couldn't find the (imho) correct answer in any of the related questions.

Don't add anything to any of your shell startup scripts, this is unnecessary hackery.

Instead, add

AddKeysToAgent yes

to your .ssh/config

Like this, ssh-add is run automatically the first time you ssh into another box. You only have to re-enter your key when it expires from ssh-agent or after you reboot.

like image 163
Toby Avatar answered Dec 29 '22 04:12

Toby