Can I permanently add ssh private key to my user agent?
Asked Answered
R

2

11

Somewhat of a noob question but everyday at work when I open git bash I have to start the ssh-agent daemon and I have to add my ssh-private key to the user-agent so that Github knows who I am.

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

If I dont do this I cannot pull/push to github.

It gets a little annoying to have to do this everyday, is there a way to add it permanently?

Rhabdomancy answered 16/11, 2020 at 20:57 Comment(1)
Most likely, you can simply put the two commands in your .bash_profile or .bashrc. I don't know exactly what kind of environment Git bash will run or run in, so there might be something more complicated you need to do to avoid running multiple instances of ssh-agent.Disjoin
D
13

Instead of using ssh-agent, put the following in your .ssh/config file:

Host github.com
    IdentityFile ~/.ssh/id_rsa

An agent is primarily useful either to avoid creating a large number of configurations in .ssh/config (as any connection will attempt to use a key found in the agent), or for allowing remote SSH sessions to reach back to your local machine for necessary keys.

Disjoin answered 16/11, 2020 at 23:2 Comment(2)
To restart ssh agent after the change: unix.stackexchange.com/a/520343/238934Dogbane
You don't need to restart the agent. Each invocation of ssh will read the configuration file. If you specify an explicit file, ssh will not need to check with the currently running agent.Disjoin
E
1

Another way that it solved myself the same problem, was moving the private key, e.g id_rsa, to the default ssh key location, ~/.ssh.

cp /your/key/location/id_rsa ~/.ssh

It seems that for some reason, when you add an identity from a different location with the -i option of the ssh-add command, it doesn't add it permanently.

Epact answered 17/3, 2022 at 11:18 Comment(1)
Mine is already there, but my file name is not a standard one like id_rsa, and that's causing the problem.Deluge

© 2022 - 2024 — McMap. All rights reserved.